1.发布APK,看到包超过了30M,一下就揪心了,可以使用Analyzer查看包中的哪些文件比较大,可以使用Analyzer,官网上有详细的介绍
https://developer.android.com/studio/command-line/apkanalyzer.html
2.在打包前希望移除掉没有用的代码,那就需要ProGuard,这块官网介绍也十分详细:https://developer.android.com/studio/build/shrink-code.html
Proguard 作用和用法,每次用第三方库为什么混淆的时候会是那样写,看了这个也许你再也不会懵
https://stuff.mit.edu/afs/sipb/project/android/sdk/android-sdk-linux/tools/proguard/docs/index.html#manual/introduction.html
具体用法:
To enable code shrinking with ProGuard, add minifyEnabled true
to the appropriate build type in your build.gradle
file.
android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } ... }
具体哪些地方给你瘦身了,请看以下文件
With each build, ProGuard outputs the following files:
- Describes the internal structure of all the class files in the APK.
- Provides a translation between the original and obfuscated class, method, and field names.
- Lists the classes and members that were not obfuscated.
- Lists the code that was removed from the APK.
dump.txt
mapping.txt
seeds.txt
usage.txt
These files are saved at <module-name>/build/outputs/mapping/release/
.
3.不想用ProGuard:
useProguard false
去掉资源,官网:https://developer.android.com/studio/build/shrink-code.html,写得很详细
具体用法:
android { ... buildTypes { release { shrinkResources true minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }