【Android 安装包优化】资源打包配置 ( resources.arsc 资源映射表 | 配置国际化资源 )
文章目錄
- 一、resources.arsc 資源映射表
- 二、配置國(guó)際化資源
- 三、完整 build.gradle 構(gòu)建腳本示例
- 四、參考資料
一、resources.arsc 資源映射表
分析 Android 應(yīng)用打包后的 APK 文件 , 打開 resources.arsc 文件 , 該文件是 Android 應(yīng)用的資源映射表 ,
點(diǎn)擊 string , 查看字符串資源 , 在 strings.xml 中定義的字符串 , 都在打包在了該位置 ;
在該資源映射表中的 string 字符串 , 包含了所有語(yǔ)言類型 , 浪費(fèi)了很多不必要的空間 ;
這些字符串很多都是國(guó)際化時(shí)用的 , 查看項(xiàng)目源碼 , 發(fā)現(xiàn) res 資源目錄中 , 并沒(méi)有進(jìn)行國(guó)際化 , 這些國(guó)際化資源都是隨著依賴庫(kù)引入而進(jìn)入到應(yīng)用中的 , 國(guó)際化資源最多的就是 androidx.appcompat:appcompat 依賴庫(kù) , 配置了所有國(guó)家語(yǔ)言的國(guó)際化資源 ;
二、配置國(guó)際化資源
在 build.gradle 構(gòu)建腳本中的 " android / defaultConfig " 層級(jí)配置 resConfigs ‘en’ , 配置后只打包默認(rèn)資源與英文資源 , 不會(huì)打包其它語(yǔ)言的國(guó)際化資源 , 最大限度節(jié)省空間 ;
android {defaultConfig {// 國(guó)際化資源配置, 只打包默認(rèn)資源與英文資源resConfigs 'en'} }配置完畢后 , 選擇 " 菜單欄 / Build / Build Bundle(s)/APK(s) / Build APK(s) " , 再次編譯生成 APK 安裝包 ;
此時(shí)就可以看到 APK 減小了 1MB\rm 1 MB1MB , 由 3.9MB\rm 3.9 MB3.9MB , 減小為 3.8MB\rm 3.8 MB3.8MB ;
原來(lái)的 resources.arsc 資源映射表文件 , 由 704.6KB\rm 704.6 KB704.6KB 減小為 366.9KB\rm 366.9 KB366.9KB ;
文件中幾十種語(yǔ)言的國(guó)際化資源只剩下一個(gè)默認(rèn)資源 ;
資源越多 , 該配置減小的體積就越多 ;
三、完整 build.gradle 構(gòu)建腳本示例
plugins {id 'com.android.application'id 'kotlin-android' }android {compileSdkVersion 30buildToolsVersion "30.0.3"defaultConfig {applicationId "kim.hsl.svg"minSdkVersion 18targetSdkVersion 30versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"// 生成 PNG 圖片配置//generatedDensities = ['hdpi', 'mdpi', 'xhdpi', 'xxhdpi', 'xxxhdpi']// 使用 com.android.support:appcompat 支持庫(kù)配置vectorDrawables.useSupportLibrary = true// 國(guó)際化資源配置, 只打包默認(rèn)資源與英文資源resConfigs 'en'}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}kotlinOptions {jvmTarget = '1.8'} }dependencies {implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"implementation 'androidx.core:core-ktx:1.3.2'// 矢量圖支持庫(kù) , 支持 5.0 以下版本手機(jī)使用矢量圖 , 這個(gè)是創(chuàng)建應(yīng)用時(shí)自帶的配置implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'com.google.android.material:material:1.3.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.+'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
四、參考資料
博客資源 :
-
GitHub 項(xiàng)目源碼 : https://github.com/han1202012/SVG
-
下載地址 : https://download.csdn.net/download/han1202012/18542570
總結(jié)
以上是生活随笔為你收集整理的【Android 安装包优化】资源打包配置 ( resources.arsc 资源映射表 | 配置国际化资源 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Android 安装包优化】Tint
- 下一篇: 【Android 安装包优化】动态库打包