android studio apk 瘦身 工具 APK Analyzer和ProGuard

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:

dump.txt
Describes the internal structure of all the class files in the APK.
mapping.txt
Provides a translation between the original and obfuscated class, method, and field names.
seeds.txt
Lists the classes and members that were not obfuscated.
usage.txt
Lists the code that was removed from the APK.

These files are saved at /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'
        }
    }
}





你可能感兴趣的:(android)