直接前往內容

最常造訪的網頁

最近瀏覽的網頁

navigation

Analyze Your Build with APK Analyzer

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

With the APK Analyzer, you can accomplish the following:

There are three ways to access the APK Analyzer when a project is open:

Important: When analyzing debug builds, use the APK created by selecting Build > Build APK or from the gradle commands. Clicking Run in the toolbar results in Instant Run-enabled APKs, which shouldn't be used with the APK Analyzer for optimization tasks because they are for development use only and load most resources dynamically. You can identify an Instant Run APK by the presence of 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 APK's 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 are 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 might 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 2 shows an error being reported for the selected manifest file.

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

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 multidex or how to remove dependencies 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 Method and Referenced Methods 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 3. A medium-sized app

Filter the DEX file tree view

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

Figure 4. DEX filters set to display fields and methods for BuildConfig

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

  1. In the File list, select the classes.dex file.
  2. In the Class list, navigate to and select a class.
  3. Expand the class you selected.
  4. Toggle Show fields to show or hide the class fields.
  5. Toggle Show methods to show or hide the class methods.
  6. Toggle Show all referenced methods or fields to show or hide referenced packages, classes, methods, and fields. In the tree view, italicized nodes are references that do not have a definition in the selected DEX file.

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

Load Proguard mappings

Next to the filtering icons are the Proguard mapping icons. They are grayed out until you load a set of Proguard mapping files that add functionality to the DEX viewer, such as deobfuscating names (mapping.txt), showing nodes that were removed (usage.txt), and indicating nodes that 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:

  1. Click Load Proguard Mappings.
  2. Navigate to the project folder that contains the mapping files and load all of the files, any combination of the files, or the folder that contains the files.

    The mapping files are normally in project/app/build/outputs/mappings/release/. The file picker defaults to the release folder if it detects this project structure. First, the file picker checks for filenames that exactly match mapping.txt, seeds.txt, and usage.txt. Next, the file picker checks for filenames that contain 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:

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 following options that let you see the bytecode, find usages, and display a dialog that shows Proguard rules that you can copy and paste for the selected node. Right-click any node in the Class list view to display its context menu.

Show bytecode: Decompiles the selected class, method, or field and displays the smali (not Java code) bytecode representation in a dialog, as follows:

Figure 6. DEX byte code for init method

Find usages: Shows which other parts of the DEX code have 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 them from being removed during shrinking:

Figure 7. References to MyClass

Generate Proguard Keep rule: Shows Proguard rules that you can copy and paste 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, see Customize which code to keep.

Figure 8. Proguard rules that you can copy from the 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 resources can be overridden by resources in a product flavor. Viewing the final version of your files is easy with the APK Analyzer: Click the entity and a preview for the text or image entity appears below, as shown in 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 the configuration-specific values such as language translations for a string resource. In figure 10, you can see the translations for each string resource.

Figure 10. A preview of translated string resources

Compare APK files

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

  1. Load the version of the APK you are about to publish into the APK Analyzer.
  2. In the top-right corner of APK Analyzer, click Compare With.
  3. 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 11 appears to help you assess the impact the update might have on users.

Figure 11 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 11. The difference between a debug and release APK

本網站使用 Cookie 儲存你在此指定的語言和顯示選項偏好設定。

掌握有關 Android 開發人員的最新消息和實用訣竅,協助你製作最受歡迎的 Google Play 內容。

* 必填欄位

訂閱成功!

在 WeChat 上追蹤 Google Developers

你要以瀏覽這個網站嗎?

你要求以顯示這個網頁,但你為此網站指定的語言偏好設定為

是否要變更語言偏好設定並改用瀏覽網站?稍後如要變更語言偏好設定,請利用位在每個網頁最下方的語言選單來調整設定。

你的 API 層級必須達 以上才能存取這個級別

本說明文件已隱藏,因為你為該文件選取的 API 層級為 。使用左側導覽列上方的選取工具即可變更說明文件的 API 層級。

如需進一步瞭解如何為應用程式指定 API 層級的相關資訊,請參閱 Supporting Different Platform Versions (支援不同的平台版本) (英文)。

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)