APK slimming guide

Why is the apk getting bigger and bigger?

1. The project continues to develop, the more functions, and the increase in the amount of code, the resource files are also increasing.

2. App supports more and more mainstream dpi, such as ldpi, mdpi, hdpi, xh xxh xxxh, etc., which indirectly leads to an increase in resources.

3. More and more third-party SDKs or open source libraries have been introduced, adding a lot of duplicate resources or methods and invalid codes.

4. High requirements for experience, higher resolution pictures will be used for picture resources to ensure clarity.

5. Versions continue to iterate, invalid codes and image resources continue to accumulate

 

The entry point of apk slimming

Decompress an unreinforced apk file and we will see the content below. I believe you are already clear about the meaning of these files or directories.

Next, we will perform apk slimming operations from three aspects:

1. Java code, corresponding to the classes.dex file, we need to eliminate invalid codes and reduce the size of the dex file.

2. Resource files, corresponding to res folder, assets directory.

3. Reduce the size of so introduced in lib.

 

Start to lose weight

Next, we will perform apk slimming for the three entry points above.

1. Resource file slimming

Prefer WebP images

Resource files are a large part of the apk, especially image resources, so we mainly focus on thinning pictures. First, we must have a simple understanding of the image encoding format. The formats supported by the Android platform are: JPEG, PNG, GIF, BNP, WebP (WebP is supported since 4.0), but in the development of Android applications, Bitmap supports only three types of JPEG, PNG, WebP, from the CompressFormat enumeration of the Bitmap class. see.

JPEG: lossy compression, does not support transparent channels and multi-frame animation (RGB)

PNG: Lossless compression, support transparent channel (ARGB), PNG size is larger than JPEG

WebP: supports lossy and lossless compression, supports transparent channels and multi-frame animation, above 4.0 is the first choice for development, Google official test, WebP can reduce the size of PNG by 45%, even if PNG is compressed, it can also be reduced by 28% compared to PNG

Use the NinePatch format for PNG format pictures as much as possible

9 pictures have the characteristics of small size, stretch and no deformation, and Android studio can be converted with one key, which is quite convenient to use.

Second, reduce the size of the code

As the project progresses, a lot of redundant code will be generated in the coding process, such as the ones that should be deleted without being deleted, too many repetitive functions, invalid references, etc. This is the second, the larger one. The problem is that as the number of open source libraries you introduce continues to increase, the emergence of invalid code is almost an inevitable event. Too many repeated methods will also cause the problem of 64k methods.

1. resConfigs removes resources in third-party libraries or SDKs

Invalid resources contained in third-party libraries usually include the following two points:

  • dpi directory, third-party libraries usually provide all dpi directories and corresponding resource files, because it is for public use, and we may not need them all. At this time, it needs to be selectively removed.
  • Due to the introduction of third-party libraries, such as appcompat-v7, the library contains a large number of internationalized resources, which we may not need. At this time, you can configure and delete internationalized resources according to the situation.

defaultConfig {

       ... ...

      resConfigs "zh" //Indicates that only Chinese is used, and multiple languages ​​are excluded

      resConfigs "xxhdpi" // means that only resource files in the xxhdpi directory are used

}

2. Turn on obfuscation

  1. 开启minifyEnable :minifyEnable true

3. Resource compression

shrinkResources identifies whether to remove useless resource files. It needs to be used with minifyEnable. There is also the problem of reflection mechanism reference, which will be deleted by mistake

4. Remove useless resources

①Remove useless resources with one click (not recommended)

②Lint inspection

Lint can check the invalid resources in the res directory (the assets cannot be checked), and then delete them, but one thing to note is that if the resource file is called through the reflection mechanism, Lint cannot know, so use lint to check every one Resources need to be manually confirmed to prevent abnormal deletion.

 

Three, libs directory slimming

The so file will eventually be packaged into the libs directory. Our slimming of the libs directory is mainly to remove the so files of the unnecessary platform ABI. For example, we only keep the so of the v7a and x86 platforms.

defaultConfig {

    ndk {

          abiFilters "armeabi-v7a","x86"

      }

}   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/MYBOYER/article/details/95765029