正确覆盖proguard选项

时间:2015-04-15 07:15:39

标签: android proguard

我正在使用当前项目中的ProGuard,并决定尝试优化android配置(使用gradle):

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

我没有找到任何关于proguard和android版本执行的优化的明确文档,这些版本与它们兼容:

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* 

如果app min sdk版本是11,它们是最新的吗?

所以我决定覆盖它,试试proguard-rules.pro

-optimizations **
-printconfiguration "result.pro"

但最终配置并不像我预期的那样。它包含所有合并的规则:

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*,**

那么如何在ProGuard中正确覆盖选项?或者这行可能等于-optimizations **

1 个答案:

答案 0 :(得分:5)

它花了我一些试验和错误,但最终发现了。要覆盖默认的ProGuard优化,例如,应用除code/simplification/arithmetic之外的所有内容:

  1. 在ProGuard文件中添加-optimizations行,并使用*代表所有。例如,以下行:

    -optimizations !code/simplification/arithmetic,*
    

    表示“启用除code/simplification/arithmetic之外的所有优化”。 the official website中提供了可用优化列表,您可以使用*启用/禁用优化类(例如!field/*)。

  2. 您必须确保在默认ProGuard文件之前加载ProGuard规则文件,方法是在Gradle文件中交换proguard-rules.progetDefaultProguardFile('proguard-android.txt')的顺序,以便proguard-rules.pro首先出现:

    buildTypes {
      release {
        minifyEnabled false
        proguardFiles 'proguard-rules.pro', getDefaultProguardFile('proguard-android.txt')
      }
    }
    
  3. 输出现在看起来应该是这样的:

    Optimizing...
      Number of finalized classes:                 68
      Number of unboxed enum classes:              1
      Number of vertically merged classes:         0
      Number of horizontally merged classes:       3
      Number of removed write-only fields:         0   (disabled)
      Number of privatized fields:                 58
      Number of inlined constant fields:           375
      Number of privatized methods:                13
      Number of staticized methods:                37
      Number of finalized methods:                 210
      Number of removed method parameters:         290
      Number of inlined constant parameters:       236
      Number of inlined constant return values:    239
      Number of inlined short method calls:        35
      Number of inlined unique method calls:       114
      Number of inlined tail recursion calls:      0
      Number of merged code blocks:                4
      Number of variable peephole optimizations:   723
      Number of arithmetic peephole optimizations: 10
      Number of cast peephole optimizations:       0
      Number of field peephole optimizations:      0
      Number of branch peephole optimizations:     42
      Number of string peephole optimizations:     35
      Number of simplified instructions:           369
      Number of removed instructions:              5019
      Number of removed local variables:           154
      Number of removed exception blocks:          0
      Number of optimized local variable frames:   201