简体   繁体   English

从第三方库中删除未使用的资产

[英]removing unused assets from 3rd party library

I've run into this issue where minifyEnabled and shrinkResources give me sub-optimal results in the sense that; 我遇到过这个问题,minifyEnabled和shrinkResources在某种意义上给出了次优的结果; when I have included, 当我包括,

'com.google.android.gms:play-services-places:9.6.1'

I am not using Google Sign In API's yet, I get loads of sign in images. 我还没有使用Google Sign In API,我收到大量登录图片。 sign_in_images

How do I get rid of them from my APK? 如何从我的APK中删除它们? They seem to add a fair bit of KB's. 他们似乎添加了相当多的KB。

I am using proguard with minify and shrink resources as explained above. 我正在使用proguard缩小和缩小资源,如上所述。

There is a detail topic here at android developer user guide. 有一个细节主题在这里 ,在Android开发者用户指南。

Basically, you have lot of options to actually reduce the size of your apk by shrinking your resources. 基本上,您有很多选项可以通过缩小资源来实际减小apk的大小。 I have discussed in little brief about them, and I think Enable strict reference checks discussed below should solve your problem, but you can look at all available options to further reduce your apk size. 我已经对它们进行了简短的讨论,我认为下面讨论的启用严格参考检查可以解决您的问题,但是您可以查看所有可用选项以进一步减小您的apk大小。


Customizing which resources to keep 自定义要保留的资源

As the doc says, use the below xml at res/raw/keep.xml, to decide what to keep and what not to keep : 正如文档所说,在res / raw / keep.xml中使用下面的xml来决定要保留什么以及不保留什么:

 <?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*" tools:discard="@layout/unused2" /> 

And rest will be taken care of by build system (This is useful when using different build variants). 其余的将由构建系统处理(这在使用不同的构建变体时很有用)。


Enable strict reference checks 启用严格的参考检查

If you have your code or your library referencing resources like below : 如果您的代码或库引用了如下资源:

 getResources().getIdentifier("image1", "drawable", getPackageName()) 

Then, in this case resource shrinker behaves defensively by default and marks all resources with a matching name format as potentially used and unavailable for removal. 然后,在这种情况下,资源缩减器默认情况下会采取防御措施,并将所有具有匹配名称格式的资源标记为可能已使用且无法删除。

So, add following to res/raw/keep.xml 因此,请将以下内容添加到res / raw / keep.xml

 <?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:shrinkMode="strict" /> 

Adding this should solve your problem. 添加这个可以解决您的问题。


Remove unused alternative resources 删除未使用的替代资源

Resource Shrinker does not remove alternative resources, like, alternative drawable resources for different screen densities, alternative string resources for different languages, etc. Resource Shrinker不会删除替代资源,例如,针对不同屏幕密度的替代可绘制资源,针对不同语言的替代字符串资源等。

So, you can yourself choose what to keep, from your build file, say, you want to keep on strings in 'en' locale : 因此,您可以自己选择要保留的内容,例如,您希望在“en”语言环境中保留字符串:

 android { defaultConfig { ... resConfigs "en", "fr" } } 

This can reduce size significantly. 这可以显着减小尺寸。


If still resources are kept by resource shrinker, then manually exclude them using the first discussed method, and see if gets compiled and build properly, if not, then reason for keeping resources by resource shrinker, will become clear from the exception thrown while building. 如果仍由资源缩减器保留资源,则使用第一个讨论的方法手动排除它们,并查看是否编译和构建正确,如果没有,那么通过资源缩减器保留资源的原因将从构建时抛出的异常变得清晰。

Hope it helps ! 希望能帮助到你 !

If nothing else works to make the build system automatically detect unused resources, you might want to try explicitly excluding these via the packagingOptions DSL like: 如果没有其他方法可以使构建系统自动检测未使用的资源,您可能希望尝试通过packagingOptions DSL明确排除这些,如:

android {
    packagingOptions {
        exclude '/**/common_google_signin*.png'
    }
}

There may be some possibilities to reduce app size and remove unused assets. 可能存在一些减少应用程序大小和删除未使用资产的可能性。

You can delete unused assets from (':lib') project library. 您可以从(':lib')项目库中删除未使用的资产。

If you have imported @aar file than you can do something like this 如果您已导入@aar文件,则可以执行此类操作

compile ('com.facebook.android:facebook-android-sdk:3.22.0@aar'){
    exclude module: 'support-v4'
}

Still we have unused files in @aar file than find alternative way that replace that @aar to (':lib') , remove assets from there. 我们仍然在@aar文件中有未使用的文件, @aar不是找到将@aar替换为(':lib')替代方法,从那里删除资产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM