[Android optimization series] apk slimming

Overview
Why APKs need to be slimmed down. The larger the APK, the more traffic they will consume during the download and installation process, and the longer the installation waiting time; for the product itself, it means that the download conversion rate will be lower (because among competing products, users have more opportunities to choose The one with the best experience, the most functions, the best performance, and the smallest package), so the slimming optimization of apk is also very important. This blog will describe the related content of slimming apk.

Package body analysis
In the Android Studio toolbar, open build->Analyze APK, and select the APK package to be analyzed.
20170210105239708 It
can be seen that the main space occupied is code, pictures, resources and lib and assert files. The main direction is to simplify code, compress pictures, remove useless libraries, and reduce the files in asserts.

Using a set of resources
For most logarithmic apps, it is enough to take only one set of design drawings. In view of the current trend of resolution, it is recommended to take 720p resources and put them in the xhdpi directory.
Compared with multiple sets of resources, only one set of 720P resources is used, which has little difference in vision. The same is true for the products of many large companies, but it can significantly reduce the size of resource usage, and by the way, it can also reduce the workload of designers. .

Enable minifyEnabled to obfuscate code Use minifyEnabled to configure Proguard obfuscation
in gradle, which can greatly reduce the size of the APP:

android {
    buildTypes {
        release {
            minifyEnabled true
        }
    }
}
In proguard, whether or not to retain the symbol table has a significant impact on the size of the APP, and it may not be retained as appropriate, but it is recommended to retain it for debugging as much as possible.
Parameter description:

-include {filename} reads configuration parameters from the given file  
-basedirectory {directoryname} specifies the base directory as the relative file name in the  
future -injars {class_path} specifies the application jar, war, ear and directory to be processed  
-outjars {class_path} specifies the name of the jar, war, ear and directory to be output after processing  
-libraryjars {classpath} specifies the library files required for the application jar, war, ear and directory to be processed  
-dontskipnonpubliclibraryclasses Specify not to go Ignore non-public library classes.  
-dontskipnonpubliclibraryclassmembers Specifies not to ignore members of library classes that are visible to the package.
reserved options

-keep {Modifier} {class_specification} protects the specified class files and members of the class  
-keepclassmembers {modifier} {class_specification} protects the members of the specified class, if this class is protected they are better protected  
-keepclasseswithmembers {class_specification} protect The specified class and class members, but only if all specified classes and class members are to exist.  
-keepnames {class_specification} protect the names of the specified classes and class members (if they will not be removed in the compaction step)  
-keepclassmembernames {class_specification} protect the names of the specified class members (if they will not be removed in the compaction step)  
-keepclasseswithmembernames {class_specification} protects the names of the specified class and class members if all specified class members are present (after the compression step)  
-printseeds {filename} lists the list of classes and class members -keep option, stdout to the given To read the full text of the document, 
click directly: http://click.aliyun.com/m/10245/

Guess you like

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