39

GitHub - shwenzhang/AndResGuard: proguard resource for Android by wechat team

 5 years ago
source link: https://github.com/shwenzhang/AndResGuard
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

AndResGuard

Build Status Download Android Arsenal

Read this in other languages: English, 简体中文.

AndResGuard is a tooling for reducing your apk size, it works like the ProGuard for Java source code, but only aim at the resource files. It changes res/drawable/wechat to r/d/a, and renames the resource file wechat.png to a.png. Finally, it repackages the apk with 7zip, which can reduce the package size obviously.

AndResGuard is fast, and it does NOT need the source codes. Input an Android apk, then we can get a 'ResGuard' apk in a few seconds.

Some uses of AndResGuard are:

  1. Obfuscate android resources. It contains all the resource type(such as drawable、layout、string...). It can prevent your apk from being reversed by Apktool.

  2. Shrinking the apk size. It can reduce the resources.arsc and the package size obviously.

  3. Repackage with 7zip. It supports repackage apk with 7zip, and we can specify the compression method for each file.

AndResGuard is a command-line tool, it supports Windows, Linux and Mac. We suggest you to use 7zip in Linux or Mac platform for a higher compression ratio.

How to use

With Gradle

This has been released on Bintray

apply plugin: 'AndResGuard'

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.17'
    }
}

andResGuard {
    // mappingFile = file("./resource_mapping.txt")
    mappingFile = null
    use7zip = true
    useSign = true
    // It will keep the origin path of your resources when it's true
    keepRoot = false
    // If set, name column in arsc those need to proguard will be kept to this value
    fixedResName = "arg"
    // It will merge the duplicated resources, but don't rely on this feature too much.
    // it's always better to remove duplicated resource from repo
    mergeDuplicatedRes = true
    whiteList = [
        // your icon
        "R.drawable.icon",
        // 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",
    ]
    sevenzip {
        artifact = 'com.tencent.mm:SevenZip:1.2.17'
        //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"
}

Wildcard

The whiteList and compressFilePattern support wildcard include ? * +.

?	Zero or one character
*	Zero or more of character
+	One or more of character

WhiteList

You need put all resource which access via getIdentifier into whiteList. You can find more whitsList configs of third-part SDK in white_list.md. Welcome PR your configs which is not included in white_list.md

The whiteList only works on the specsName of resources, it wouldn't keep the path of resource. If you wanna keeping the path, please use mappingFile to implement it.

For example, we wanna keeping the path of icon, we need add below into our mappingFile.

res path mapping:
    res/mipmap-hdpi-v4 -> res/mipmap-hdpi-v4
    res/mipmap-mdpi-v4 -> res/mipmap-mdpi-v4
    res/mipmap-xhdpi-v4 -> res/mipmap-xhdpi-v4
    res/mipmap-xxhdpi-v4 -> res/mipmap-xxhdpi-v4
    res/mipmap-xxxhdpi-v4 -> res/mipmap-xxxhdpi-v4

How to Launch

If you are using Android Studio, you can find the generate task option in andresguard group. Or alternatively, you run ./gradlew resguard[BuildType | Flavor] in your terminal. The format of task name is as same as assemble.

Sevenzip

The sevenzip in gradle file can be set by path or artifact. Multiple assignments are allowed, but the winner is always path.

Result

If finalApkBackupPath is null, AndResGuard will overwrite final APK to the path which assemble[Task] write. Otherwise, it will store in the path you assigned.

Other

Looking for more detail

Known Issue

  1. The first element of list which returned by AssetManager#list(String path) is empty string when you're using the APK which is compressed by 7zip. #162

Best Practise

  1. Do NOT add resources.arsc into compressFilePattern unless the app size is really matter to you.(#84 #233)
  2. 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. (#233)

Thanks

Apktool Connor Tumbleson

v2sig @jonyChina162


Recommend

  • 94
    • 掘金 juejin.im 6 years ago
    • Cache

    ProGuard 在 Android 上的使用姿势

    为什么使用 ProGuard ProGuard 是一个压缩、优化、混淆代码的工具。尽管有很多其他工具供开发者们使用,但是 ProGuard 作为 Android Gradle 构建过程的一部分,已经打包在 SDK 中。 当我们构建应用时,使用 ProGuard 有很多好处。有的开发者更关心混...

  • 55

  • 50

    文章主体部分已经发表于《程序员》杂志2018年2月期,内容略有改动。 ProGuard简介 ProGuard是2002年由比利时程序员Eric Lafortune发布的一款优秀的开源代码优化、混淆工具,适用于Java和Android应用,目标是让程序更小,运

  • 51

    本文由玉刚说写作平台提供写作赞助 原作者:Sen 版权声明:本文版权归微信公众号玉刚说所有,未经许可,不得以任何形式转载 Proguard介绍 Proguard被人们熟知的是它的混淆功能,根据Proguard帮助文档的描述,Proguard可以对Java c

  • 32
    • www.tuicool.com 5 years ago
    • Cache

    Getting Started with ProGuard [FREE]

    There’s been a recent increase in popularity of Internet of Things, DIY boards and entry-level devices. The consequence has been a step back from writing large apps in favor of smaller ones. ProGuard is here...

  • 39

    README.md ProGuard, Java bytecode optimizer and obfuscator This distribution contains the following directories: bin : s...

  • 9

    May 11, 2021 Configuring ProGuard, an Easy Step-by-Step Tutorial Written by:...

  • 10
    • www.guardsquare.com 3 years ago
    • Cache

    Using ProGuard with the Android Gradle Plugin

    July 13, 2021 Using ProGuard with the Android Gradle Plugin Written by: Toon...

  • 7
    • my.oschina.net 2 years ago
    • Cache

    向工程腐化开炮 | proguard治理

    向工程腐化开炮 | proguard治理 - 阿里巴巴移动技术 - OSCHINA - 中文开源技术交流社区 作者:刘天宇(谦风) 工程腐化是app迭代过程中,一个非常棘手的问题,涉及到广泛而细碎的具体细节,对研发效能&体验、工程&产物质...

  • 9
    • frank-zhu.github.io 2 years ago
    • Cache

    编写Android Proguard File

    编写Android Proguard File 21 Nov 2014 ProGuard的常用语法 -libraryjars class_path 应用的依赖包,如android-support-v4-keep [,modifier,...] class_specification 不混淆某些类-keepclas...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK