APK瘦身,APK资源对齐优化-AndResGuard编译集成

时间:2024-05-20 11:45:33

项目中通常用到代码混淆,添加不混淆代码块,为生成的apk中减少内存占用以及反编译apk后代码可读性

但是资源文件还是比较好拿到的。所以集成AndResGuard

集成方式:

1.在项目根build.gradle的buildscript --> dependencies中添加

//andresguard
classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.15'
buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        //andresguard
        classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.15'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    repositories {
        mavenCentral()
    }
}

2.在app目录下新建andresguard.gradle

//---------APP瘦身 配置AndResGuard-----------
apply plugin: 'AndResGuard'

andResGuard {
    mappingFile = null
    use7zip = true
    useSign = true
    keepRoot = false
    compressFilePattern = [
            "*.png",
            "*.jpg",
            "*.jpeg",
            "*.gif",
    ]
    //白名单,主要第三方的不混淆
    whiteList = [
            // your icon
            "R.drawable.icon",
            "R.mipmap.app_icon",
            //极光推送
            "R.drawable.jpush_notification_icon"
    ]
    //配置7zip压缩
    sevenzip {
        //artifact = 'com.tencent.mm:SevenZip:1.2.10'//配置线上7zip地址
        path = ‘/usr/local/bin/7za’//mac下brew安装7zip后的地址
    }

    /**
     * 可选: 如果不设置则会默认覆盖assemble输出的apk
     **/
    if (IS_JENKINS.toBoolean())
        finalApkBackupPath = "${project.rootDir}/app/build/outputs/apk/andresguard_signed.apk"

    /**
     * 可选: 指定v1签名时生成jar文件的摘要算法
     * 默认值为“SHA-1”
     **/
    // digestalg = "SHA-256"
}
//---------配置AndResGuard-----------

3.在app的build.gradle中添加

apply from: 'andresguard.gradle'

 

后续建议使用打包命令

./gradlew assembleRelease  --> ./gradlew resguardRelease

./gradlew assembleYingyongbaoRelease  --> ./gradlew resguardYingyongbaoRelease

......

命令参照

APK瘦身,APK资源对齐优化-AndResGuard编译集成