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

本文是小编为大家收集整理的关于生成的.apk包括未使用的资源和未使用的类的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

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

 screenshot

您可以看到,大约是应用程序大小的26%.

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

因此,最后,我的问题是:是否可以避免使用这些未使用的资源文件和类.

注意:我已经在使用

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

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

推荐答案

我从未使用过,也不包括其中的任何一个

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

有可能以某种方式避免使用这些未使用的资源文件和类.

消除您的依赖.

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

本文地址:https://www.itbaoku.cn/post/1937981.html

问题描述

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.

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:

screenshot

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

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.

So finally, my question is: Is it possible to somehow avoid these unused resource files and classes from being included in the release of the .apk?

NOTE: I am already using

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

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.

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

Eliminate your dependencies.

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.