One of my favorite recent additions to Android Studio is
APK Analyzer , which you can find in the main menu in the
Build → Analyze APK section .
Useful tip: you can simply drag and drop the APK files into the editor to open themAPK Analyzer allows you to open and check the contents of any APK file that you have on a computer that can be created from your project in Android Studio, or obtained from the build server or another storage. It is not necessary to
build the APK file (
Build → Build APK ) before this, and you do not need the source code for this APK.
')
Note APK Analyzer works best with release versions of APK. If you need to analyze the debug version of your application, make sure that you use an APK that is not intended for Instant run. To get this APK, build APK Build → Build APK . And you can also find out if you opened APK Instant Run by checking the archive file instant-run.zip.
Using APK Analyzer is a great way to review an APK file and learn about
its structure , check the contents before you release it, or check out some common problems, such as APK size and DEX problems.
Reducing the size of the application using APK Analyzer
APK Analyzer can provide you with a lot of interesting and useful information about the size of the application. At the top of the screen you can see the
size of the raw file - this is the APK size on the disk.
The download size shows how much data will be used to load your application, taking into account the compression that the Play Store does.
The list of files and folders is sorted by their size in descending order. Thanks to this, you are immediately shown as “on a blue saucer”, which can be most easily optimized in the size of an APK. Moving to any folder in the APK file, you will see the resources and objects that occupy the most space in the APK.
Resources are sorted in descending order of size.In this example, while studying the APK for possible size reduction, I was immediately able to notice that the 3-frame PNG animation is the biggest thing in resources, weighing 1.5 MB, and this is for
xxhdpi density!
Since these images look like ideal candidates for storage in the form of vectors, we found the source files for the illustrations and imported them as VectorDrawables, using the
new PSD support in the import tool of vector resources Android Studio 2.2 .
By going through the same process for the other remaining animation (
instruction_touch _ * .png ) and deleting these PNG files in all densities, we were able to save more than 5 MB. To maintain backward compatibility, we used
VectorDrawableCompat from the support library.
Looking through other resource folders, it was easy to detect some uncompressed WAV files that could be converted to OGG, which meant even more savings without touching the line of code.
Next in the list of things that need to be checked was the
lib / folder, which contains our own libraries for the three ABIs we support.
It was decided to use the
support of the APK sections in our Gradle assembly to create separate versions of the application for each ABI.
View other folders in APKI quickly looked at
AndroidManifest.xml and noticed that the
application lacks the
android: extractNativeLibs attribute . Setting this attribute to false allows you to save some space on the device, as it prevents copying your own libraries from the APK to the file system. The only requirement is that these files are paged and uncompressed inside the APK, which are supported by the
new packer in the Android Gradle plugin version 2.2.0+.
Full AndroidManifest.xml when viewed in APK AnalyzerAfter making these changes, I was curious to compare the new version of the application with the previous one. To do this, I checked the source from git commit from which I started, compiled the APK and saved it in another folder. Then I used the
“Compare with ...” function to see the difference in size between the old and new builds.
Comparison APK - access it through the button in the upper right cornerWe walked through resources and native libraries well, saving 17 MB with very few changes in the application. However, I see that our DEX size regresses, and class2.dex grows by 400 KB.
Debugging DEX Problems
In this case, the difference was due to updating our dependencies to newer versions and adding new libraries.
Proguard and
Multidex have already been included for our assemblies, so this DEX size is not so much. However, the APK Analyzer is a great tool for debugging any problems with this setting, especially when you first turn on Multidex or Proguard for your project.
View the contents of classes.dexWhen you click on any DEX file, you will see information about how many classes and methods it defines, and how many general references to methods it contains (these are the methods that are taken into account in the
64K limit in one DEX file). In this example, a screenshot, the application is about to reach the limit, which means that in the near future it will need MultiDex to separate the classes into separate files.
You can view the contents of the packages to find out which of them use all the links. In this example, we see that the main reasons for the bloated DEX file are the support library and Google Play services.
Number of links to the packageAfter you have enabled MultiDex and compiled the application, you will see the second file, classes2.dex (and, possibly, classes3.dex, etc.). The MultiDex solution in the Android-Gradle plugin determines which classes are needed to run your application and places them in the main classes.dex file, but in the rare case when it does not work, and you get a ClassNotFoundException exception, you can use APK Analyzer to check DEX files and then
force the missing classes to the primary DEX file .
You will encounter similar problems when you enable Proguard and use classes or methods by reflecting or from XML layouts. The APK Analyzer can help check the correctness of the Proguard configuration, allowing you to easily check if there are any methods and classes in the APK, and whether they are renamed (during obfuscation). You can also make sure that the classes you want to delete are actually deleted and are not counted in the method counter.
We would be interested to know what other applications you will find for APK Analyzer and what other features you would like to see integrated into this tool!