Proguard揭秘:了解和将Proguard集成到您的Android应用中的简化指南

If you have worked on end to end development of an Android app, you are bound to have crossed paths with the magical tool called Proguard! If you haven’t or are having trouble wrapping your head around it, worry not; this article is here to help! By the end of it, you should have a fairly decent idea of what Proguard is and how to seamlessly integrate it with your Android app to reap its benefits.

如果您从事Android应用程序的端到端开发工作,那么势必会使用名为Proguard的神奇工具克服困难 ! 如果您没有或无法将头缠绕在头上,请不要担心。 本文对您有所帮助 ! 到最后,您应该对Proguard是什么以及如何将其与Android应用程序无缝集成以获取其好处有一个相当不错的想法。

So let’s delve straight into it and demystify the air that surrounds it!

因此,让我们直接深入研究它,并使周围的空气变得神秘!

什么是Proguard? (What is Proguard?)

ProGuard is a free Java tool in Android that performs three major roles:

ProGuard是Android中的免费Java工具,具有三个主要作用:

1.缩小/缩小代码(删除项目中未使用的代码) (1. Shrink/minify the code (remove unused code in the project))

  • Code shrinking (or tree-shaking): detects and safely removes unused classes, fields, methods, and attributes from your app and its library dependencies e.g. if you use only a few APIs of a library dependency, shrinking can identify library code that your app is not using and remove only that code from your app.

    代码收缩(或摇树):检测并安全地从您的应用及其库依赖项中删除未使用的类,字段,方法和属性,例如,如果您仅使用几个具有库依赖项的API,则收缩可以识别您的应用程序所使用的库代码没有使用,仅从您的应用中删除该代码。

  • Resource shrinking: removes unused resources from your packaged app, including unused resources in your app’s library dependencies. It works in conjunction with code shrinking such that once unused code has been removed, any resources no longer referenced can be safely removed as well.

    资源缩减:从打包的应用程序中删除未使用的资源,包括应用程序库依赖项中的未使用资源。 它与代码缩减结合使用,以便一旦删除了未使用的代码,就可以安全地删除不再引用的任何资源。

2.混淆代码(重命名类,字段等的名称) (2. Obfuscate the code (rename the names of class, fields, etc.))

Renames the remaining classes, fields, and methods using short meaningless names.

使用简短的无意义名称重命名其余的类,字段和方法。

3.优化代码 (3. Optimise the code)

Inspects and rewrites your code to further reduce the size of your app’s DEX files. For example, if Proguard detects that the else {} branch for a given if/else statement is never taken, it removes the code for the else {} branch.

检查并重写代码,以进一步减小应用程序的DEX文件的大小。 例如,如果Proguard的检测到else {}对于给定的if / else语句是从来没有,它消除了对代码分支else {}分支。

好的,但是它对我的应用有什么好处? (Alright, but how does it benefit my app?)

  • It removes the unused code and resources from your project (including the library dependencies) and hence has a massive impact on the app size! You can think of Proguard as a vacuum sealer for your Android app- it sucks out the unnecessary elements and delivers a much compact package that is un-compromised in terms of value. Simply by enabling Proguard, we at Gradeup reduced our app size by a whooping 40%!

    从项目中删除了未使用的代码和资源 (包括库依赖项),因此对应用程序大小产生了巨大影响! 您可以将Proguard视为适用于Android应用程序真空封口机-它吸收了不必要的元素,并提供了一个非常紧凑的包装,在价值方面毫不妥协。 只需启用Proguard,我们Gradeup的应用程序大小便减少了40%!

  • It obfuscates the code, which means that it renames classes, fields, and methods with semantically obscure names that, in addition to making the codebase smaller and more efficient, also makes it difficult to reverse engineer the app.

    它混淆了代码,这意味着它使用语义上模糊的名称重命名了类,字段和方法,这不仅使代码库更小,更有效,而且还使得对应用程序进行反向工程变得困难。

我卖了! 如何启用它? (I’m sold! How do I enable it?)

Add the following to project level build.gradle file:

将以下内容添加到项目级别的build.gradle文件中:

android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android.txt'),
'proguard-rules.pro'
}
}
...
}

Please note the following things to make Proguard work in perfect conjunction with your app:

请注意以下几点,以使Proguard与您的应用完美结合:

  • Proguard should ideally be used only in release mode. It is not recommended to enable Proguard for Debug builds as it increases build time and makes debugging cumbersome.

    理想情况下,Proguard应该仅在发布模式下使用。 不建议启用Proguard for Debug构建,因为它会增加构建时间并增加调试的麻烦。

  • The minifyEnabled property in the build.gradle file enables and disables Proguard for release builds. Set minifyEnabled true to enable Proguard.

    build.gradle文件中的minifyEnabled属性为发布版本启用和禁用Proguard。 将minifyEnabled设置为true可启用Proguard。

  • The getDefaultProguardFile(‘proguard-android.txt’) method obtains the default Proguard settings from the Android SDK tools/proguard folder. If you want to optimize the Proguard, use the proguard-android-optimise.txt configuration file instead of proguard-android.txt, but you need to be wary as adding optimisation introduces certain risks.

    getDefaultProguardFile('proguard-android.txt' )方法从Android SDK tools / proguard文件夹获取默认的Proguard设置。 如果要优化Proguard,请使用proguard-android-optimise.txt配置文件,而不要使用proguard-android.txt,但是您需要警惕,因为添加优化会带来某些风险。

  • Android Studio adds the proguard-rules.pro file at the root of the module, in which you can add custom Proguard rules.

    Android Studio在模块的根目录中添加proguard-rules.pro文件,您可以在其中添加自定义Proguard规则。

您所说的Proguard规则是什么意思? (What do you mean by Proguard rules?)

Proguard rules formulate the boundaries for Proguard , giving you control over what files/classes/libraries to protect from being obfuscated, optimised or shrunk. You can add Proguard rules to the proguard-rules.pro file at the root of your module. Many common libraries are compatible with Proguard by default but there are some that require you to add rules explicitly. Such libraries provide the rules along with their documentation and a simple Google search will pull up those rules for you.

Proguard规则为Proguard设定了边界,使您可以控制要保护的文件/类/库,以免混淆,优化或缩小。 您可以将Proguard规则添加到模块根目录的proguard-rules.pro文件中。 默认情况下,许多通用库与Proguard兼容,但是有些库要求您显式添加规则。 此类库提供了规则及其文档,并且简单的Google搜索将为您提取这些规则。

Here are the proguard rules of some commonly used libraries.

以下是一些常用库的保护规则。

A typical Proguard rule looks something like this

一个典型的Proguard规则如下所示

-keep class io.reactivex.**{*;}

There are various “keep” directives available for use e.g.

有多种“保持”指令可供使用,例如

  • -keep: It disables Proguard of all its goodness! No shrinking, no obfuscation; not for classes, not for members.

    -keep:禁用Proguard的所有优点! 无收缩,无混淆; 不适合上课,不适合会员。

  • -keepclassmembers: This protects only the members of the class from shrinking and obfuscation.

    -keepclassmembers:这仅保护类的成员免于收缩和混淆。

  • -keepnames: This allows shrinking for classes and members, but not obfuscation

    -keepnames:允许缩小类和成员,但不能混淆

  • -keepclassmembernames: Unused classes are removed, the remaining classes are renamed; unused members of those classes are removed, but then the remaining members keep their original names.

    -keepclassmembernames:删除未使用的类,重命名其余的类; 这些类的未使用成员将被删除,但是其余成员将保留其原始名称。

Here is a very well explained article to understand the difference between these directives and to help you choose the one that suits your use-case.

是一篇很好解释的文章,以了解这些指令之间的区别,并帮助您选择适合您的用例的指令。

keepclassmembernames is the most recommended option and keep the least as it basically renders the Proguard useless for the item on which it is used.

keepclassmembernames是最推荐的选项,并保持 至少因为它基本上使Proguard对使用它的物品无用。

有什么办法可以查看Proguard删除的项目列表? (Is there any way I can see the list of items removed by Proguard?)

Android allows you to generate a report of removed (or kept) code as well as the mapping between obfuscated and original code. Following are the files which serve that purpose:

Android允许您生成已删除(或保留)的代码以及混淆代码与原始代码之间的映射的报告。 以下是用于该目的的文件:

  • dump.txt : Describes the internal structure of all the class files in the APK.

    dump.txt:描述APK中所有类文件的内部结构。

  • mapping.txt: Provides a translation between the original and obfuscated class, method, and field names.

    mapping.txt:提供原始类和混淆类,方法和字段名称之间的转换。

  • seeds.txt: Lists the classes and members that were not obfuscated.

    seed.txt:列出未混淆的类和成员。

  • usage.txt: Lists the code that was removed from the APK.

    usage.txt:列出从APK中删除的代码。

For each module that you want to generate usage.txt for, add

对于要为其生成usage.txt的每个模块,添加

-printusage <output-dir>/usage.txt

to your custom rules file.

到您的自定义规则文件。

If instead you want to see a report of the entry points that Proguard determines from your project’s keep rules , include

相反,如果您想查看Proguard从项目的保留规则确定的入口点的报告,请包括

-printseeds <output-dir>/seeds.txt

in your custom rules file, and likewise

在您的自定义规则文件中

When you build you app after adding the above mentioned lines of code, you can find these files in app’s (module’s)/build/outputs/mapping/release/ folder.

添加上述代码行后构建应用程序时,可以在应用程序(模块)的/build/outputs/mapping/release/ folder.找到这些文件/build/outputs/mapping/release/ folder.

但是手动分析这些文件不难吗? (But isn’t it hard to analyse these files manually?)

Yes! But thankfully, Android studio comes to your rescue! When you analyse your APK in Android studio, you can load the above generated proguard-mappings by clicking on the Load Proguard Mappings button, and selecting the folder in which the above generated files reside.

是! 但值得庆幸的是,Android Studio助您一臂之力! 在Android Studio中分析APK时,可以通过单击“ 加载Proguard映射”按钮并选择以上生成的文件所在的文件夹来加载以上生成的proguard映射。

Image for post

Once the mappings have been loaded, you can click on the show removed nodes button and all the files removed by Proguard will appear with a strikethrough in the list! What’s more? If you want to explicitly keep a file removed by Proguard, you can right click on it, select generate proguard rules, copy the desired rule and simply paste it in your proguard-rules.pro!

加载映射后,您可以单击“ 显示已删除的节点”按钮,并且Proguard删除的所有文件都将在列表中带有删除线! 更重要的是? 如果要明确保留Proguard删除的文件,可以右键单击它,选择生成proguard规则,复制所需的规则,然后将其简单地粘贴到proguard-rules.pro中!

Image for post

I highly recommend you watch this awesome Google IO video to better understand the whole process.

我强烈建议您观看这个很棒的Google IO视频,以更好地了解整个过程。

Aaaand that’s all(and a tad more) you need to get started with Proguard! You can also check Android’s official documentation for further details!

aa,这就是Proguard入门所需的全部(还有更多)! 您还可以查看Android的官方文档以了解更多详细信息!

Cheers to compact, secure apps!

为紧凑,安全的应用程序加油!

翻译自: https://medium.com/gradeup/demystifying-proguard-a-simplified-guide-to-understanding-and-integrating-proguard-in-your-fd3accae3238

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值