The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Addressing Application Size Issues

Sometimes, our apps are just too big, where “too big” can be defined as:

This chapter will review various techniques for trying to keep the size of your app down to a reasonable level.

Prerequisites

This chapter assumes that you have read the core chapters of the book.

The APK Analyzer

One way to reduce the size of your APK is to see what is inside of it that you can get rid of. This is also useful to see if your attempts to get rid of it actually work and reduce the size of the APK file.

Android Studio, starting with version 2.2, has an APK analyzer that will help you do this.

To analyze an APK, choose Build > Analyze APK… from the Android Studio main menu. Unfortunately, this is not aware of your actual APKs from your project, instead dumping you in a standard file-open dialog. You will need to rummage around your development machine to try to track down the APK to analyze. Look in your module’s build/outputs/apk/ directory, though you may need to specifically build the APK for the build variant that you want to look at.

The analyzer will then generate a tree-table, showing you what things in your app are consuming space:

APK Analyzer, As Initially Launched
Figure 1057: APK Analyzer, As Initially Launched

APK Analyzer, Drilling Down Into Resources
Figure 1058: APK Analyzer, Drilling Down Into Resources

If you click on a DEX file, such as classes.dex, a bottom panel displays details of the classes inside the file:

APK Analyzer, Drilling Down Into DEX
Figure 1059: APK Analyzer, Drilling Down Into DEX

The “Defined Methods” column indicates how many methods are defined by the classes in the packages, where those classes are in the DEX file. “Referenced Methods” refers to how many methods that classes in the DEX file reference from the indicated packages, and that ties into what’s known as the “64K DEX method reference limit”, which we will get into a bit later in this chapter.

If you have multiple builds of your APK — such as a debug and a release build, or an old and a new build — you can open one in the APK Analyzer, then click the “Compare with…” button and choose the second APK. This opens up a floating window showing you the size differences between the two APKs:

APK Analyzer, Comparing Two APKs
Figure 1060: APK Analyzer, Comparing Two APKs

Java Code, and the 64K Method Limit

The preview of this section was eaten by a grue.

Native Code

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Images

The preview of this section was traded for a bag of magic beans.

APK Expansion Files

The preview of this section is in the process of being translated from its native Klingon.