如何从我已经包含在Android的第三方库中删除未使用的资源?从我、第三方、库中、资源

2023-09-04 07:46:42 作者:几许欢颜こ指尖飞舞

这是我链接到我的应用程序的第三方库通常包括未使用我的应用程序资源文件,正因为如此,最终腹胀我的APK。

例如,其中包括谷歌Play业务库,而不是使用登录按钮的功能;所有这些图像和布局资源,最终在我的最终版本。

由于这些资源都包含在编译的库,如何从我的版本中删除?

解决方案   Play商店已开始建议删除未使用的应用 Android

这答案总结从删除未使用的资源这也解释了如何使用minifyEnabled和shrinkResources,这在的官方文档页面。

这对第三方库,包括您的应用程序codePATH不使用资产的一个共同的问题,这是非常重要的,以便生产出更小的APK文件,为您的用户删除这些资产。值得庆幸的是,摇篮和Android Studio的最新版本提供了一个解决方案,以帮助。

通过设置在摇篮配置的 minifyEnabled 和 shrinkResources 以真,系统将出去,从您的应用程序删除未使用的资源。

 安卓{
    ...

    buildTypes {
        推出 {
            minifyEnabled真
            shrinkResources真
            proguardFiles getDefaultProguardFile('ProGuard的-android.txt),
                         proguard-rules.pro
        }
    }
}
 

重要的是要注意,删除未使用的资源,需要在 minifyEnabled 标志被设置。该标志(如上文删除未使用的code )将触发ProGuard的删除未使用您的应用程序code路径。这是从包括库中删除资源的重要一步。如果code路径不会被删除,那么编译器会仍然认为,资源是由现有的codePATH引用,将无法正常删除。

值得一提的是,这是一个pretty的广泛的系统。例如,它会通过看你的code特定字符串常量,以及各种资源/原材料资源寻找的文件形式的任何网址:/// ... 继续。它甚至会走这么远来分析CSS,HTML和JavaScript的文件。

现在,可以存在这里的假阳性或假阴性的实例。资产可能会得到削减,或存放,当你想相反的行为。 (公平地说,资源萎缩往往是overegar ...)要调整,你可以添加工具:让和工具:丢弃属性来定义所需的行为,通过在 RES /生/ keep.xml 文件约定。

 <资源的xmlns:工具=htt​​p://schemas.android.com/tool​​s
     工具:保留=@布局/ l_used * _c,@布局/ l_used_b *
     工具:丢弃=@布局/ unused2
/>
 

The third-party libraries that I link into my app often include resource files that aren’t being used by my application, and as such, end up bloating my APK.

For example, including the Google Play services library, but not using the login button functionality; all those image and layout resources end up in my final build.

Since these resources are included in a compiled library, how can I remove them from my build?

解决方案

This answer is summarized from Removing Unused Resources which explains how to use minifyEnabled and shrinkResources, which are covered in more depth at the Official document page.

It’s a common problem for third-party libraries to include assets that your application codepath does not use, and it’s critically important to remove those assets in order to produce smaller APK files for your users. Thankfully, the latest version of Gradle and Android Studio provides a solution to help.

By setting minifyEnabled and shrinkResources to true in your gradle configuration, the system will go forth removing unused resources from your application.

android {
    ...

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

It is important to note that removing unused resources requires the minifyEnabled flag to be set. This flag (as mentioned in Removing unused code) will trigger proguard to remove code paths that aren’t being used by your application. This is an important step in the removal of resources from included libraries. If the code paths aren’t removed, then the compiler will still believe the resources are referenced by an existing codepath and won’t remove them properly.

It's worth noting that this is a pretty extensive system. For instance, it will look through specific string constants in your code, as well as various res/raw resources looking for any URLs in the form of file:///…. to keep. It will even go so far as to analyze CSS, HTML, and JavaScript files as well.

Now, there may be instances here of false positives or false negatives. Assets might be getting cut, or kept, when you want the opposite behavior. (To be fair, resource shrinking tends to be overegar...) To adjust this, you can add the tools:keep and tools:discard attributes to define the desired behavior, by convention in a res/raw/keep.xml file.

<resources xmlns:tools="http://schemas.android.com/tools"
     tools:keep= "@layout/l_used*_c,  @layout/l_used_b*"
     tools:discard="@layout/unused2"
/>