Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obfuscate private fields using ProGuard

I'm using ProGuard in AndroidStudio 1.2.1.1 with Gradle 1.2.3.

My Gradle's release build is configured like so:

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

I would like the private fields of classes to be obfuscated.

Here is my proguard config file (after many tries) as of now:

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

But I end up, after decompiling with androdd from AndroidGuard, with:

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

I know the use of this obfuscation is limited, but I would like googleApiClient to be renamed by ProGuard. How to do so?

Here is the refcard.

Is there any way to do the opposite of -keepclassmembernames?

like image 554
shkschneider Avatar asked May 29 '15 09:05

shkschneider


People also ask

Does ProGuard obfuscate code?

You can obfuscate Android code to provide security against reverse engineering. You can use the Android ProGuard tool to obfuscate, shrink, and optimize your code.

How does ProGuard obfuscation work?

Simply by enabling Proguard, we at Gradeup reduced our app size by a whooping 40%! It obfuscates the code, which means that it renames classes, fields, and methods with semantically obscure names that, in addition to making the codebase smaller and more efficient, also makes it difficult to reverse engineer the app.

Does ProGuard obfuscate package name?

Obfuscating package names myapplication. MyMain is the main application class that is kept by the configuration. All other class names can be obfuscated. Note that not all levels of obfuscation of package names may be acceptable for all code.


1 Answers

Getting from this: How to tell ProGuard to keep private fields without specifying each field

According to ProGuard documenation the wildcard matches any field.

Top of that, You can use negators (!). (http://proguard.sourceforge.net/#manual/usage.html)

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

I am not so experienced in this field, so rather it is a guess, but easier to write in a new comment. Something like this should do the job (NOT TESTED):

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

May be you can use like this, but do not sure how it works with a negator first:

-keepclassmembers class * { //should find all classes 
    !private <fields>;    
    *; //should exclude everything except private fields, which should be obfuscated.
}
like image 168
czupe Avatar answered Oct 12 '22 23:10

czupe