Analyze your build with APK Analyzer

Android Studio includes an APK Analyzer that provides immediate insight into thecomposition of your APK after the build process completes. Using the APKAnalyzer can reduce the time you spend debugging issues with DEX files andresources within your app, and help reduce your APK size. It's also availablefrom the command line withapkanalyzer.

With the APK Analyzer, you can accomplish the following:

  • View the absolute and relative size of files in the APK, such as the DEX andAndroid resource files.
  • Understand the composition of DEX files.
  • Quickly view the final versions of files in the APK, such as theAndroidManifest.xml file.
  • 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 thendouble-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 selectingBuild > Build APK or from the gradle commands. Clicking Run inthe toolbar results in InstantRun-enabled APKs, which shouldn't be used with the APK Analyzerfor optimization tasks because theyare for development use only and load most resources dynamically. You can identifyan Instant Run APK by the presence of an instant-run.zip fileembedded within the APK.

View file and size information

APKs are files that follow the ZIP file format. The APK Analyzer displays eachfile or folder as an entity with expansion functionality available to navigateinto folders. The hierarchy of the entities mirrors the structure of the filesand 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 theentity on disk while Download Size represents the estimated compressed sizeof the entity as it would be delivered by Google Play. The % of Total DownloadSize indicates the percentage of the APK's total download size the entityrepresents.

Analyze your build with APK Analyzer - 图1

Figure 1. File sizes in the APK Analyzer

View the AndroidManifest.xml

If your project includes multiple AndroidManifest.xml files (such as forproduct flavors) or includes libraries that also provide a manifest file, theyare merged into a single file in your APK. This manifest fileis normally a binary file within the APK, but whenselected in the APK Analyzer, the XML form of this entity is reconstructed andpresented. This viewer allows you to understand any changes that might have beenmade to your app during the build. For example, you can see how theAndroidManifest.xml file from a library your application depends on was mergedinto the final AndroidManifest.xml file.

Additionally, this viewer provides some lint capabilities, and warnings orerrors appear in the top-right corner. Figure 2 shows an error being reportedfor the selected manifest file.

Analyze your build with APK Analyzer - 图2

Figure 2. An error icon appears in the right marginfor the selected manifest file

View DEX files

APK Analyzer's DEX file viewer gives you immediate access to the underlyinginformation in the DEX file(s) in your app. Class, package, total reference, anddeclaration counts are provided within the viewer, which can assist in decidingwhether to use multidex or how to removedependencies to get below the 64K DEX limit.

Figure 3 depicts a medium-size app that is below the 64k DEX limit. Each package,class, and method inside the DEX file has counts listed in the Defined Methodand Referenced Methods columns. The Referenced Methods column counts allmethods that are referenced by the DEX file. This typically includes methodsdefined in your code, dependency libraries, and methods defined in standard Javaand Android packages that the code uses—these are the methods counted towardthe 64k method limit in each DEX file. The Defined Methods column countsonly the methods that are defined in one of your DEX files, so this number is asubset of Referenced Methods. Note that when you package a dependency inyour APK, the methods defined in the dependency add to both method counts. Alsonote that minification andProguard shrinking can each also considerablychange the contents of a DEX file after source code is compiled.

Analyze your build with APK Analyzer - 图3

Figure 3. A medium-sized app

Filter the DEX file tree view

Just above the Class list, APK Analyzer provides filters forviewing the contents of the selected DEX file.

Analyze your build with APK Analyzer - 图4

Figure 4. DEX filters set to display fields andmethods for BuildConfig

To use the filters to display all methods and fields inside a class,do the following:

  • In the File list, select the classes.dex file.
  • In the Class list, navigate to and select a class.
  • Expand the class you selected.
  • Toggle Show fieldsAnalyze your build with APK Analyzer - 图5to show or hide the class fields.
  • Toggle Show methodsAnalyze your build with APK Analyzer - 图6to show or hide the class methods.
  • Toggle Show all referenced methods or fieldsAnalyze your build with APK Analyzer - 图7to show or hide referenced packages, classes, methods, and fields. In thetree view, italicized nodes are references that do not have a definition inthe selected DEX file.

A DEX file can reference methods and fields that are defined in a differenta file. For example System.out.println() is a referenceto the println() method in the Android framework.

Load Proguard mappings

Next to the filtering icons are theProguard mapping icons. They aregrayed out until you load a set of Proguard mapping files that add functionalityto the DEX viewer, such as deobfuscating names (mapping.txt),showing nodes that were removed (usage.txt), and indicating nodesthat cannot be removed (seeds.txt).The Proguard mapping files apply to APKs that were built with Proguard enabled,and must come from the same build that produced the APK.

Analyze your build with APK Analyzer - 图8

Figure 5. Load Proguard mappings

To load the Proguard mapping files, do the following:

  • Click Load Proguard Mappings.
  • Navigate to the project folder that contains the mapping files and load allof the files, any combination of the files, or the folder that contains thefiles.

The mapping files are normally inproject/app/build/outputs/mappings/release/.The file picker defaults to the release folder if it detects thisproject structure. First, the file picker checks for filenames that exactlymatch mapping.txt,seeds.txt, and usage.txt. Next, the file picker checks for filenames thatcontain the text mapping, usage, or seeds somewhere and end with .txt.For example release-seeds-1.10.15.txt is a match.

The following list describes the mapping files:

  • seeds.txt: Nodes that the Proguard configuration prevents from being removedduring shrinking are shown in bold.
  • mapping.txt: Enables Deobfuscate namesAnalyze your build with APK Analyzer - 图9 so you can restore the original names of nodes thatwere obfuscated by Proguard. For example, you can restore obfuscated nodenames like a, b, c to MyClass, MainActivity, and myMethod().
  • usage.txt: Enables Show removed nodesAnalyze your build with APK Analyzer - 图10 so you can show classes, methods, and fields that wereremoved by Proguard during shrinking. The restored nodes are shown instrikethrough.

For more information about using Proguard to obfuscate and minimize your code,see Shrink your code and resources.

Show bytecode, find usages, and generate Keep rule

The nodes in the Class list view have a context menu with the followingoptions thatlet you see the bytecode, find usages, and display a dialog that shows Proguardrules that you can copy and paste for the selected node. Right-click anynode in the Class list view to display its context menu.

Show bytecode: Decompiles the selected class, method, or fieldand displays the smali (not Java code) bytecode representation in a dialog, asfollows:

Analyze your build with APK Analyzer - 图11

Figure 6. DEX byte code for init method

Find usages: Shows which other parts of the DEX codehave references to the selected class or method (figure 7).If you have seeds.txt loaded,nodes displayed in bold indicate that the Proguard configuration prevents themfrom being removed during shrinking:

Analyze your build with APK Analyzer - 图12

Figure 7. References to MyClass

Generate Proguard Keep rule: Shows Proguard rules that you can copy andpaste into your project Proguard configuration file to keep a given package,class, method, or field from being removed during the Proguard shrinking phase(figure 8).For more information, seeCustomize which code to keep.

Analyze your build with APK Analyzer - 图13

Figure 8. Proguard rules that you can copy fromthe dialog into your Proguard configuration file

View code and resource entities

Various build tasks change the final entities in an APK file. For example,Proguard shrinking rules can alter your final code, and image resourcescan be overridden by resources in aproduct flavor.Viewing the final version of your files is easy with the APK Analyzer: Clickthe entity and a preview for the text or image entity appears below, as shownin figure 9.

Analyze your build with APK Analyzer - 图14

Figure 9. 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 theconfiguration-specific values such as language translations for astring resource. In figure 10, you can see the translations for each stringresource.

Analyze your build with APK Analyzer - 图15

Figure 10. A preview of translated string resources

Compare APK files

The APK Analyzer can compare the size of the entities in two different APKfiles. This is helpful when you need to understand why your app increased insize compared to a previous release. Before you publish an updated APK, do thefollowing:

  • Load the version of the APK you are about to publish into the APK Analyzer.
  • In the top-right corner of APK Analyzer, click Compare With.
  • In the selection dialog, find the APK that was last published to your usersand click OK.

A dialog similar to the one in figure 11 appears to help you assess theimpact the update might have on users.

Figure 11 shows the difference between a particular app's debug and releasebuilds. Different build options are in use between these build types, whichalter the underlying entities differently.

Analyze your build with APK Analyzer - 图16

Figure 11. The difference between a debugand release APK