Android development optimization - Apk slimming optimization

Understand APK structure

Before discussing how to reduce the size of an application, it is necessary to understand the structure of an application APK. An APK file consists of a Zip archive that contains all the files that make up the app. These include Java class files, resource files, and files for compiled resources.

The APK contains the following directories:

  • META-INF/ : Contains the CERT.SF and CERT.RSA signature files, and the MANIFEST.MF manifest file.
  • assets/ : Contains the app's assets; the app can retrieve these assets using the AssetManager object.
  • res/ : Contains resources (images, audio and video, etc.) not compiled into resources.arsc.
  • lib/ : Contains compiled code specific to the processor software layer. This directory contains subdirectories for each platform type, such as armeabi , armeabi-v7a , arm64-v8a , x86 , x86_64 , and mips .

The APK also contains the following files. Of these files, only AndroidManifest.xml is required.

  • resources.arsc : Contains compiled resources. This file contains the XML content of all configurations in the res/values/ folder. The packaging tool extracts this XML content, compiles it into binary form, and compresses the content. This content includes language strings and styles, and paths to content not directly included in the resources.arsc file, such as layout files and images.
  • classes.dex : Contains classes compiled in a DEX file format understood by the Dalvik/ART virtual machine.
  • AndroidManifest.xml : Contains the core Android manifest file. This file lists the app's name, version, access rights, and referenced library files. This file uses Android's binary XML file

Android Size Analyzer

The Android Size Analyzer tool makes it easy to discover and implement multiple strategies for reducing app size.

First download and install the Android Size Analyzer plugin from the plugin market in Android Studio. After installing the plugin, select Analyze > Analyze App Size from the menu bar to run an app size analysis on the current project. After the project is analyzed, a tool window appears with suggestions on how to reduce the size of the app.

Remove unused resources

The key to APK slimming is in one word: delete! Delete it if it is useless.

Enable resource reduction (without packaging)

If resource shrinking: shrinkResources is enabled in your app's build.gradle file, Gradle can automatically ignore unused resources when packaging the APK. Resource minification only works when used in conjunction with code minification: minifyEnabled. After the code reducer removes all unused code, the resource reducer can determine which resources the app still uses.

android { 
    // Other settings 
    buildTypes { 
        release { 
            minifyEnabled true 
            shrinkResources true 
       proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
        } 
    } 
}    
复制代码

Dynamic library packaging configuration

The so file is a dynamic library compiled by ndk, written in c/c++, so it is not cross-platform. ABI is short for Application Binary Interface, which defines how binary files (especially .so files) run on the corresponding system platform, from the instruction set used, memory alignment to available system function libraries. In the Android system, each CPU architecture corresponds to an ABI, currently supported are: armeabi-v7a, arm64-v8a, x86, x86_64. At present, mobile phone devices on the market are basically arm architecture, and armeabi-v7a is compatible with almost all devices. So it is possible to configure:

android{ 
    defaultConfig{ 
        ndk{
            abiFilters "armeabi-v7a" 
        } 
    }
 }
复制代码

For third-party services, such as Baidu Maps, Bugly, etc., the CPU architecture of the whole platform will be provided. After the above configuration, it means that only armeabi-v7a will be packaged into Apk. Thereby reducing the APK size.

Guess you like

Origin juejin.im/post/7098256852768522277