Android 正确覆盖proguard选项

Android 正确覆盖proguard选项,android,proguard,Android,Proguard,我在当前项目中使用ProGuard,并决定尝试优化android配置(使用gradle): 我没有找到任何关于proguard和android版本所执行的与之兼容的优化的清晰文档: -optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* 如果app min sdk版本为11,它们是否为最新版本 所以我决定覆盖它,在proguard规则中尝试一下。pro

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

我没有找到任何关于proguard和android版本所执行的与之兼容的优化的清晰文档:

-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* 
如果app min sdk版本为11,它们是否为最新版本

所以我决定覆盖它,在
proguard规则中尝试一下。pro

-optimizations **
-printconfiguration "result.pro"
但最终的配置并不像我预期的那样。它包含所有合并的规则:

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

那么,如何在ProGuard中正确地覆盖该选项呢?或者这一行是否等于
-优化**

我花了一点时间反复尝试,但最终还是找到了答案。例如,要覆盖默认的ProGuard优化,请应用除代码/简化/算术之外的所有内容,然后:

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

    -optimizations !code/simplification/arithmetic,*
    
    表示“启用除代码/简化/算术之外的所有优化”。中提供了可用优化列表,您可以使用
    *
    启用/禁用优化类(例如
    !field/*

  • 您必须通过交换Gradle文件中
    ProGuard rules.pro
    getdefaultproguard file('ProGuard-android.txt')
    的顺序,确保在默认ProGuard文件之前加载您的ProGuard规则文件,以便
    ProGuard rules.pro
    首先出现:

    buildTypes {
      release {
        minifyEnabled false
        proguardFiles 'proguard-rules.pro', getDefaultProguardFile('proguard-android.txt')
      }
    }
    
  • 现在,输出应该如下所示:

    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
    
    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