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

查看:83
本文介绍了生成的.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

如果您有dependencies,则这些类和资源来自它们.

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天全站免登陆