Translation - Make the Most of APK Analyzer

One of my favorite recent additions to Android Studio is the APK Analyzer , which you can find in the main menu under “Build → APK Analysis” .

image
Useful tip: you can simply drag and drop APK files into the editor to open them.

APK Analyzer allows you to open and check the contents of any APK file that you have on your computer, which can be created from your project in Android Studio or received from the build server or other storage. The APK file does not have to be built ( 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 APKs. If you need to analyze the debug version of your application, make sure that you are using an APK that is not designed for Instant run. To get this APK, build APK Build → Build APK . You can also find out if you opened APK Instant Run by checking the availability of the instant-run.zip file in the archive.

Using the APK Analyzer is a great way to browse the APK file and learn about its structure , check the contents before release, or check out some common problems, such as the size of the APK and problems with the DEX.

Decrease app size with 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 size of the APK on disk. The download size indicates how much data will be used to download 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 saucer with a blue border”, which can be optimized most easily in the size of the APK. Moving to a folder in the APK file, you will see the resources and objects that occupy the most space in the APK.

image
Resources are sorted in descending order by size.

In this example, while studying the APK for a possible reduction in size, 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 as vectors, we found the source files for the illustrations and imported them as VectorDrawables using the new PSD support in the Android Studio 2.2 vector resource import tool .

By going through the same process for the other remaining animation ( instruction_touch _ * .png ) and deleting these PNG files at all densities, we were able to save more than 5 MB. To maintain backward compatibility, we used the VectorDrawableCompat from the support library.

Looking through other resource folders, it was easy to find some uncompressed WAV files that could be converted to OGG, which meant even greater savings without touching a line of code.

Next in the list of things to check was the lib / folder , which contains its own libraries for the three ABIs we support.
It was decided to use the support of APK sections in our Gradle build to create separate versions of the application for each ABI.

image
View other folders in the APK

I quickly looked through AndroidManifest.xml and noticed that the android: extractNativeLibs attribute is missing from the application. Setting this attribute to false allows you to save some space on the device, since it prevents copying your own libraries from the APK to the file system. The only requirement is that these files are page-aligned and stored uncompressed inside the APK, which is supported by the new packer in the Android Gradle plugin version 2.2.0+.

image
Full AndroidManifest.xml when viewed in APK Analyzer

After 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 old and new builds.

image
Comparison of the APK - access to it through the button in the upper right corner

We carefully went through resources and native libraries, saving 17 MB with very small changes in the application. However, I see that our DEX size is regressing, and class2.dex is growing at 400 KB.

Debugging DEX Issues


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 builds, so there is not much of this DEX size. 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.

image
Viewing the contents of classes.dex

When you click on any DEX file, you will see information about how many classes and methods it defines and how many general method references it contains (these are the methods that are taken into account in the 64K restrictionin one DEX file). In this example, a screenshot, the application is about to reach its limit, which means that in the near future it will need MultiDex to separate classes into separate files.

You can browse the contents of packages to see which ones 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.

image
Number of package links

After you turn on MultiDex and compile the application, you will see the second classes2.dex file (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 to the main classes.dex file, but in the rare case when it does not work and you get a ClassNotFoundException, you can use the APK Analyzer to check the DEX files and then force the missing classes to be added to the primary DEX file .

You will encounter similar issues when you enable Proguard and use classes or methods by mirroring or from XML layouts. The APK Analyzer can help verify that the Proguard configuration is correct, allowing you to easily check if the methods and classes are in the APK and if they are renamed (if obfuscated). 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 functions you would like to see integrated into this tool!

Also popular now: