Android APK瘦身實踐

現在APK動不動就是幾十兆,當用戶連接到2G和3G網絡,或者按流量收費的設備是多麼的浪費,這篇文章介紹瞭如何對APK進行瘦身,幫助更多用戶毫不猶顧忌地下載你們的APP。

以我的個人APP【 微言 】實踐,未做任何處理,APK大小 11712 KB= 11.43 MB。

開啓minifyEnabled混淆代碼

在app/build.gradle打開minifyEnabled:

android {
buildTypes {
release {
minifyEnabled true
}
}
}

在app/proguard-rules.pro編寫混淆規則,根據自己項目依賴的庫一一混淆,這裏就不提供具體混淆代碼了。

經過混淆,APK大小 10906 KB,效果很明顯,一下子瘦了0.78 MB。

開啓shrinkResources去除無用資源

在app/build.gradle打開shrinkResources:

android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
}
}

shrinkResources依賴於minifyEnabled,必須和minifyEnabled一起用,就是打開shrinkResources也必須打開minifyEnabled。

APK大小 10903 KB。

刪除未使用到xml和圖片

如何知道哪些xml和圖片未被使用到?使用Android Studio的Lint,步驟:點擊菜單欄 Analyze -> Run Inspection by Name -> unused resources -> Moudule ‘app’ -> OK,這樣會搜出來哪些未被使用到未使用到xml和圖片,如下:

經過刪除,APK大小 10891 KB。

刪除未使用到代碼

同樣使用Android Studio的Lint,步驟:點擊菜單欄 Analyze -> Run Inspection by Name -> unused declaration -> Moudule ‘app’ -> OK

經過一番清理,APK大小 10880 KB,才瘦了11KB,我以爲能瘦很多,不過,平時必須保持良好的編碼習慣,哪些沒有使用到的代碼還是刪刪掉吧,不要想着以後還會用到。

重複操作上一步和這一步,會有意外的發現哦。

png圖片格式轉成jpg

將一些大圖的格式轉成jpg格式,將會有效減小圖片的體積,我的APP大圖不是很多。

更改格式後,APK大小 10762 KB。

使用vector

微言是我很早就已經做得app,一開始圖標都是圖片,且每個drawable都有相應尺寸的圖標,佔了不少大小,後來部分用vector代替了圖片。注意一點,Button在xml裏設置app:srcCompat居然無效,只能在代碼裏設置:

button.setImageResource(R.drawable.ic_favorite_border_black_24dp)。

APK大小 10754 KB。

使用shape作爲背景

很多點擊效果可能會使用到圖片,可以換成shape是實現,我查看了下,微言APP都是shape實現的,Good。

使用TinyPng

如果對png圖片格式轉成jpg,減小效果還不滿意,直接使用TinyPng智能有損壓縮技術,網址:https://tinypng.com/ ,壓縮效果:

對於圖片資源很多的APP,APK瘦身效果不要太好。

APK大小 10341 KB。

配置resConfigs

如果APP支持中文,可以配置resConfigs,只支持中文

android {
defaultConfig {
...
//語言資源,只支持中文
resConfigs "zh"
}
}

APK大小 10243 KB。

使用微信Android資源混淆工具

微信AndResGuard是一個幫助你縮小APK大小的工具,詳情:Android資源混淆工具使用說明
使用方法:

Project/build.gradle

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.0'
}
}

app/build.gradle

apply plugin: 'AndResGuard'
def supportVersion = "25.0.0"
android {
...
signingConfigs {
release {
storeFile file('keystore/android.keystore')
storePassword '123456'
keyAlias 'android.keystore'
keyPassword '123456'
}
debug {
storeFile file('keystore/android.keystore')
storePassword '123456'
keyAlias 'android.keystore'
keyPassword '123456'
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}
repositories {
flatDir {
dirs 'aars'
}
}
andResGuard {
// mappingFile = file("./resource_mapping.txt")
//mappingFile用於增量更新,保持本次混淆與上次混淆結果一致;
mappingFile = null
//uss7zip爲true時,useSign必須爲true;
use7zip = true
//useSign爲true時,需要配置signConfig;
useSign = true
//打開這個開關,會keep住所有資源的原始路徑,只混淆資源的名字;
keepRoot = false
//whiteList添加在代碼內部需要動態獲取的資源id,不混淆這部分;
whiteList = [
// for your icon
"R.drawable.icon",
// for fabric
"R.string.com.crashlytics.*",
// for umeng update
"R.string.umeng*",
"R.string.UM*",
"R.string.tb_*",
"R.layout.umeng*",
"R.layout.tb_*",
"R.drawable.umeng*",
"R.drawable.tb_*",
"R.anim.umeng*",
"R.color.umeng*",
"R.color.tb_*",
"R.style.*UM*",
"R.style.umeng*",
"R.id.umeng*",
// umeng share for sina
"R.drawable.sina*",
// for google-services.json
"R.string.google_app_id",
"R.string.gcm_defaultSenderId",
"R.string.default_web_client_id",
"R.string.ga_trackingId",
"R.string.firebase_database_url",
"R.string.google_api_key",
"R.string.google_crash_reporting_api_key",
// umeng share for facebook
"R.layout.*facebook*",
"R.id.*facebook*",
// umeng share for messager
"R.layout.*messager*",
"R.id.*messager*",
// umeng share commond
"R.id.progress_bar_parent",
"R.id.webView"
]
//用來指定文件重打包時是否壓縮指定文件;
compressFilePattern = [
"*.png",
"*.jpg",
"*.jpeg",
"*.gif",
"resources.arsc"
]
//sevenzip可使用artifacr或path,path指本地安裝的7za(7zip命令行工具)。
sevenzip {
artifact = 'com.tencent.mm:SevenZip:1.2.0'
//path = "/usr/local/bin/7za"
}
}

AndResGuard打包命令行:gradlew resguardRelease,最終的混淆APK會生成在{App}/build/output/apk/AndResGuard目錄下。

APK大小 10035 KB= 9.79 MB。

結尾


最終APK瘦身 11.43 - 9.79 = 1.63 MB,瘦身效果不是很明顯,說明:
1、我的這個APP畢竟不是商業項目,本身就不大;
2、我的編碼習慣可以,沒有多少冗餘代碼和無用資源,哈哈。
大家可以將你們的APK按照這些方法過一遍,看能瘦多少。
瘦身還有其他方法,生命不息,折騰不止。

參考

官方文檔Reduce APK Size
https://developer.android.google.cn/topic/performance/reduce-apk-size.html

Android APP終極瘦身指南
http://jayfeng.com/2016/03/01/Android-APP%E7%BB%88%E6%9E%81%E7%98%A6%E8%BA%AB%E6%8C%87%E5%8D%97/

AndResGuard資源混淆
http://www.jianshu.com/p/7ffea26c9fd8

發佈了14 篇原創文章 · 獲贊 35 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章