【问题标题】:Android support library increases APK size a lotAndroid 支持库大大增加了 APK 的大小
【发布时间】:2014-01-27 20:12:36
【问题描述】:

我在我的 Android 项目中使用 AppCompat 支持库。 AppCompat 有很多我没有在我的应用程序中使用的绘图和资源。那些不必要的文件将我的 900K 应用程序增加到 2M 以上,我不喜欢。
创建APK文件时有什么方法可以排除这些文件吗?或者我应该在我的代码中混淆库而不是建立依赖关系? 我在 Android Studio 中使用 Gradle。

谢谢

EDIT 1 我已经在使用 proguard 了。但是proguard 不知道我不想拥有drawable-xxxhdpivalues-it

EDIT 2我也在使用Android Lint,这对我没有帮助,因为我不直接访问lib的代码,并且android在构建APK文件时添加了它们。

【问题讨论】:

  • 你试过 Proguard 了吗? proguard.sourceforge.net/index.html
  • 是的,我已经在使用了。但proguard 不知道我不想拥有drawable-xxxhdpivalues-it
  • 您是否尝试过将支持库 jar 作为依赖项添加到 libs 文件夹并将“-libraryjars libs”添加到 proguard 配置?
  • 当您根本不需要应用兼容资源时,这可能会有所帮助。我只是想排除一些我不需要的。
  • @KeyhanAsghari 使用github.com/marcoRS/lint-cleaner-plugin 可能会解决您的问题

标签: android apk android-support-library android-gradle-plugin android-appcompat


【解决方案1】:

从版本 24.2.0 开始,v4 支持库has been split into several smaller modules

因此,除了使用 shrinkResourcesproguardFiles 之外,还要确保您仅使用应用所需的特定模块。例如

如果您的应用仅使用 Compat 工具,如 NotificationCompat、ContextCompat 或 ResourcesCompat 等,请仅使用 compat 模块:

compile 'com.android.support:support-compat:24.2.0'

【讨论】:

    【解决方案2】:

    来自 Android Gradle 构建系统,从 0.7.0 版开始:

    product Flavor(和defaultConfig)的新选项允许通过aapt 的-c 选项过滤资源 您可以通过 DSL 传递单个值 (resConfig) 或多个值 (resConfigs)。 默认配置和风味中的所有值都被合并并传递给aapt。 请参阅“基本”示例。 在“基本”示例中:

    defaultConfig {
        ...
        resConfig "en"
        resConfigs "nodpi", "hdpi"
    }
    

    因此,请尝试以下方法来实现您的要求:

    productFlavors {
        ...
        frOnly {
            resConfig "fr"
        }
        ...
    }
    

    请注意,您可能还想包含*dpiportland 等。

    答案来自:Android Studio exports strings from support library to APK,感谢 Primoz990

    【讨论】:

    • 这看起来很有用,但是您的发布构建 APK 文件的大小有多大改进?
    • @Sam 我记不太清了,但是很多。在添加此配置之前,我的 APK 中充满了我不想添加的大量本地化文件夹(值-*,...)。
    【解决方案3】:

    shrinkResources 也可以是您的选择。它从 0.14 开始可用 - 但要小心 - 它仍然有一些坑,例如 protect resources when using shrinkResources

    【讨论】:

      【解决方案4】:

      虽然 OP 已经澄清他正在使用 proguard,但如果它对某人有帮助,我想发布一些代码,因为我可以使用接受的答案将我的应用程序从 3.8 MB 缩小到 3.1 MB,然后进一步缩小到 1.8 MB。前卫。我在我的应用级 build.gradle 文件中使用了这个配置:

      buildTypes {
              release {
                  minifyEnabled true
                  shrinkResources true
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 2016-03-23
        • 2016-01-07
        • 2018-03-25
        • 1970-01-01
        • 2021-11-15
        相关资源
        最近更新 更多