How can I go about creating an obfuscate jar file? As of now I can easily export my Android lib project to a jar and use it. How do I obfuscate the jar file?
My end goa
figuring this out was not easy, but once done, repeating the steps is not that hard:
proguard.cfg example
# optimization passes will allow multiple tries to strip dead code
# if it doesn't find anything on the 2nd pass, it won't do a 3rd.
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
# it would complain about my missing R stuff, and it does the right thing with this line
-dontwarn com.mycompany.myproject.R*
# the location of the pre-obfuscated .jar built by your project
-injars bin/myproject.jar
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
# this will fake "dead-strip" calls to Log.v() & Log.d()
-assumenosideeffects class android.util.Log {
public static int v(...);
public static int d(...);
}
# this will fake "dead-strip" calls to a more complex logging function i created
-assumenosideeffects class com.mycompany.myproject.FirstPackageWithLogging {
private void LogR(...);
}
# this will fake "dead-strip" a function in which i check to see if the
# USB-cable is connected and perform waitForDebugger(), since i don't want
# those making use of my obfuscated library to have to wait for the debgger
# when they won't be debugging my library
-assumenosideeffects class com.mycompany.myproject.MyProject {
private void setupDebugging();
}
# this will fake "dead-strip" a toast i use to measure focus
-assumenosideeffects class com.mycompany.myproject.UXObserver {
private void toastFocus();
}
# i don't want to obfuscate 3rd-party stuff, in case someone else
# is using the same classes, and wants to just get mine via pass-through
-keep class org.thirdparty.package.**
-keep class com.licensedpackage.**
# i want my API to be public, so i expose it
-keep public class com.mycompany.myproject.MyAPI
# almost all the rest are the suggestion of the proguard examples page
-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
-keepclassmembers public class com.mycompany.myproject.MyAPI {
public static ;
}
-keepclasseswithmembernames class * {
native ;
}
-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public (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 *;
}
…
$ java -jar $ANDROID_SDK/tools/proguard/lib/proguard.jar
-libraryjars $ANDROID_SDK/platforms/android-18/android.jar @proguard.cfg
-outjars ../MyReleaseSDK/libs/myproject.jar
that's it. the output will contain your obfuscated .jar file.
from that point, i copy over a few items from my project (.project file minus external tools builder for NDK; res folder, libs folder for my jni code, and project.properties and AndroidManifest.xml) into MyReleaseSDK, then create a .zip out of it. that .zip can then be used by someone else as an Eclipse "import" project; it will create an empty src folder, gen the resources properly, and anyone who wants to can now use it as a regular Android library project despite it being obfuscated.