【问题标题】:Android Studio split APK over DensityAndroid Studio 通过密度拆分 APK
【发布时间】:2023-03-13 12:38:02
【问题描述】:
splits {

        // Configures multiple APKs based on screen density.
        density {
            // Configures multiple APKs based on screen density.
            enable true

            reset()
            // Specifies a list of screen densities Gradle should create multiple APKs for.
            include 'ldpi', 'mdpi', 'hdpi', 'xhdpi', 'xxhdpi', 'xxxhdpi'
            // Specifies a list of compatible screen size settings for the manifest.
            //compatibleScreens 'small', 'normal', 'large', 'xlarge'
        }
        // Configures multiple APKs based on ABI.
        abi {
            // Enables building multiple APKs per ABI.
            enable false
            // By default all ABIs are included, so use reset() and include to specify that we only
            // want APKs for x86, armeabi-v7a, and mips.
            // Resets the list of ABIs that Gradle should create APKs for to none.
            reset()
            // Specifies a list of ABIs that Gradle should create APKs for.
            include 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
            // Specifies that we do want to also generate a universal APK that includes all ABIs.
            universalApk true //generate an additional APK that contains all the ABIs

            //'armeabi' - Not surported by Realm since v2.0.0
        }
    }

我正在尝试按 Desity 拆分我的 APK 以减小 APK 大小,使用上面的 Gradle 将 APK 拆分为密度,但每个 APK 的可绘制资源是相同的。

这样的结果应该不会是xhdpi APK只包含drawable-ldrtl-xhdpi-v17、drawable-xhdpi-v4和mipmap-xhdpi-v4?

【问题讨论】:

  • 你所有的drawables都是xxxhdpi版本吗?这是没有必要的。查看developer.android.com/guide/practices/… 的第一个提示,这可以显着减小您的 APK 大小。此外,您可以考虑让 ldpi drawables 出来。它们可以从 mdpi 缩小。
  • 不,我们只有 xml drawables,我们不能 vectorDrawables.useSupportLibrary = true 因为其他依赖项

标签: android gradle split apk


【解决方案1】:

gradle 中的密度 apk 拆分示例:

apply plugin: 'com.android.application'
android {
  compileSdkVersion 19
  buildToolsVersion = rootProject.ext.buildToolsVersion
  defaultConfig {
    versionCode 12
    minSdkVersion 16
    targetSdkVersion 20
    buildConfigField "String", "FOO", "\"bar\""
  }
  splits {
    density {
      enable true
      exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
      compatibleScreens 'small', 'normal', 'large', 'xlarge'
    }
  }
}
// map for the version code
ext.versionCodes = [all:1, mdpi:2, hdpi:3, xhdpi:4, xxhdpi:5]
android.applicationVariants.all { variant ->
  // assign different version code for each output
  variant.outputs.each { output ->
    def key = output.getFilter(OutputFile.DENSITY) == null ? "all" : output.getFilter(OutputFile.DENSITY)
    output.versionCodeOverride = project.ext.versionCodes.get(key) * 100 + android.defaultConfig.versionCode
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多