How can I obfuscate my code in Eclipse? (Android) - android

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.

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.

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.

Related

Ant seems to ignore jar generated by Proguard while packaging apk

I have a Jenkins build server for building Android APKs with ant and Android (SDK Revision 18). Releasing APKs is working fine. However if i enable proguard by providing a proguard.cfg and pointing to it by "proguard.config=proguard.cfg" in the project.properties the following seems to happen:
Proguard is executed (as seen from shell output and the existence of mappings.txt and seeds.txt afterwards)
an apk is generated. However the Stacktraces generated by the APK are not obfuscated at all.
Anybody got an idee on this? It seems that the jar generated by proguard is for some reasons not included in the apk.
Here is my Proguard Config
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/,!class/merging/
-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.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-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.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 *;
}
#ACRA specifics
# we need line numbers in our stack traces otherwise they are pretty useless
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
# ACRA needs "annotations" so add this...
-keepattributes *Annotation*
# keep this class so that logging will show 'ACRA' and not a obfuscated name like 'a'.
# Note: if you are removing log messages elsewhere in this file then this isn't necessary
-keep class org.acra.ACRA {
*;
}
# keep this around for some enums that ACRA needs
-keep class org.acra.ReportingInteractionMode {
*;
}
# keep this otherwise it is removed by ProGuard
-keep public class org.acra.ErrorReporter
{
public void addCustomData(java.lang.String,java.lang.String);
}
# keep this otherwise it is removed by ProGuard
-keep public class org.acra.ErrorReporter
{
public org.acra.ErrorReporter$ReportsSenderWorker handleSilentException(java.lang.Throwable);
}
The mappings.txt states that my code is obfuscated:
de.cellular.crashtest.ObfuscateThisClass -> de.cellular.crashtest.b:
However it is not obfuscated in the reported Stacktrace
It seems it was just a jenkins configuration problem: the apk was archived from the bin/ directory but the obfuscated one was in release/

Proguard doesn't work when export with Eclipse ADT plugin R19

When I export my application through Export > Export Android Application or through Android Tools > Export Signed Application Package, I just cannot get Proguard work.
All I know about how to enable Proguard is to uncomment the line
proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt
in project.properties.
And I tried putting my configuration file there, too. Won't work either.
The classes.dex file in the exported package contains the name of class private methods such as onConfirmButtonClicked. So there's more than 99% chance that Proguard didn't do its work.
After that I put some random characters in the proguard-project.txt file, but I didn't get any error. The verbose output of the build didn't refer Proguard at all.
I wonder what's wrong there.
Proguard does its work when you export your project into an .apk file, and you should put the configuration you need in a file (.cfg) and you point to that file in your project.properties
proguard.config=${dir}\YOURCONFIGFILE.cfg
and in that config file you should tell proguard to keep all the crucial elements in your project. For exemple :
-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>;
}
Hope this helps!

Android with eclipse proguard.cfg problems

My application works fine until now.
I'm getting error like
Obsolete proguard file; use -keepclasseswithmembers instead of -keepclasseswithmembernames proguard.cfg
I haven't touched proguard.cfg file.
I have already cleaned my application and also reopened my application. Without luck.
Any ideas?
Regards
This is a bug with the SDK tools v11.
http://code.google.com/p/android/issues/detail?id=16384
When Lint generates error,
go to lint warnings view (Window > Show View > Other > android > Lint Warnings) and double click the error to modify proguard.cfg
rename "keepclasseswithmembernames" to "keepclasseswithmembers" on line it shows error and save.
run Lint again (click refresh in Lint Warnings view)
repeat renaming if it shows further warnings in subsequent lines.
I changed mine to look like this.
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-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 com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

Error in default proguard.cfg?

When I create a new Android project in Eclipse, the following default proguard.cfg file is created:
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-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.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
Note that the -optimizations line comments out the rest of the file. I'm wondering if it is intentional or if it is a typo and that line should have its last two characters reversed:
-optimizations !code/simplification/arithmetic,!field/*,!class/merging*/
I'm using ADT plugin 9.0 and Eclipse 3.6.1.
Okay. I'm now feeling a little stupid. The answer is that nothing here is a comment. The line should be interpreted as if it were spaced out like this:
-optimizations !code/simplification/arithmetic,
!field/*,
!class/merging/*
The * is a wildcard character and the syntax highlighting done by the forum is wrong in this case.
Are you sure you didn't accidently do that?
I just created a dummy project and ended up with this. I'd try to remove that line and see if that works.
-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 * implements android.os.Parcelable {
static android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
The keep-statements are used to prevent proguard from removing classes or class members in the shrinking step and from renaming them in the obfuscation step.
An Activity is a class that you most likely don't want to be romoved from your project. On Android's ProGuard page it says
For some situations, the default configurations in the proguard.cfg file will suffice. However, many situations are hard for ProGuard to analyze correctly and it might remove code that it thinks is not used, but your application actually needs. Some examples include:
List item a class that is referenced only in the AndroidManifest.xml file
Since that keep part is commented out in the default config I think it is intented, because the out commented -keep Activities line would prevent proguard from eliminating any activity.
In short: No typo

Android ProGuard settings to use with app that uses licensing server to not force close?

I've seemingly tried every setting in various articles on the internet including excluding all of my classes through -keep public class.
What settings should be used to not force close? At this point if I could get obfuscation with nothing else would be fine.
Below are sample configurations I've tried and my app still force closes. Any ideas?
I followed this article as well as others: http://android-developers.blogspot.com/2010/09/proguard-android-and-licensing-server.html
Still no luck. Any help would be appreciated.
Thanks
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-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 com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native ;
}
-keepclasseswithmembernames class * {
public (android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
public (android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
Similar options:
-target 1.6
-optimizationpasses 5
-allowaccessmodification
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
# The -optimizations option disables some arithmetic
# simplifications that Dalvik 1.0 and 1.5 can't handle.
-optimizations !code/simplification/arithmetic
-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 View {
public (android.content.Context);
public (android.content.Context, android.util.AttributeSet);
public (android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
-keep public class com.android.vending.licensing.ILicensingService
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep interface com.android.vending.licensing.*
-keep public interface com.android.vending.licensing.ILicenseResultListener
-keep public interface com.android.vending.licensing.ILicensingService
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
Just comment this line in the proguard.cfg file and clean/rebuild your project:
#-keep public class com.android.vending.licensing.ILicensingService
Delete all compiled classes in the classes directory and rebuild was the answer. Something was obviously corrupt. Once I did that, it just worked!

Categories

Resources