Android Studio includes an APK Analyzer that provides immediate insight into the composition of your APK after the build process is complete. Using the APK Analyzer can reduce time you spend debugging issues with DEX files and resources within your app as well as help with reducing your APK size. With the Android Studio APK Analyzer, you can accomplish the following:
- View absolute and relative size of files in the APK (such as DEX and Android resource files).
- Understand the composition of DEX files.
- Quickly view final versions of files in the APK (such as the
AndroidManifest.xml). - Perform a side-by-side comparison of two APKs.
There are three ways to access the APK Analyzer when a project is open:
- Drag an APK into the Editor window of Android Studio.
- Switch to the Project perspective in the Project window and then
double-click the APK in the default
build/output/apks/directory. - Select Build > Analyze APK in the menu bar and then select your APK.
Important: When analyzing debug builds, use the APK created by selecting
Build > Build APK or from the gradle commands. Clicking Run in
the toolbar will result in Instant
Run enabled APKs, which shouldn't be used with the APK Analyzer
for optimization tasks—they
are for development use only and load most resources dynamically. Instant Run
APKs can be identified by an instant-run.zip file embedded within
the APK.
View file and size information
APKs are files that follow the ZIP file format. The APK Analyzer displays each file or folder as an entity with expansion functionality available to navigate into folders. The hierarchy of the entities mirrors the structure of the files and folders in the APK file.
APK Analyzer shows raw file size and download file size values for each entity, as shown in figure 1. Raw File Size represents the unzipped size of the entity on disk while Download Size represents the estimated compressed size of the entity as it would be delivered by Google Play. The % of Total Download Size indicates the percentage of the APKs total download size the entity represents.
Figure 1. File sizes in the APK Analyzer
View the AndroidManifest.xml
If your project includes multiple AndroidManifest.xml files (such as for
product flavors) or includes libraries that also provide a manifest file, they
will be merged into a single file in your APK. This manifest file
is normally a binary file within the APK, but when
selected in the APK Analyzer, the XML form of this entity is reconstructed and
presented. This viewer allows you to understand any changes that may have been
made to your app during the build. For example, you can see how the
AndroidManifest.xml file from a library your application depends on was merged
into the final AndroidManifest.xml file.
Additionally, this viewer provides some lint capabilities, and warnings or errors appear in the top-right corner. Figure 5 shows an error being reported for the selected manifest file.
Figure 5. An error appears for the selected manifest file with an icon in the right margin
View code and resource entities
Various build tasks change the final entities in an APK file. For example, Proguard shrinking rules files can alter your final code and image resources might be overriden by resources in a product flavor. Viewing the final version of your files is easy with the APK Analyzer: Click the entity and a context sensitive file view displays just below the file listing, including a preview for text and image entities.
Figure 2. A preview of the final image resource
The APK Analyzer can also display various text and binary files.
For instance, the resources.arsc entity viewer allows you to see the
configuration-specific values such as specific language translations for a
string resource. In figure 3, you can see the translations for each string
resource.
Figure 3. A preview of translated string resources
View DEX files
APK Analyzer's DEX file viewer gives you immediate access to the underlying information in the DEX file(s) in your app. Class, package, total reference and declaration counts are provided within the viewer, which can assist in deciding whether to use multi-DEX or how to remove dependencies to get below the 64k DEX limit.
Figure 4 depicts a medium-size app approaching the 64k DEX limit. Each package, class, and method includes counts listed in the Defined Method and Referenced Method columns. The Referenced Methods column counts all methods that are referenced by the DEX file. This typically includes methods defined in your code, dependency libraries, and methods defined in standard Java and Android packages that the code uses—these are the methods counted toward the 64k method limit in each DEX file. The Defined Methods column counts only the methods that are defined in one of your DEX files, so this number is a subset of Referenced Methods. Note that when you package a dependency in your APK, the methods defined in the dependency add to both method counts. Also note that minification and/or Proguard shrinking can also considerably change the contents of a DEX file after source code is compiled.
Figure 4. An app that uses multi-dex in APK Analyzer
Compare APK files
The APK Analyzer can also compare the size of the entities in two different APK files. This is helpful when trying to understand why your app increased in size, as compared to a previous release.
Before publishing an updated APK, load the version of the APK you are about to publish into the APK Analyzer. Then click Compare With in the top- right corner of the APK Analyzer window. In the selection dialog, find the APK that was last published to your users and click OK. A dialog similar to the one in figure 6 appears, allowing you to compare the impact the update will have on users.
Figure 6 shows the difference between a particular app's debug and release builds. Different build options are in use between these build types, which alter the underlying entities differently.
Figure 6. The difference between a debug and release APK