Analyze your build with APK Analyzer

    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.
    • 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 -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.

    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.

    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 - 图1

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

    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 and can each also considerablychange the contents of a DEX file after source code is compiled.

    Figure 3. A medium-sized app

    Filter the DEX file tree view

    Analyze your build with APK Analyzer - 图2

    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 fieldsto show or hide the class fields.
    • Toggle Show all referenced methods or fieldsAnalyze your build with APK Analyzer - 图3to 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.

    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 (), 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.

    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.
    • : Enables Deobfuscate namesAnalyze your build with APK Analyzer - 图4 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 nodes 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 .

    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 - 图5

    Figure 6. DEX byte code for init method

    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 - 图6

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

    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 a.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.

    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 - 图7

    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.