2015-05-29 133 views
14

我在AndroidStudio 1.2.1.1中使用ProGuard和Gradle 1.2.3。使用ProGuard混淆私人领域

我的摇篮的发行版本配置,像这样:

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

我想类的私有字段被扰乱。

这里是我的ProGuard配置文件(多次尝试之后)截至目前:

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

但我最终与androdd从AndroidGuard反编译后,有:

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

我知道使用这种混淆是有限的,但我想由ProGuard更名。如何做?

这里是refcard

有没有办法做到-keepclassmembernames的反义词?

+0

是googleApiClient唯一是非模糊字段还是所有字段都是一样的? –

+1

我想为所有私人领域。 – shkschneider

回答

8

从这个入门: How to tell ProGuard to keep private fields without specifying each field

根据ProGuard documenation通配符任何 场比赛。

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

属性名称可以包含?,*,**和通配符,并且它们可以 其前面!否定器。

我不是那么有经验的这个领域,所以这是一个猜测,但更容易写在一个新的评论。这样的事情应该做的工作(未测试):

-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. 
} 
+0

我用过'-keepclassmembers类my.package {!private ;保护;公众; ; }'并得到我想要的:名称为'a','b','c'等的字段。我知道这与取消者有关,这要感谢他引导我正确使用。我会根据我的个人用法进行改进,但这是我正在寻找的答案。 – shkschneider

+0

这里是我最后一个ProGuard文件,它工程bug bug#78377:https://gist.github.com/shkschneider/a1c81780cd1f35a7037d – shkschneider

+0

谢谢你的回应和奖励! 我很高兴你根据我的提示制定了出来,也感谢你加入github,这对我来说也是一次很好的体验。 – czupe