从第三方库中删除未使用的资产 [英] removing unused assets from 3rd party library

查看:101
本文介绍了从第三方库中删除未使用的资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过这个问题,minifyEnabled和shrinkResources在某种意义上给出了次优的结果;当我加入时,

I've run into this issue where minifyEnabled and shrinkResources give me sub-optimal results in the sense that; when I have included,

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

我还没有使用Google Sign In API,我收到大量登录图片。

I am not using Google Sign In API's yet, I get loads of sign in images.

我如何获得摆脱我的APK?他们似乎增加了相当多的KB。

How do I get rid of them from my APK? They seem to add a fair bit of KB's.

我正在使用proguard来缩小和缩小资源,如上所述。

I am using proguard with minify and shrink resources as explained above.

推荐答案

有一个详细主题这里在android开发者用户指南。

There is a detail topic here at android developer user guide.

基本上,你有很多选择实际上通过缩小资源来减小apk的大小。我已经对它们进行了一些简短的讨论,我认为下面讨论的启用严格参考检查可以解决您的问题,但是您可以查看所有可用选项以进一步减小您的apk大小。

Basically, you have lot of options to actually reduce the size of your apk by shrinking your resources. 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.



自定义要保留的资源

正如文档所说,在res / raw / keep.xml中使用下面的xml来决定保留什么以及不保留什么:

As the doc says, use the below xml at res/raw/keep.xml, to decide what to keep and what not to keep :

<?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).

启用严格参考检查

如果你的代码或你的库引用了如下资源:

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.

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

So, add following to 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.

删除未使用的替代资源

资源收缩器不会删除替代资源,例如,替代资源用于不同屏幕密度的可绘制资源,用于不同语言的替代字符串资源等等。

Resource Shrinker does not remove alternative resources, like, alternative drawable resources for different screen densities, alternative string resources for different languages, etc.

因此,您可以自己选择要保留的内容,例如,您想要的构建文件保持'en'语言环境中的字符串:

So, you can yourself choose what to keep, from your build file, say, you want to keep on strings in 'en' locale :

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

这可以显着减小尺寸。

如果仍有资源由资源缩减器保留,则使用第一次讨论手动排除它们方法,看看是否编译和构建正确,如果没有,那么资源缩减器保留资源的原因,将从异常抛出变得清晰n建设时。

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.

希望有所帮助!

这篇关于从第三方库中删除未使用的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆