Android ProGuard:最积极的优化 [英] Android ProGuard: Most Aggressive Optimizations

查看:391
本文介绍了Android ProGuard:最积极的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的官方 proguard文档显示了两个主要的优化:

Android's official proguard documentation shows two primary optimizations:

  • minifyEnabled设置为true
  • 使用proguard-android-optimize.txt 代替proguard-android.txt
  • set minifyEnabled to true
  • use proguard-android-optimize.txt instead of proguard-android.txt

这两个设置是否最具侵略性?

Are these two the most aggressive possible settings?

我正在编写一个android库,需要确保当人们使用我的库时,我的代码不会中断. (我知道我可以在我的库中放一些规则,以对抗使用该库的应用程序上设置的proguard配置,但是如果不需要的话,我不想这样做.)

I am writing an android library and need to make sure when people use my library that my code doesn't break. (I know there are rules I can put in my library to counter the proguard configuration set on the app that uses the library, but I don't want to do that if I don't have to.)

推荐答案

请记住,最佳的ProGuard配置-带有最少例外的配置. 在例外情况下,我了解:

Remember that the best ProGuard configuration - is a configuration with a minimum of exceptions. Under the exceptions I understand:

 -keepclassmembers class * extends android.content.Context {
    public void *(android.view.View);
    public void *(android.view.MenuItem);
 }

让我们遍历proguard-android-optimize.txt并查看优化/混淆选项.

Let's walk through proguard-android-optimize.txt and look on optimisation/obfuscation options.

有关ProGuard选项的详细说明,我使用

For detailed description of ProGuard options I use this

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* -可能的优化列表!表示否定,因此不使用此优化

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* This - list of possible optimisation, ! mean negate, so this optimization is not used

-optimizationpasses 5 指定要执行的优化遍数.默认情况下,执行一次通过.多次通过可能会导致进一步的改进.如果在优化通过之后未发现任何改进,则优化将结束.仅在优化时适用.
用法:确定,看起来默认5次通过就足够了

-optimizationpasses 5 Specifies the number of optimization passes to be performed. By default, a single pass is performed. Multiple passes may result in further improvements. If no improvements are found after an optimization pass, the optimization is ended. Only applicable when optimizing.
Usage: OK, and looks like that default 5 passes is enough

-allowaccessmodification 指定在处理过程中可以扩大类和类成员的访问修饰符.这样可以改善优化步骤的结果.
用法:确定,是的,看起来像是优化优化

-allowaccessmodification Specifies that the access modifiers of classes and class members may be broadened during processing. This can improve the results of the optimization step.
Usage: OK, yes looks like improve optimization

-dontpreverify 以Android为目标时,无需进行预验证,因此请不要进行预验证将其关闭以减少处理时间.但是此选项不会影响代码的不可破解性.
用法:确定,只需一点时间就可以节省处理时间

-dontpreverify When targeting Android, preverifing is not necessary, so dontpreverify turn it off to reduce the processing time a bit. But this option is not impact on unbreakability of code.
Usage: OK, just to little bit reduse processing time

-dontusemixedcaseclassnames 指定在混淆时不生成大小写混合的类名.默认情况下,混淆的类名可以包含大写字符和小写字符的混合.这将创建出完全可接受且可用的罐子.
用法:可疑,我找不到添加此选项的确切原因,但看起来将类名从abcdef更改为AbSdEf不会使代码坚不可摧

-dontusemixedcaseclassnames Specifies not to generate mixed-case class names while obfuscating. By default, obfuscated class names can contain a mix of upper-case characters and lower-case characters. This creates perfectly acceptable and usable jars.
Usage: QUESTIONABLE, I can't find exact reason why this option added, but looks like change class name from abcdef to AbSdEf doesn't make code unbreakable

-dontskipnonpubliclibraryclasses 指定不忽略非公共库类.从4.5版开始,这是默认设置.
用法:确定,非常有用

-dontskipnonpubliclibraryclasses Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting.
Usage: OK, very useful

proguard-android-optimize.txt不包含以下选项:

The following options aren't include to proguard-android-optimize.txt:

-mergeinterfacesaggressively 指定即使接口的实现类未实现所有接口方法,接口也可以合并...设置此选项可能会降低某些JVM上已处理代码的性能
用法: BAD ,对于Android来说很危险,未包含在配置中,禁止类/合并/优化的总结

-mergeinterfacesaggressively Specifies that interfaces may be merged, even if their implementing classes don't implement all interface methods... setting this option can reduce the performance of the processed code on some JVMs
Usage: BAD, look dangerous for Android, don't included into config, sumular of prohibition of class/merging/ in optimizations

-overloadaggressively 指定在混淆时应用主动重载.只要Java字节码要求的参数和返回类型不同(而不只是Java语言要求的参数),多个字段和方法就可以使用相同的名称.
用法: BAD ,Google的Dalvik VM无法处理重载的静态字段.

-overloadaggressively Specifies to apply aggressive overloading while obfuscating. Multiple fields and methods can then get the same names, as long as their arguments and return types are different, as required by Java bytecode (not just their arguments, as required by the Java language)
Usage: BAD, Google's Dalvik VM can't handle overloaded static fields.

因此,我只知道混淆和非危险选项中有用的一种:
-repackageclasses ''

-repackageclasses '' 指定通过将所有重命名的类文件移动到单个给定的包中来重新打包它们.不带参数或带空字符串('')的包将被完全删除.此选项将覆盖-flattenpackagehierarchy选项.
用法:确定,由Google使用,因此看来我们至少找到了可以添加到配置中的选项

-repackageclasses '' Specifies to repackage all class files that are renamed, by moving them into the single given package. Without argument or with an empty string (''), the package is removed completely. This option overrides the -flattenpackagehierarchy option.
Usage: OK, Used by Google, so look like we at least have found the option which we can add to our config

也请注意有关解码堆栈跟踪的信息. ProGuard还从堆栈跟踪中删除文件名和行号.这使得发现错误非常复杂.您可以通过将以下代码添加到配置中来保留行号:

Also please notice about decoding stack traces. ProGuard also removes the filename and line numbers from the stacktrace. This makes finding errors very complicated. You can keep the line numbers by adding the following code to your config:

-renamesourcefileattribute SourceFile 
-keepattributes SourceFile,LineNumberTable

这将保留行号,但将堆栈跟踪中的文件名替换为"SourceFile".

This will keep the line numbers but replace the filename in the stacktrace with "SourceFile".

也不要忘记ProGuard看起来很脆弱,因为它不加密字符串资源,因此请考虑使用DexGuard或自己加密重要的字符串(例如令牌,URL).

Also do not forget that the ProGuard looks vulnerable because it does not encrypt the string resources, so consider using DexGuard or encrypt important strings (like tokens, urls) themselves.

这篇关于Android ProGuard:最积极的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆