Performance optimization 08-APK slimming

Performance optimization 08-APK slimming

1. Image compression

Use images in SVG and webp formats to reduce image size. Using the tint shader, the number of images can be reduced. In addition, you can use the Typng tool to compress png images.

### 1、SVG

SVG (Scalable Vector Graphics) scalable vector graphics. SVGs do not degrade image quality due to scaling like bitmaps. The advantage is that it saves space and memory. Often used for simple small icons.

SVG is defined by xml, and the standard svg root node is <svg>. In Android, the support for svg is realized through Vector, and the root node is <vector>.

To get a svg, you need to convert it before it can be used in android. Open the project in Android Studio and right-click in the res directory Vector Asset.

Vector graphics support is available on Android 5.0 (API level 21) and higher. If the minimum API level of the application is lower than the above version, use png to generate: add it in app/build.gradle, generatedDensities=['xhdpi','hdpi']and the corresponding png image will be automatically generated.

Reference: https://developer.android.google.cn/studio/write/vector-asset-studio.html .

3、webp

Using images in webp format can reduce the size of the apk.

To get a single image in webp format, you can select the image on AndroidStudio and right-click to select Converting Images to Webpit to convert.

Obtaining multiple images in webp format can be achieved through third-party plug-ins:

In your project's gradle file add:

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.dongnao.optimizer:optimizer-picture:2.0.0'
    }
}

Add in the gradle of the module:

apply plugin: 'com.dongnao.optimizer'

3、Tinypng

Tinypng is an efficient png compression tool and can compress a certain number of images for free.

If the number of compressions is small (within 20), you can compress directly on the official website; if the number is large, you can use a python script for batch compression.

4. Tint shader

Tint can realize the color change of pictures. An ImageView only needs to set a picture, and then set different colors through tint, you can see pictures of different colors. Originally, when multiple images of the same color are required, after using tint, only one image is needed, which can reduce the size of the apk.

2. Delete useless resources

There may be some useless resources in the app, which need to be deleted and then packaged.

1. Resource packaging configuration

Due to the introduction of third-party libraries, such as appcompat-v7, the library contains a large number of internationalized resources, which can be deleted through configuration according to the situation.

Add in the gradle of the module:

android {
    compileSdkVersion 26
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        resConfigs 'en' //只保留英文资源
    }
 }

2. Dynamic library packaging configuration

If the project contains a third-party SDK or uses the ndk by itself, if it is not configured, the dynamic library of the full cpu architecture will be packaged into the apk. For the real machine, you only need to keep an armeabi (armeabi-v7a) on it.

android {
     defaultConfig {
         ndk {
               abiFilters "armeabi-v7a"
         }
     }
}

### 3. Remove useless resources

1. One-click removal

One-click removal: right-click the item, select Refactor, select Remove Unused Resources.

If there is a problem using a resource with a dynamic id, it is not recommended to use one-click removal. The resource id obtained dynamically is the resource id that does not use R.xx.xx.

2. Check with Lint

Steps: Right-click the item, select Analyze, select Run Inspection By Name, enter Unused Resources.

Check out the useless resources, check and delete them manually.

3. Open Proguard

1. Open Proguard

Open Proguard, you can compress (Shrink), optimize (Optimize), obfuscate (Obfuscate), pre-check (Preveirfy).

  1. Shrink: In this step of the shrink process, it is used to detect and delete unused classes, fields, methods and properties.
  2. Optimize (Optimize): In this step of the optimization process, the bytecode is optimized and useless instructions are removed.
  3. Obfuscate: In the obfuscation step, classes, fields and methods are renamed using meaningless names such as a, b, c, etc.
  4. Pre-check (Preveirfy): In the pre-check step, the processed code is mainly pre-checked on the Java platform.
android {
    buildTypes {
        release {
            shrinkResources true //压缩
            minifyEnabled true //混淆代码
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

2. Obfuscating resources

resource.arsc provides the mapping relationship between resource ID and resource file path, such as the mapping relationship between R.layout.activity_main (ID: 0x7f030000) and res/layout/activity_main.xml, the resource ID used in the application development process uses 0x7f030000 to find resources .

Android obtains a resource such as a picture or xml through AssetManager and Resources. Among them, the Resources class can find resources according to the ID, and the AssetManager class can find resources according to the file name. The Resources class first finds the resource file name according to the ID, and then passes the file name to the AssetManager class to open the corresponding file by looking for the arsc file (resource mapping file).

Obfuscation is to modify the mapping table (resource.arsc file). The obfuscation steps are:

  1. Parse arsc files (mainly global and resource name string pools)
  2. Modify the strings in the string pool (replace with meaningless a/b)
  3. Modify the resource file name of the res directory in the apk
  4. Pack (7zip), align, sign.

Parse reference: https://github.com/google/android-arscblamer

Previous: Performance Optimization 07 - Process Keep Alive

Next

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324800608&siteId=291194637