This article focuses on the size optimization of Android APK in Android performance optimization
But now the network speed has been very fast, user flow is also a lot, but for our Android APK file optimization is still necessary, easily dozens of hundreds of megabytes of size, user experience is still very bad, we will sort out the Android APK optimization method
Icon ICONS use SVG
There will be many ICONS in our App, and artists usually give them in sets, so we may need to put multiple sets of ICONS in our RES file, which will make our APK file volume become very large. Therefore, the first step of optimization will start from icon processing.
Ion should use SVG files rather than PNG files
First, SVG files exist as XML files, take up little space, and can automatically scale according to the device screen without distortion.
Andoid itself does not support importing SVG files directly, so we need to convert SVG files. As follows:
Use as follows:
<ImageView
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:src="@drawable/ic_icon_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> or <ImageView Android :layout_marginTop="100dp"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
app:srcCompat="@drawable/ic_icon_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Copy the code
Icon state differentiation using Tint shaders
Tint shader can realize image color change, using Tint to display different color pictures, in the original need for multiple images of different colors, can reduce the volume of APK
The UI looks like this:
Notice, this is a different effect from the same image
Use as follows:
Add a line android:tint="@color/colorAccent"
<ImageView
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"
android:src="@drawable/ic_icon_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/colorAccent"
/>
Copy the code
Use SVG when you need multiple sets of ICONS in different sizes
Android Studio has built-in features that allow you to customize your icon size and automatically generate PNG images of that size when you pack them.
Use as follows:
Under the defaultConfig TAB in the build.graldle of your app:
defaultConfig {
applicationId "com.example.apk"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"19 (5.0) vectorDrawables. / / minSdkVersion generatedDensities ('xhdpi'.'xxhdpi'.'xxxhdpi')
//minSdkVersion > 19
// vectorDrawables.useSupportLibrary = true
}
Copy the code
In this case, the drawable file looks like this:
Packed as follows:
In the future, only one set of maps is needed in APP to solve the problem of apK volume increase caused by multiple sets of maps
Large image compression in App, using WebP format pictures
WebP, an image format developed by Google to speed up image loading. Image compression volume is only about 2/3 of JPEG, and can save a lot of server broadband resources and data space.
Use as follows:
Comparison before and after transformation
Removing Useless Resources
One-click Removal (not recommended)
If you use dynamic ID to load resources, there will be problems, and this is a physical delete, once deleted, it will not be found back, so you can not use it as far as possible, if necessary, please backup the RES file in advance.
Use as follows:
Remove with shrinkResources, with //Zipalign optimization
To use shrinkResources, you must first turn on the code to obfuscate minifyEnabled
Use as follows:
BuildTypes {release {// turn on code to confuse minifyEnabledtrue/ / zipAlignEnabled Zipalign optimizationtrue// Remove the useless resource file shrinkResourcestrue
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}Copy the code
After packaging, the effect is as follows:
Although the picture still exists. But the size of 400 plus k becomes 2B
Resource packaging Settings
Due to the introduction of third-party libraries, such as appCompat-v7’s import library contains a large number of international resources, which can be retained or deleted according to their own services
The original package is as follows:
The original package contains languages of different countries, so we generally only need to keep Chinese, and the configuration is as follows:
defaultConfig {
applicationId "com.zthx.xianglian"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"// Only the specified and default resources resConfigs('zh-rCN'.'ko')}Copy the code
The configuration is as follows:
Dynamic library packaging configuration
If the project includes a third party SDK or uses the NDK directly, it will automatically package the full CPU architecture dynamic library into APK if it is not configured. For the real machine, only one Armeabi or Armeabi-v7A is needed, so it can be configured
// configure the so library architecture (real machine: ARM, simulator x86) NDK {abiFilters"armeabi"."armeabi-v7a"
}Copy the code
Turn on code obfuscation compression
BuildTypes {release {// Source code confusion to enable minifyEnabledtrue
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}Copy the code
For code obfuscation configuration, there is no more to say here. If you do not know, you can go to the Internet to learn about it
At this point, the eight steps of apK extreme optimization are over. If your APK has not been optimized at all, then
After these eight processes, you can see your APK volume reduced to at least half, go to try this magic optimization
The last
To share an architecture outline, including Android architects need to master all the technical system, we can compare their own shortcomings or lack of a direction to learn and improve;
Need hd architecture diagram and video data
You can add QQ group: 875911285 (note nuggets) or directly click the link below, you can get it.
Android learning PDF+ architecture video + interview document + source notes