生成的 .apk 包括未使用的资源和未使用的类 [英] Generated .apk includes unused resources and unused classes

查看:19
本文介绍了生成的 .apk 包括未使用的资源和未使用的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 开发一个非常简单的应用程序,实际上它所做的就是在屏幕上显示当前时间和日期.考虑到这一点,我原以为该应用的大小只有几千字节,但事实并非如此.

I am developing a very simple application for Android, which literally all it does is display the current time and date on the screen. With this in mind I was expecting the app to be only a couple of kilobytes in size, but this is not the case.

在生成应用程序的发布"并在 Android Studio 中使用 Build > Analyze APK... 对其进行分析后,我可以看到大量的 .png 和 .xml 资源文件被放入 .apk 文件中,我从未使用或包含其中任何一个(我的项目仅包含 4 个资源文件).这让我很困扰,因为这些自动包含的资源文件只会占用空间.

After generating a "release" of the application, and analyzing it with Build > Analyze APK... in Android Studio, I can see that an extensive amount of .png and .xml resource files are placed into the .apk file, and I never used nor included any of them (my project only contains 4 resource files). This bothers me because these auto-included resource files do nothing but occupy space.

这些是自动包含的文件夹(蓝色),其中包含我正在谈论的文件:

These are the auto-included folders (in blue) which contain the files I'm talking about:

如您所见,这大约是应用大小的 26%.

As you can see, that's about 26% of the app's size.

类似的情况发生在classes.dex"文件中,该文件包含数百(可能是数千)个我从未在代码中引用的类.例如,在查看文件包含的内容后,我发现其中包含HashMap",因此将其包含在内是没有意义的,因为我从未在代码中导入它.

A similar case occurs with the "classes.dex" file which contains hundreds (possibly thousands) of classes that I never reference in my code. For instance, after looking at what the file contains I found "HashMap" in it, which makes no sense for it to be included since I never imported it in my code.

最后,我的问题是:是否有可能以某种方式避免将这些未使用的资源文件和类包含在 .apk 的发布中?

注意:我已经在使用

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

在我的build.gradle"文件中,它确实减少了 .apk 的大小,但是我上面描述的未使用的元素仍然包含在内.

in my "build.gradle" file, which does indeed reduce the size of the .apk, however the unused elements I have described above are still included.

推荐答案

我从未使用或包含其中任何一个

I never used nor included any of them

如果你有依赖项,那些类和资源来自它们.

If you have dependencies, those classes and resources came from them.

是否有可能以某种方式避免将这些未使用的资源文件和类包含在 .apk 的发布中?

Is it possible to somehow avoid these unused resource files and classes from being included in the release of the .apk?

消除您的依赖项.

例如,当您创建项目时,您可能会看到一个预先选中的向后兼容性 (AppCompat)"复选框.离开该检查会添加对 appcompat-v7 的依赖,并设置您的项目以使用它(例如,您的活动扩展了 AppCompatActivity 而不是 Activity).使用 appcompat-v7 有利有弊;缺点之一是它为您的项目增加了相当多的开销.

For example, when you created the project, you may have been presented with a "Backwards Compatibility (AppCompat)" checkbox, pre-checked. Leaving that checked adds a dependency on appcompat-v7 and sets up your project to use it (e.g., your activity extends AppCompatActivity instead of Activity). There are pros and cons for using appcompat-v7; one of the cons is that it adds a fair bit of overhead to your project.

这篇关于生成的 .apk 包括未使用的资源和未使用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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