Resources unified

Try to use the same set of resources for one project. For most logarithmic apps, it is enough to take only one set of design drawings. Given the current resolution trend, it is recommended to take 720p resources and place them in the XHDPI directory.

Compared with multiple sets of resources, only using a set of 720P resources has little visual difference, and the products of many large companies are the same, but it can significantly reduce the size of resource occupation, which can also reduce the workload of designers. Instead of deleting all xHDPI directories, it’s important to keep a set of design resources.

Compress the resource image before introducing it, do not use the original image (do not use the uI-cut original image directly)

Use JPG images

For large, opaque images, JPG will have a significant, though not absolute, advantage over PNG in size, often reduced by more than half. It would be wise to use JPG for large display areas such as startup pages, active pages, etc.

Use webP format images

  1. WebP is an image file format that supports lossless compression and lossless compression. According to Google’s tests, lossless compression of WebP files is 26% smaller than PNG files, and lossless compression of WebP images is 25% to 34% smaller than JPEG images of equivalent quality index.
  2. Webp supports transparency, compression ratio is higher than JPG but display effect is as good as JPG, official evaluation quality parameter is equal to 75 balance best.

Compared with JPG and PNG, WebP, as a new image format, has not been widely used in mobile phones due to the support of Android. Since Android 4.0+ native support, but does not support including transparency, until Android 4.2.1+ does not support the display of transparency webP, when using special care. 3. According to Google’s test, compared with JPG, the encoding speed of WebP is 10 times slower and the decoding speed is 1.5 times slower. The problem lies in whether the decoding speed of 1.5 times will affect the user experience. At the same time, due to the reduction of file size, shortened load time, page rendering speed. Meanwhile, as the number of images increases, WebP pages load faster than JPG pages. So there are few technical barriers to using WebP.

Use the Shape background and selector coloring scheme

For some simple graphics or background pictures, by custom drawing graphics instead of introducing the image itself. For some images that look the same but have different colors, you can use the selector file.

Online library

If there are many or a group of graphics to be introduced, such as emojis, consider using online introduction to save local space. However, it will increase code complexity and APP traffic consumption at the same time.

Lint check

Code scanning tools that can help you find and correct problems with the quality of your code structure, for example, if an XML resource file contains unused namespaces that not only take up space, but also cause unnecessary processing.

The website address

Terminal Execution command

gradlew lint
Copy the code

Or you can manually run configured Lint and other IDE checks by selecting Analyze > Inspect Code in order. The Inspection Results are displayed in the Inspection Results window and can be deleted if there are unused resources.

Delete unnecessary SO libraries

Basically, armable SO is compatible with Armable V7. The armable V7A library will greatly improve the graphics rendering. If not, it can be simplified. Very few devices Crash, test passes and then use. So under the x86 package is required in the x86 model of mobile phone, if the product does not have this requirement can be simplified. The actual working configuration is that you can choose to keep only so files under armable and armable-x86.

Remove useless language resources

Only Chinese and English language resources are reserved for some SDKS that support multiple languages.

android {
    defaultConfig {
        resConfigs "zh-rCN", "en-rUS"
    }
}
Copy the code

Open the confusion

android {
    defaultConfig {
      minifyEnabled true
    }
}
Copy the code

Enable shrinkResources to remove unwanted resources

The unused resource files will be removed when they are packaged, but the source code will not change. However, the actual measurement of this scheme may not have a positive effect on apK slimming optimization, so it can be used as appropriate. However, it should be used at the same time as the previous step. It is invalid to use it alone.

android {
    buildTypes {
        release {
            shrinkResources true
        }
    }
}
Copy the code

Using the zipAlign

Resource alignment tools (aligned to four-byte boundaries and accessed in units) make resources more efficient when accessed and optimize packaged applications, making interactions between systems and apps more efficient, but not significantly slimming apK.

android {
    buildTypes {
        release {
           zipAlignEnabled true
        }
    }
}
Copy the code

Use AndResGuard to compress resource files

The above methods mainly deal with the resources themselves and compress the code, but the resources in APK do not have too much operation. AndResGuard wechat exit is a tool to help you reduce the size of APK, but only for resources (mainly RES) does not involve compilation process. He makes the lengthy resource path shorter, for example changing res/drawable/wechat to R/D/A

Official website and introduction method

Add obfuscation whitelist: Specify resource path rules that do not need to be obfuscated, mainly some third-party SDKS. Because some SDK codes reference corresponding resource files, if they are obfuscated, the corresponding resource files cannot be found and crash will occur. Therefore, resource files cannot be obfuscated. The whitelist link is as follows;

White list

You can also choose to customize Gradle when importing

After the configuration is complete, print the REQUIRED APK in the following way

The effect is similar to that of wechat’s APK after compression. Change the RES file to the following r folder form, and the compression is good.