使用 ProGuard 混淆私有字段

Obfuscate private fields using ProGuard

我在 AndroidStudio 1.2.1.1 和 Gradle 1.2.3.

中使用 ProGuard

我的 Gradle 的发布版本配置如下:

minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
shrinkResources true

我希望 类 的私有字段被混淆。

这是我目前的混淆器配置文件(经过多次尝试):

-allowaccessmodification
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''
-verbose
[...]

但我最终在使用 androdd 从 AndroidGuard 进行反编译后得到了:

private com.google.android.gms.common.api.GoogleApiClient googleApiClient;

我知道这种混淆的使用是有限的,但我希望 googleApiClient 被 ProGuard 重命名。如何操作?

这里是 refcard.

有什么办法可以做到-keepclassmembernames的相反操作吗?

由此得出: How to tell ProGuard to keep private fields without specifying each field

According to ProGuard documenation the wildcard matches any field.

最重要的是,您可以使用否定符 (!)。 (http://proguard.sourceforge.net/#manual/usage.html)

Attribute names can contain ?, *, and ** wildcards, and they can be preceded by the ! negator.

我在这方面不是很有经验,所以这是一个猜测,但更容易写在新的评论中。像这样的东西应该可以完成工作(未测试):

-keepclassmembers class * { //should find all classes 
    !private <fields>;    
    <methods>;
    <init>; //and keep every field, method, constructor apart from private fields
}

也许你可以这样使用,但首先不确定它如何与否定符一起使用:

-keepclassmembers class * { //should find all classes 
    !private <fields>;    
    *; //should exclude everything except private fields, which should be obfuscated.
}