记一次AndResGuard的使用

Github:https://github.com/shwenzhang/AndResGuard

 

下面是项目内配置:

//工程gradle下添加
dependencies {
      
        classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.16'
   
}

新建一个and_res_guard.gradle(与app的build.gradle同一级别)

apply plugin: 'AndResGuard'

andResGuard {
    // mappingFile = file("./resource_mapping.txt")
    mappingFile = null


   //Do NOT enable 7zip compression(use7zip) when you distribute your APP on Google         
   //Play. It'll prevent the file-by-file patch when updating your APP.
    use7zip = false

    useSign = true
    // it will keep the origin path of your resources when it's true
    keepRoot = false

    whiteList = [
            // your icon
            //表情
            "R.drawable.face00",
            "R.drawable.face01",
            "R.drawable.face02",
            "R.drawable.face03",
           
            // for fabric
            "R.string.com.crashlytics.*",
            // for google-services
            "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"
    ]
    compressFilePattern = [
            "*.png",
            "*.jpg",
            "*.jpeg",
            "*.gif",
//Do NOT add resource.asrc into compressFilePattern unless the app size is really matter //to you.
            
    ]
    sevenzip {
        artifact = 'com.tencent.mm:SevenZip:1.2.15'
        //path = "/usr/local/bin/7za"
    }

    /**
     * Optional: if finalApkBackupPath is null, AndResGuard will overwrite final apk
     * to the path which assemble[Task] write to
     **/
    // finalApkBackupPath = "${project.rootDir}/final.apk"

    /**
     * Optional: Specifies the name of the message digest algorithm to user when digesting the entries of JAR file
     * Only works in V1signing, default value is "SHA-1"
     **/
    // digestalg = "SHA-256"
}

然后在app的build.gradle下添加   apply from: 'and_res_guard.gradle'

注意:

如果代码中使用了

int resId = application.getResources().getIdentifier(res_name, "drawable", application.getPackageName());

是获取不到的,需要将res_name的图片加入白名单,就像我上面的配置

 

配置完成就可以打包了:

先build一下,打开下图

编译resguardRelease 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Kotlin中进行App瘦身可以采取以下几种方法: 1. 使用ProGuard进行代码混淆和优化。ProGuard是一个Java字节码优化器,可以删除未使用的代码和资源,减小应用的体积。在Kotlin项目中,可以通过在build.gradle文件中配置ProGuard规则来启用它。例如: ```kotlin android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } ``` 2. 使用R8进行代码压缩。R8是Google推出的一种代码压缩工具,可以在编译时删除未使用的代码和资源,减小应用的体积。在Kotlin项目中,默认情况下,R8已经启用,无需额外配置。 3. 优化资源文件。可以通过使用WebP格式替换PNG格式的图片,使用Vector Drawable替换多个分辨率的位图,以及压缩和优化其他资源文件来减小应用的体积。 4. 使用动态特性模块化。将应用的功能模块化,只在需要的时候下载和安装相应的模块,可以减小应用的初始安装包体积。 5. 使用APK分包。将应用的代码和资源分成多个APK文件,按需下载和安装,可以减小应用的初始安装包体积。 6. 使用资源压缩工具。可以使用工具如AndResGuard对资源文件进行压缩和优化,减小应用的体积。 7. 使用动态加载技术。将一部分代码和资源放在服务器上,通过动态加载的方式在运行时下载和加载,可以减小应用的初始安装包体积。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值