Performance Optimization 7--app Slimming

Source: Internet
Author: User
Tags webp tinypng

1. Preface

If you are sensitive to app optimization, the size of the APK installation package will not be overlooked. About the reasons for slimming, there are probably the following aspects:

    • For the user, under the premise that the function difference is not big, the smaller apk size means less traffic consumption, also means more users to download;
    • For products, more than the competitor apk means a lower download base, not conducive to verifying product strategy;
    • For developers, app slimming is an opportunity to optimize technology and improve technology.
2, apk composition 2.1 apk Typical composition of a typical apk composition

The following table shows the APK directory and the file description:

Files/Directories Description
assets/ Store some static files that can be accessed via Assertmanager
lib/ If the directory exists, it is generally stored in the NDK compiled so
meta-inf/ Save the APK signature information
res/ The directory where the resource files are located, including drawable, layout, etc.
Androidmanifest.xml Program Global configuration file
Classes.dex Java Class, a file format that is compiled by Dex to be understood by Dalvik/art virtual machines
Resources.arsc Binary resource files generated after compilation
2.2 Example Analysis

Do app slimming before need to have a clear understanding of the existing composition of their own app, the above decompression method can only be roughly see the size of the specific directory, but the useful information is still limited.

2.2.1 Android Studio Analyze APK

Android Studio 2.2 has a feature analyze APK, convenient and simple, features or Google comes with the reliable;

    • To see the size of any file in the APK, get an intuitive understanding;
    • Learn about the composition of Dex files, see the use of those open source libraries, etc.;
    • View binaries (such as androidmainfest.xml, etc.);
    • APK comparison, easy to find the difference between the two versions.
Analyze APK uses the 2.2.2 Anti-compilation tool Classyshark

Classshark is an Android executable file (apk) browsing tool, you can easily open apk/class/jar/res and other files and analysis of the contents.

Use of Classyshark 2.2.3 nimbledroid

Nimbledroid, a PhD entrepreneurship team at Columbia University, developed a system that analyzes the performance indicators of Android apps in both static and dynamic ways, where static analysis can analyze the big file leaderboards in APK installation package, The size of the various well-known SDKs and the percentage of the code as a whole, the size and ranking of various types of files, the number of methods for various well-known SDKs, and the proportion of methods in all Dex.

File Size Ranking
Number of methods statistics

Summary: These three ways can have a clearer understanding of the composition of the APK, but it is more recommended to use the Androidstudio analyze apk, simple and efficient.

Using analyze apk to see the file size found that Classes.dex, res, assets, Lib and other files larger, where the fat, we go to smoke where. Determine the direction of optimization:

    1. Code part: Redundant code, useless function, code confusion, method number reduction and so on;
    2. Resources: Redundant resources, resource confusion, image processing, etc.
    3. The processing of so files.
3, apk thin body Code slimming 3.1 Remove useless code, function

With the iteration of the version, some features may have been removed, but their code still exists in the project. removing useless code and useless functionality can help reduce the amount of code, and the direct manifestation of this is that Dex's volume will become smaller.

Note: based on experience, the absence of code in the project is a common phenomenon, equivalent to the zombie code, and too much of this type of code can cause the Dex file too large.

3.2 Remove the useless library, avoid the function of the same library 3.2.1 Project basic functions of the Library to be unified implementation, to avoid the emergence of multiple sets of network requests, picture loader and other implementations. 3.2.2 Libraries to remove items in a timely manner, such as when we decided to switch from one push to a push, we should remove the push library from the original project and not just comment out its registration code. 3.2.3 Some features can be a curve to the national salvation, do not introduce the SDK, such as positioning function, you can not introduce the positioning SDK, and by the latitude and longitude and then call the relevant interface to achieve, also realized the function and did not introduce the SDK. 3.2.4 and the introduction of the SDK also need to consider the number of methods, you can use Classyshark, Nimbledroid or Apk method count and other tools to view.

Note: Based on experience, it is not uncommon for a project to have libraries that were not used after the previous use.

3.3 Enable Proguard

Code obfuscation, also known as the flower Directive, translates the code of a computer program into a functionally equivalent but difficult to read, understandable behavior. Proguard is a free Java-class file compression, optimization, obfuscation, pre-validated tool that detects and removes unused classes, fields, methods, properties, optimizes bytecode and removes unused instructions, and changes the name of the class, field, and method in your code to a short, meaningless name.

It can be seen that Proguard not only changes the various elements in the diamante, but also removes redundant code, thus reducing the size of the Dex file.

android {    buildTypes {        release {            true            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘),                    ‘proguard-rules.pro‘        }    }    ...}

Where Proguard-android.txt is getting the default Proguard settings, Proguard-rules.pro files are used to add custom Proguard rules.

Note: For Proguard, although the effect is obvious, but still need to be cautious;

    • Code obfuscation slows down the construction of a project, so switching off Proguard in debug mode does not make rd crazy when running code;
    • Because the Proguard is turned off in debug mode, if the obfuscation rule is not configured, the release mode may not appear in the debug mode of the bug;
    • Proguard is not you want to engage in, if the app has done a period of time to do Proguard, the project package structure is not standardized, then Proguard rules will be very much. The adjustment of the package structure in a short time is also a very painful thing.
3.4 Reduction Method Number

In general, the number of reduction methods is for Android's famous 64k method number problem, which is no longer reviewed here, see the previous series on Multidex article. The goal of reducing the number of methods here is to slim down the app.

With Dalvik executable format, we can see the composition of the Dex file. As you can see from the Method-ids-size field in the Header-item table, you can reduce the size of the method list after reducing the number of methods, while the method takes up less space in the Dex file and the app is naturally thin.

The number of reduction methods, in addition to the general method written above: remove useless methods, libraries, using the smaller SDK also:

    1. Avoid accessing private methods, variables of external classes in internal classes. In the Java inner class (which contains anonymous inner classes) to access the private methods of the external class, variables, the compiler will generate additional methods, will increase the number of methods;
    2. Avoid calling non-overridden methods in derived classes, avoid calling methods of the non-overridden base class in derived classes, and avoid invoking methods of base classes that are not overridden in derived classes with objects of derived classes. When a method is called that is not overridden in a derived class, it produces more than one method number;
    3. Remove the Get and set methods for some classes, and of course sacrifice some object-oriented concepts.
4, apk thin body of resources slimming

The weight of the code and resources is equally important for importance, but the effect is that the slimming effect of the resource file is much better than the slimming effect of the code. It's very likely that the weight-loss effect of a long-time code can be easily achieved in a thin body of resource files.

4.1 Removing useless resource files

Removing a useless resource file is easier than removing the useless code, right-clicking in any file in Android Studio and choosing to clear the unused resources to delete the resource files that are not used.

Remove Unused Resources

Note: When you set Shrinkresources to True in Build.gradle, the unused resources are automatically excluded each time you package. Shrinkresources need to be used in conjunction with minifyenabled. But according to my experiment: useless resources will still be hit into the apk, just become a black diagram, the volume is very small, only less than 100b. There are mistakes in the use of the place welcome correction!

4.2 drawable directory keeps only one copy of the resource

It's a lot of discussion in this developer, and it's true that Google strongly recommends that multiple sets of graph resources be prepared to fit the different screen densities. But given that the UI requirements on Android will not be the highest level of height, and even if placed in the appropriate (note the word) in a directory, at different resolutions will also do automatic adaptation (proportional stretching, scaling) , so it is recommended that the UI is not the top-level requirement, according to its own user group model in a suitable directory. This will undoubtedly reduce the size of the res, thereby reducing the volume of the APK.

Note: images placed in inappropriate directories may have a significant impact on memory, you can refer to the previous article "Android Performance optimization (v) Bitmap".

4.3 Compressing a picture

Before I found in the project file size of 1M images, may be due to the UI classmate and Rd classmate's double negligence, resulting in such a large picture into the project, the impact of the APK volume naturally self-evident.

Consider compressing your pictures with tools such as Tinypng, Pngquant, Imageoptim, and so on, which can reduce the PNG file size while maintaining image quality.

Here take Tinypng as an example: Tinypng is a pretty good image compression tool, in the case of preserving the alpha channel, the compression of PNG can reach 1/3, and with the naked eye basically can't tell the loss of compression. This 3.4M image was compressed to 984.7k, with a compression rate of up to 71%.

Example of compressing a picture using tinypng

Also has the classmate developed a Androidstudio plugin: Tinypngplugin, can compress the picture in the project in bulk, more convenient.

Note: It should be noted that in the Android build process, AAPT uses built-in compression algorithms to optimize PNG images in the res/drawable/directory, but it can also lead to larger images that have already been optimized. You can prevent AAPT from using the default way to optimize images that we have optimized by setting cruncherenabled in Build.gradle.

aaptOptions {    false}
4.4 PNG conversion jpg

PNG is a lossless format and JPG is a lossy format. JPG in the processing of a lot of color pictures, depending on the compression rate, sometimes remove some of the naked eye to identify the small gap between the middle color. However, PNG will strictly retain all color numbers for this basic requirement. So the size of the picture, or the number of colors, especially the gradual change of color, PNG volume will be significantly larger than JPG.

In this case, we can make a choice. Use PNG when small size, less color, or alpha channel transparency, use JPG for large size, color gradients, and more.

Note: According to experience, for the image can be directly used in JPG format, it is best not to convert from PNG to JPG, but the image is directly out of the JPG format, relatively speaking, the latter is better.

4.5 Using vector images

Scalable Vector Graphics (English: Scalable Vector graphics,svg) is a graphic format for describing two-dimensional vector graphics based on Extensible Markup Language (XML). SVG was developed by the consortium and is an open standard. You can use vector graphics to create resolution-independent icons and other scalable pictures. Using vector images can effectively reduce the size of images in your app, and vector graphics are represented as vectordrawable objects in Android.

Advantages

    • Image extensibility: No damage to image quality, a set of drawings to fit all;
    • The picture is very small: more than 10 times times smaller than using bitmaps, which helps to reduce the APK volume;

Disadvantages

    • Good performance loss, system rendering vectordrawable takes more time, because the initialization of the vector graph load will consume more CPU cycles than the corresponding raster pictures, but the memory consumption and performance are close;
    • Vectors are mainly used in the single-tone icon.
4.6 Using WEBP

Google introduced a new image compression format,-WEBP, in 2010, providing lossless and lossy compression for images while supporting transparent channels under lossy conditions. According to official experiments: lossless WEBP compared to PNG 26% size, lossy webp in the same ssim (Structural similarity Index, structural similarity) compared to JPEG reduced 25%~34% size; lossy WEBP also supports transparent channels, The size is usually approximately 1/3 of the corresponding PNG. At the same time, Google in 2014 put forward a dynamic WEBP, expand WEBP Make IT support dynamic graph ability. Dynamic WEBP supports richer colors compared to GIF, and also takes up less space and is more adaptable to mobile network motion diagram playback.

Advantages:

    • WEBP in the same image volume smaller, WEBP support transparency, compression compared to JPEG higher but the display effect is not lost in JPEG;
    • The conversion of PNG to WEBP can be done through tools, cloud service, etc.

Disadvantages:

    • Android starts from 4.0 WEBP native support, which means to be compatible with 4.0 of the following models need to add the appropriate library, of course, the market is now suitable for less than 4.0 applications.
    • Android 4.2.1+ only supports the display of WEBP with transparency, so the minimum version is less than 4.2.1 of the app is not available to use. You can convert a picture that does not display transparency to WEBP.
4.7 Resource Confusion

During the APK packaging process, AAPT generates a corresponding int value for each resource, and we use this int to find the resource. In the apk composition, we can see that there is a RESOURCES.ARSC file, which holds the resource ID and the resource key mapping relationship.

When the image is called, the drawable classification is found, then the matching config table is found according to the current system config, and the corresponding RES data is found according to the ID. drawable is stored as a string type in ARSC, and the RES data has the index of this resource in the RES string pool. Depending on the index, a string can be found in the string pool. This string is actually a path, for example: res/drawable-xhdpi/icon.png; The confusion is to change the path to R/s/f.png, and to modify the mapping of the RESOURCES.ARSC file. This makes it clear that resource obfuscation can reduce the cause of the APK:

    • RESOURCES.ARSC become smaller;
    • The file information is smaller, the ultra-short path is used, and the res/drawable-xhdpi/icon.png is modified to r/s/f.png.

The resource obfuscation scenario recommended here: Andresguard.

4.8 Resource Online

The use of low-frequency resource samples, put on the Internet, in the right time to download in advance, this can also save some space.

5, the apk slimming so thin body

So (Shared object, share library) is a binary code that the machine can run directly, and is a dynamic link library on Android, similar to a DLL on Windows. The ABI supported by each Android app is determined by the. So file provided by its apk, and these so files are packaged in the lib/directory of the apk file.

So common scenarios such as: Encryption and decryption algorithm, audio and video codec, core code and so on. When generating so files, you need to consider adapting to different phone CPU architectures on the market and generating so files that support different platforms for compatibility. Currently Android supports seven different types of CPU architectures, namely: Armv5,armv7 (since 2010), x86 (since 2011), Mips (from 2012 onwards), Armv8,mips64 and x86_64 (from 2014).

In theory, the CPU architecture of so is the highest execution efficiency, but this will result in the Libs directory placement of the various schema platform so file, the size of the APK file is naturally larger. Then we naturally think of reducing the Libs directory, the general situation (attention limit) to leave the Armeabi directory, Armeabi directory of so can be compatible with other platforms so, but the performance will be lost, loss of the optimization of the specific platform.

It is therefore necessary to make a specific distinction based on the so function that is used for the performance-sensitive module, which can be placed in the Armeabi directory, then the code to determine the device's CPU type, and then load its corresponding schema of the so file, for example. It reduces the size of the APK and does not affect performance-sensitive module execution.

Use of SO

The way to remove a specific platform so that the package will only save so in Armeabi.

ndk {    //设置支持的SO库架构    abiFilters  ‘armeabi‘}

Note: The CPU of the original x86 architecture does not support the so that the ARM architecture is running, but Intel and Google have partnered with a binary Translator named Houdini on top of the system core layer of the x86 machine (binary conversion middle tier). The middle tier dynamically reads the arm instruction during runtime and converts it to the x86 instruction to execute.

6, APK slimming 7ZIP compression

We know that the apk file is actually a zip file. The Android SDK Packaging tool Apkbuilder uses the DEFLATE algorithm to compress the Android app's code, resources, and other files, compress it into a zip format, and then sign the release.

Since it is compression, can you improve its compression method to get a smaller apk file? By analyzing the flowchart of the APK package we can find that the Signedjarbuilder class compresses the entire project, including code Dex and some of the lessons compressed resources, files, using the algorithm provided under the ZIP package in the JDK.

Use 7ZIP compression

In a simple way, we can compress the generated apk file two times without changing the app compiler, using the deflate algorithm, but the compression level is lifted from standard to ultimate compression. Increase the compression level to get a smaller apk without making any changes to the contents of the APK package itself.

Note:

    • It is important to note that the signature after the limit compression is destroyed and needs to be re-signed.
    • The Android Platform APK installation package decompression algorithm only supports the DEFLATE algorithm, other algorithms such as LZMA, although the compression rate is better, but because the Android platform is not supported by default, so if the use of this algorithm compression apk, will cause the APK can not install.
    • No good 7ZIP compression software is found on Mac and needs to be used under Windows.
7, App slimming Summary: 7.1 Code Slimming
    • Remove useless code, function;
    • Remove useless libraries and avoid the similarities of the functions;
    • Enable Proguard;
    • Number of reduction methods;
7.2 Resource Downsizing
    • Remove useless resource files;
    • The drawable directory retains only one copy of the resource;
    • Compress the image;
    • PNG convert jpg;
    • Use vector images;
    • Use WEBP;
    • Resource confusion;
    • Resource online;
7.3 So thin body
    • In the case of permitting, the distribution of a particular architecture is retained for the user model;
7.4 7Zip Compression

Use 7Zip to compress the APK for ultimate compression.

7.5 Other
    • Classes such as plug-ins, the Dex and resource files on the server, need to download, but the plug-in implementation and integration with the existing project is not very difficult, but also beyond the topic of this article, not elaborate;
    • By configuring the Include in Build.gradle to generate a separate installation package for each CPU architecture, upload the APK according to the schema, but this scheme is rarely used in the domestic application market and can only be used on Google Play.
    • A little bit of experience: slimming the APK, slimming so and resource files is the quickest operation. After slimming so and deleting unused images and compressing images, the apk shrinks a large percentage, while the optimizations for Dex may not be obvious.

Performance Optimization 7--app Slimming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.