问题
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:

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.
回答1:
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.
来源:https://stackoverflow.com/questions/49368755/generated-apk-includes-unused-resources-and-unused-classes