Life is hard work, no gain.

What is an APK analyzer?

Android Studio includes an APK analyzer that lets you see what APK consists of immediately after the build process is complete. Using an APK profiler can reduce the time spent debugging your application’s DEX files and resource-related issues and help reduce the size of the APK.

What can APK profilers do?

  • Look at the absolute and relative sizes of files in APK, such as DEX and Android resource files.
  • Understand the composition of DEX files.
  • Take a quick look at the final version of files in APK, such as the Androidmanifest.xml file.
  • The two APKs are compared side by side.

Field experience

(1) Access APK analyzer

  • Drag APK into the Editor window of Android Studio.
  • Switch to the Project perspective in the Project window, and then double-click the corresponding APK in the default build/ Output /apks/ directory.
  • From the menu bar, select Build > Analyze APK, then select your APK.

(2) View file information

APK is a file that follows the ZIP file format

  • Raw File Size: Indicates the uncompressed Size of the entity on the disk
  • Download Size: represents an estimate of the compressed Size of the entity provided by Google Play
  • % of Total Download Size: indicates the percentage of entities in the Total Download Size of APK

(3) View the manifest file

  • In APK, multiple manifest files are eventually merged into one
  • The manifest file is usually a binary file in APK, but is displayed as XML in AN APK parser
  • The viewer also provides some Lint functionality and displays warnings or errors in the upper right corner

(4) View the DEX file

Classes, packages, total references, and declaration counts are provided in the viewer

  • Defined Methods: Only the Methods Defined in a DEX file are counted, so this number is a subset of Referenced Methods
  • Referenced Methods: All Methods Referenced in the DEX file are counted. This typically includes methods defined in your code and dependency libraries, as well as in the standard Java and Android packages your code uses – methods that count toward the 64K method limit in each DEX file
  • Size: indicates the corresponding file Size

(5) Filter DEX files

Filter the DEX file tree view (see the DEX file figure above). Directly above the Class list, the APK analyzer provides a filter for viewing the contents of the selected DEX file. To use filters to display all methods and fields in a class, do the following:

  1. inFileIn the list, selectclasses.dexFile.
  2. In the Class list, navigate to a Class and select it.
  3. Expand the class of your choice.
  4. Toggle the Show Fields icon to Show or hide class fields.
  5. Toggle the Show Methods icon to Show or hide class methods.
  6. Toggle the Show All referenced Methods or Fields icon to Show or hide referenced packages, classes, methods, and fields. In the tree view, the italic node is a reference that is not defined in the selected DEX file.

DEX files can reference methods and fields defined in other files. For example, system.out.println () is a reference to the println() method in the Android framework.

(6) Load ProGuard mapping

Load the ProGuard map (refer to the DEX file diagram above). Next to the filter icon is the ProGuard mapping icon. They are gray-on until you load a set of ProGuard mapping files to add features to the DEX viewer such as de-obfuming names (mapping.txt), showing removed nodes (usage.txt), and indicating nodes that cannot be removed (seeds. TXT). The build version of the ProGuard mapping file you import must be the same as the build version of APK generated with code reduction enabled.

To load the ProGuard mapping file, do the following:

  1. Click Load Proguard Mappings.
  2. Navigate to the project folder containing the mapping files, and then load all files, any combination of files, or folders containing files.
  3. The mapping file is usually locatedproject/app/build/outputs/mappings/release/. If the file selector detects this project structure, it is selected by defaultreleaseFolder. The file selector checks withmapping.txt,seeds.txt 和 usage.txtExactly the same file name.
  4. The following list describes the mapping files:
  • seeds.txtThe ProGuard configuration prevents nodes removed during compression from being displayed in bold.
  • mapping.txtOpening:Deobfuscate names(Click the icon) to restore the original name of the node obfuscated by R8. For example, you can puta,b,cThe obfuscated node name is restored toMyClass,MainActivity 和 myMethod().
  • usage.txtOpening:Show removed nodes(Click the icon) to display the classes, methods, and fields that R8 removed during compression. The recovered node has a deletion line.

How do you feel? The mapping file is like a key that locates the details of compiler optimization.

(7) Usage and rules

Display bytecode, find usage, and generate the Keep rule. Right-click on any node in the Class list view to display its context menu. Nodes in the Class list view have a context menu.

(7.1) Show bytecode

Decompile the selected class, method, or field and display the SMALI bytecode (rather than Java code) representation in a dialog box.

(7.2) Find Left

Shows which other parts of the DEX code refer to the selected class or method. If seeds.txt is loaded, the nodes in bold indicate that the Proguard configuration prevents removal of these nodes during compression.

(7.3) Generate Proguard Keep rule

Shows Proguard rules that you can copy and paste into the project Proguard configuration file to prevent the removal of a given package, class, method, or field during the code reduction phase.

How do you feel? Worried that you can’t write confusing documents? Someone else wrote it for us.

(8) Look at code and resource entities

The APK analyzer makes it easy to view the final version of a file: just click on the entity and the next page will display a preview of the text or image entity.

APK profilers can also display a variety of text and binary files. For example, in the Resources.arsc entity viewer, you can view configuration-specific values, such as language translations for string resources.

How do you feel? The language comparator has a quick check on our multilingual problems.

(9) Compare APK files

APK profiler can compare entity sizes in two different APK files. This is useful when you need to understand why your application is larger than previous versions. Before publishing the updated APK, do the following:

  1. The APK version to be released is loaded into the APK profiler.
  2. Click Compare With in the upper right corner of the APK analyzer.
  3. In the selection dialog box, find the APK that was last published to the user, and click OK.

How do you feel? Release comparison of each milestone, clear understanding of increase and decrease, better control quality.

(10) Dependency tree generation

Take a look at your APK

Xiaobian extension links

  • SamplePop code download
  • Android Performance Optimization family Bucket

Refer to the link

  • This is the first article that should be read

Only the sky on, no mountain and qi

❤ ❤ than heart