To optimize the benefits

  1. Package size is reduced and easy to upgrade
  2. Multi-market channels have volume limitations to avoid secondary processing
  3. The APK installation time is reduced
  4. Small memory footprint at runtime
  5. The disk space is small, and the Odex binary file is small.

APK composition and analysis

An APK

  • Assets: Assets directory under the development directory
  • Lib: The so library required
  • Meta-inf: indicates the signature file
  • Okhttp3: OkHTTP network related
  • Org: org related information
  • Res: indicates the layout information in the res development directory
  • AndroidManifest: Four component configuration
  • Classes. dex: code zip file

Apk analysis

  1. Use the normal Apktool method

  2. Use the JADX tool

    github.com/skylot/jadx

    You can view various information by dragging APK directly into the program

  3. The Analyze APK in Android Studio can Analyze the composition of the APK

Optimization scheme

Code optimization

The code is all stored in the dex file, so we need to figure out what dex is.

Dex understand

Dex files are a compressed format designed for Dalvik

.java–>.class–>.dex

In the. Java –>. Class process, which is the operation of the JVM, in the. Class –>. Dex process, the. Class file needs to be converted to the Dalvik recognized. Dex file

The main structure of DEX is as follows:

Dex files can be reduced in size by about 50% compared to jar files

ProGuard code confusion

Turn on obfuscations in build.gradle, convert class names to meaningless A, B, C, etc., make it harder to read, and shorten names to effectively reduce dex size and remove code that is not used in the code.

Optimization of D8 and R8
  • D8: Converts Java bytecode to DEX code, which is simply a DEX compiler
  • R8: An alternative to confusing compression and optimization, but not a complete replacement for ProGuard

D8 is proposed to replace DX, and his optimization is as follows:

  • Shortened compilation time
  • Dex files are smaller
  • Dex has better running performance
  • Includes java8 language support

R8 is very similar to ProGuard, but is not a complete replacement

  • ProGuard is even more powerful at reducing enumerated types to raw integers
  • The pattern matching algorithm used in ProGuard can identify and replace short sequences of instructions, improving code efficiency and opening up opportunities for more optimizations. In optimizing the order of traversal, math operations and string operations in particular can benefit
  • ProGuard has a unique ability to optimize code that serializes or deserializes objects to JSON using the GSON library
Dex subcontracting optimization

When the number of APK methods exceeds 65536, the subcontracting strategy must be adopted, so that some repeated information will appear in cross-DEX calls:

  • Redundant method IDS: invoking across Dex will cause the current Dex to retain the method IDS in the invoked Dex. This redundancy will result in fewer classes in each Dex, and eventually lead to an increase in the number of compiled Dex. The increase in Dex data will further aggravate this problem.
  • Information redundancy caused by other cross-dex calls: In addition to the id of the method to be called, the definition information of the class to which it belongs and the current method need to be recorded more than once, resulting in redundant information of string_IDS, type_IDS, and proto_ids.

Therefore, ReDex is used for subcontracting optimization, and debug information and line number information in the dex file are removed

The use of SVG

Delete unnecessary languages using the specified language

Use XZ Utils for Dex compression

tukaani.org/xz/ Compress dex and store it in assets. This reduces the package volume but improves the installation time

Tripartite library optimization

In the case of tripartite libraries, small libraries can be used to accomplish current business by reducing the size and functionality of tripartite libraries such as Picasso, Glide, Fresco

Lint

Lint optimizes code to remove unused code

Reduce the use of enUms

How much memory does Enum occupy in Android? How do I use it?

Resource optimization

  • Lint: Uses Lint to remove redundant resources
  • ShrinkResources: Removes unwanted resources
  • Duplicate resource optimization: Multiple modules use the same resource file. Remove duplicate resource files and retain the first resource
  • Image compression: AAPT optimizes images and selects webP format images
  • Use targeted image formats
    1. Chat emoji out of a set of graphs => HDPI.
    2. Solid color small icon VD => RAW.
    3. If a large background is displayed, go to XHDPI.
    4. If the logo has a large equal weight, send two sets of pictures => HDPI, xHDPI.
    5. If some graphs are abnormal in the real machine, use multiple sets of graphs.
    6. If you encounter strange models, then targeted map complement.
  • Resource paths confused with individual resource paths: Shorten resource paths using AndroidResGuard
  • The resource file is placed on the server and loaded over the network

So library optimization

  • Filter the SO library through abiFilters
  • For sensitive SO libraries, different architectures are all placed in Armeabi, system architectures are judged in the program, and different SO libraries are loaded
  • Native Library compression: XZ Utils, SoLoader
  • So library dynamic download: mp.weixin.qq.com/s/X58fK02im…

Other optimization

  1. pluggable

    Plug-ins can be hot-swappable and downloaded from the server

  2. Changing the development model

    Use hybrid development

  3. Streamline business, simplify logic and business