Question

I am new in android application. I write an application and need to obfuscate it in eclipse. I tried to use following tutorial:

http://developer.android.com/tools/help/proguard.html

but when i created my project the eclipse did not produce any "proguard.cfg" file. I just have a "project.properties" that uncomment the following line of that.

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

I put instead of {sdk.dir} path to sdk. I don't have progurd-android.txt as well. is there any clear step by step for beginner how to obfuscate code in android? or is it possible to you to explain it for me? Thanks for help.

Was it helpful?

Solution

First, you should comment out this line on project.ptoperties:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

Then you should write your rules to proguard-project.txt This is an example from http://proguard.sourceforge.net/#manual/examples.html:

-injars      bin/classes
-injars      libs
-outjars     bin/classes-processed.jar
-libraryjars /usr/local/java/android-sdk/platforms/android-9/android.jar

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
} 

And then you should sign and export your apk. If you dont sign obfuscation does not work.

OTHER TIPS

The tutorial is not up to date. The proguard-project.txt is the correct proguard config file (which used to be "proguard.cfg" ).

I don't know though what the proguard-android.txt is supposed to be... In my project.properties it only says:

"proguard.config=proguard-project.txt" (proguard.project.txt is in the same folder as the the properties file) and it works...

To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the proguard.config property in the project.properties file.

proguard.config=proguard.cfg

Add this to a file called proguard.cfg to your projects root folder. It' s the basic configuration from the example (proguard-android.txt).

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

-dontoptimize
-dontpreverify

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}


-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-dontwarn android.support.**

Then run your application.

Proguard configuration is not easy especially when you have several libraries.

See the documentation here Proguard and on the android developers page Android Proguard Description.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top