Android 支持库大大增加了 APK 的大小 [英] Android support library increases APK size a lot
问题描述
我在我的 Android 项目中使用 AppCompat
支持库.AppCompat
有很多我没有在我的应用程序中使用的可绘制对象和资源.那些不必要的文件使我的 900K 应用程序增加到 2M 以上,这是我不喜欢的.
创建 APK 文件时有什么办法可以排除这些文件吗?或者我应该在我的代码中混淆库而不是建立依赖关系?我在 Android Studio 中使用 Gradle.
I'm using AppCompat
support library in my Android project. AppCompat
has plenty of drawables and resources which I don't use in my app. That unnecessary files increases my 900K app to above 2M, which I don't like.
Is there any way to exclude those files when creating the APK file? Or I should obfuscate the library in my code instead of making a dependency?
I'm using Gradle in Android Studio.
谢谢
EDIT 1 我已经在使用 proguard.但是 proguard 不知道我不想有 drawable-xxxhdpi
或 values-it
例如.
EDIT 1 I am using proguard already. but proguard can't know I don't want to have drawable-xxxhdpi
or values-it
for example.
EDIT 2 我也在使用 Android Lint,这帮不了我,因为我不能直接访问 lib 的代码,而 android 在构建 APK 文件时添加了它们.
EDIT 2 I am also using Android Lint, which can't help me, beacuse I don't access to lib's code directly, and android adds them when building the APK file.
推荐答案
从 24.2.0 版本开始,v4 支持库 已拆分为几个较小的模块.
Starting from version 24.2.0, the v4 Support Library has been split into several smaller modules.
因此,除了使用 shrinkResources
和 proguardFiles
之外,还要确保您只使用应用程序需要的特定模块.例如
So, apart from using shrinkResources
and proguardFiles
, also make sure that you are using only the specific modules that your app needs. e.g.
如果您的应用仅使用 Compat 实用程序,如 NotificationCompat、ContextCompat 或 ResourcesCompat 等,请仅使用 compat 模块:
If your app only uses Compat utils like NotificationCompat, ContextCompat or ResourcesCompat etc., use only the compat module as:
compile 'com.android.support:support-compat:24.2.0'
这篇关于Android 支持库大大增加了 APK 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!