11

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

感谢

编辑1我使用ProGuard了。但proguard不知道我不想要drawable-xxxhdpivalues-it例如。

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

+0

您是否尝试过Proguard? http://proguard.sourceforge.net/index.html –

+0

是的,我已经在使用。但proguard不知道我不想''drawable-xxxhdpi'或'values-it'例如。 –

+0

您是否曾尝试添加支持库jar作为libs文件夹的依赖项,并将“-libraryjars libs”添加到proguard配置中? –

回答

5

从Android的摇篮构建系统,因为0.7.0版本:

产品的风味(和defaultConfig)允许资源的过滤通过的aapt -c选项,您可以通过单一值(resConfig)或新选项通过DSL的多个值(resConfigs)。 将默认配置和风味的所有值合并并传递到aapt。 请参阅“基本”样本。 在 “基本” 样本:

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

所以,请尝试以下操作来实现你的要求为:

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

请注意,您可能还需要包括*dpiportland等。还有。

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

+0

这看起来很有用,但是您的发布版本APK文件的大小有多少改进? – Sam

+0

@Sam我不记得,但它是很多。在添加此配置之前,我的APK充满了大量本地化文件夹(值,*,...),我不想这样做。 –

0

虽然OP已经清除他正在使用proguard,应用程序从3.8 MB到3.1 MB使用接受的答案,并通过proguard仅仅1.8 MB。我在我的应用程序级的build.gradle文件中使用这种配置:

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

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

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

如果您的应用程序只使用NotificationCompat,ContextCompat或ResourcesCompat等Compat实用程序。,请只使用compat模块:

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