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

时间:2021-02-01 00:32:56

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.

我链接到我的应用程序的第三方库通常包括我的应用程序未使用的资源文件,因此,最终导致我的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.

例如,包括Google Play服务库,但不使用登录按钮功能;所有这些图像和布局资源最终都在我的最终版本中。

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

由于这些资源包含在已编译的库中,如何从构建中删除它们?

2 个解决方案

#1


69  

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.

这个答案总结自Remove Unused Resources,它解释了如何使用minifyEnabled和shrinkResources,这些内容在官方文档页面中有更深入的介绍。

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.

第三方库包含应用程序代码路径不使用的资产是一个常见问题,删除这些资产以便为用户生成较小的APK文件至关重要。值得庆幸的是,最新版本的Gradle和Android Studio提供了一个解决方案来提供帮助。

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

通过在Gradle配置中将minifyEnabled和shrinkResources设置为true,系统将从应用程序中删除未使用的资源。

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.

请务必注意,删除未使用的资源需要设置minifyEnabled标志。此标志(如删除未使用的代码中所述)将触发ProGuard删除应用程序未使用的代码路径。这是从包含的库中删除资源的重要步骤。如果未删除代码路径,则编译器仍会认为资源是由现有代码路径引用的,并且不会正确删除它们。

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.

值得注意的是,这是一个非常广泛的系统。例如,它将查看代码中的特定字符串常量,以及以file:/// ...形式查找任何URL的各种res / raw资源。保持。它甚至可以分析CSS,HTML和JavaScript文件。

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

现在,这里可能存在误报或漏报。当您想要相反的行为时,资产可能会被削减或保留。 (公平地说,资源缩减往往过于紧张......)为了调整这一点,您可以添加工具:keep和tools:discard属性以按照res / raw / keep.xml文件中的约定来定义所需的行为。

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

#2


8  

If you can't use shrinkResources for some reasons, at the least we can use resConfig to remove languages we don't support.

如果由于某些原因无法使用shrinkResources,至少我们可以使用resConfig删除我们不支持的语言。

defaultConfig {
    resConfig "en"     
}

#1


69  

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.

这个答案总结自Remove Unused Resources,它解释了如何使用minifyEnabled和shrinkResources,这些内容在官方文档页面中有更深入的介绍。

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.

第三方库包含应用程序代码路径不使用的资产是一个常见问题,删除这些资产以便为用户生成较小的APK文件至关重要。值得庆幸的是,最新版本的Gradle和Android Studio提供了一个解决方案来提供帮助。

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

通过在Gradle配置中将minifyEnabled和shrinkResources设置为true,系统将从应用程序中删除未使用的资源。

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.

请务必注意,删除未使用的资源需要设置minifyEnabled标志。此标志(如删除未使用的代码中所述)将触发ProGuard删除应用程序未使用的代码路径。这是从包含的库中删除资源的重要步骤。如果未删除代码路径,则编译器仍会认为资源是由现有代码路径引用的,并且不会正确删除它们。

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.

值得注意的是,这是一个非常广泛的系统。例如,它将查看代码中的特定字符串常量,以及以file:/// ...形式查找任何URL的各种res / raw资源。保持。它甚至可以分析CSS,HTML和JavaScript文件。

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

现在,这里可能存在误报或漏报。当您想要相反的行为时,资产可能会被削减或保留。 (公平地说,资源缩减往往过于紧张......)为了调整这一点,您可以添加工具:keep和tools:discard属性以按照res / raw / keep.xml文件中的约定来定义所需的行为。

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

#2


8  

If you can't use shrinkResources for some reasons, at the least we can use resConfig to remove languages we don't support.

如果由于某些原因无法使用shrinkResources,至少我们可以使用resConfig删除我们不支持的语言。

defaultConfig {
    resConfig "en"     
}