Foreword
This article focused on optimizing the performance of Android in Android APK size optimization
Although speed has been very fast, but also a lot of user traffic, but carried out for our Android apk file optimization is still very necessary, at every turn tens of megabytes on the size of the user experience is very bad, here we come to organize about the optimization of Android apk
A, icon icons svg use
There will be many icon in our App, and artists are generally sets of small jiejie to, so in our res files may need to put multiple sets of icon, this way it will make our apk file size becomes very big, so, the first step in optimizing the process starts from the icon.
- Try to use svg file icon instead of png files
First svg files are xml files exist in the way, small footprint, and can not be distorted according to the device's screen automatically extendable.
Android itself does not support direct import svg file, so we need to change if the svg file 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"
/>
或者
<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"
/>
Two, icon state division using Tint shader
Tint shader enables color images, using different colors of images displayed Tint, otherwise required in the case of multiple identical images of different colors, it is possible to reduce the volume of apk
UI effect is as follows:
Note, this is different from the effect of the same image
Use as follows:
加上一行代码 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"
/>
Third, if desired icon sets of different sizes, using svg
Android studio own function, can self-configure the required size icon, automatically generated when the corresponding dimension of package png image.
Use as follows:
under the app build.graldle defaultConfig tag of:
defaultConfig {
applicationId "com.example.apk"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//minSdkVersion 19 (5.0)
vectorDrawables.generatedDensities('xhdpi','xxhdpi','xxxhdpi')
//minSdkVersion > 19
// vectorDrawables.useSupportLibrary = true
}
At this point, drawable file as follows:
After the package is as follows:
Later in the APP only need a map can solve multiple sets of map apk volume increase caused by the problem
Fourth, in the App big picture compression, picture format webp
WebP format a Google designed to accelerate the development of image loading speed of image formats. JPEG picture compression volume is only about two-thirds, and can save a lot of server resources and data broadband space.
Use as follows:
Comparison before and after conversion
V. Removing unused resources
- A key is removed (not recommended)
A key to remove unused resources, if there appears to use dynamic resource id will be loaded question, but it is physically deleted, once you delete will be brought back, so can not try not to use, have to use a backup beforehand res file .
Use the following
- Use shrinkResources be removed, with the optimization // Zipalign
Use shrinkResources must first turn on code obfuscation minifyEnabled
Use as follows:
buildTypes {
release {
//开启代码混淆
minifyEnabled true
//Zipalign优化
zipAlignEnabled true
//移除无用的resource文件
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
After packing effect is as follows:
Although the picture there. But the size of more than 400 k become 2B
Six resource pack set
Since the introduction of third-party libraries, such as the introduction of appcompat-v7 library contains a large number of international resources, can make the appropriate retention and deletion according to their business
Original package as follows:
There is the original package in national language, so we can generally only need to retain Chinese, 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"
//只保留指定和默认的资源
resConfigs('zh-rCN','ko')
}
After the configuration is as follows:
Seven dynamic libraries packaged configuration
If the project contains a third-party SDK or directly using the NDK, if not automatically configure dynamic library packed full cpu architecture into the apk, and for the real machine, just need to keep a armeabi or armeabi-v7a on it, so it can be at the configuration
//配置so库架构(真机: arm ,模拟器 x86 )
ndk {
abiFilters "armeabi", "armeabi-v7a"
}
Eight, open compression code obfuscation
buildTypes {
release {
//源代码混淆开启
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
About code obfuscation configuration, there is not more to say, do not understand their own can go online to find out
So far, eight apk ultimate optimization step is over, if you have not done any optimization apk, then this eight processes down your apk visual volume reduced to at least half, hurry to try this amazing optimize it
end
Okay, so the article to end here, if you feel good give a praise chant? If you think there need to be improved, please give me a message. Will be serious inquiry, insufficiently corrected. Thank you.
I hope you can read this and look forward to share my future will update the technology of dry cargo, thank you for your support!
+ + Forwarding thumbs attention , the first time to obtain the latest knowledge points
Android architects of the road is long, encourage each other with it!
Finally, I wish you all a happy life ~