I am developing a android application. I obfuscates the code of the project using progaurd. I want to obfuscate the folder structure as well. anyway to do that ?
Thanks for your help
ProGuard contains various directives, for your ProGuard rules file, that can obfuscate package names, such as -flattenpackagehierarchy and -repackageclasses. Just make sure that anything referenced from AndroidManifest.xml or otherwise accessed via reflection is kept intact.
It is conceivable that the commercial, Android-tuned DexGuard tool can even let you repackage stuff in the manifest (by modifying the manifest entries as part of the obfuscation process), but I do not know if it does or not.
copy and paste this code in proguard.cfg:
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
-dontoptimize
-dontpreverify
-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.app.backup.BackupAgent
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v7.app.ActionBarActivity
-keep public class * extends android.app.Fragment
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-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.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.**
Related
java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean java.lang.String.equals(java.lang.Object)’ on a null object reference
After debugging I have come to the conclusion that this is because that a value is not being passed from one activity to the other using putextra. This error only happens after enabling proguard. I am not aware of any keep rules to prevent this.
Currently my proguard file looks like this:
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep class * extends com.bumptech.glide.module.AppGlideModule {
<init>(...);
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
-keep class com.bumptech.glide.load.data.ParcelFileDescriptorRewinder$InternalRewinder {
*** rewind();
}
-keep public class pl.droidsonroids.gif.GifIOException{<init>(int, java.lang.String);}
-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.* { *;}
-dontwarn okio.
-dontshrink
-dontoptimize
# unknown stuff
-android
-allowaccessmodification
-dontpreverify
-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>;
}
I don't understand proguard much. I have added rules as required by third party libraries. Other than that I use firebase and some classes with getter and setter methods. Activity has all its members as private. It is my understanding that there is a null pointer exception occurring due to inability to pass values between intents using putextra after enabling proguard and so the value of variable of the second activity remains null.
How to solve this and better write proguard rules?
First of all, you should give more details.
For example, you have a folder named model and inside you have a response model class.
Your FileTree
-model
--Response.java
If you do not specify this in proguard, the release app will not be able to use it as a reference, because proguard will change keys on memory. so you should remove your models from proguard. And finally, the app cannot wrong reference key on memory, after then you will get a null pointer error.
-keep class com.name.app.model.** { *; }
Thus, the model folder will not interfere with its classes within
I am working on an Android library project. I want to distribute this library as JAR to other developers.Before distributing the JAR I want to obfuscate it. I have tried using PROGUARD for this purpose, but its failing at the last step and says
The output jar is empty. Did you specify the proper '-keep' options?
Can anyone suggest what is going wrong ?
=================EDIT : Error Log =======================
ProGuard, version 4.7
Reading program jar [C:\Users\XYZ\Downloads\login.jar]
Reading library jar [D:\Softwares\Android SDK\adt-bundle-windows-x86-20130522\sdk\platforms\android-8\android.jar]
Note: the configuration refers to the unknown class 'javax.swing.plaf.ComponentUI'
Note: the configuration refers to the unknown class 'javax.swing.plaf.ComponentUI'
Note: the configuration refers to the unknown class 'javax.swing.JComponent'
Note: there were 3 references to unknown classes.
You should check your configuration for typos.
The output jar is empty. Did you specify the proper '-keep' options?
=============== Edit : proguard.cfg =====================
-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
-keep public class MyClass extends MyView {
public static String GetSessionID () {
}
}
-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 *;
}
Add this line in your proguard file.
-keep class <your jar file name>.** { *; }
Proguard returned with error code 1. See console
[2013-04-04 16:25:20 - ] Note: there were 157 duplicate class definitions.
[2013-04-04 16:25:20 - ] Error: Method must be overridden in [proguard.optimize.peephole.ClassMerger] if ever called
I have tried every possible solution found on SO. There were around 2000 duplicates which now came down to 157. I have some external jars in my project too. This is my proguard.cfg file:
-dontwarn android.support.**
-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 android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
-keep class com.flurry.** { *; }
-dontwarn com.flurry.**
-keep class org.apache.** { *; }
-dontwarn org.apache.**
-dump proguard/class_files.txt
-printseeds proguard/seeds.txt
-printusage proguard/unused.txt
-printmapping proguard/mapping.txt
This application is for Android 4.0
Can you kindly let me know whats causing this and how do I go about in fixing it.
Merging my project's proguard config with one from ${sdk.dir}/tools/proguard/proguard-android.txt did the trick.
This error mostly happens with 3rd party build systems (e.g. Maven), which don't know about default proguard config.
UPD: ...because of the following content of default config:
# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
I would like to use ProGuard to obfuscate my Android app. This works fine. But my gui classes, which extends acitvity, view and sherlockactivity are not obfuscated. Here is the proguard.cfg
-injars bin/classes
-injars libs
-outjars bin/classes-processed.jar
-libraryjars C:/Users/android-sdks/platforms/android-17/android.jar
-dontpreverify
-dontoptimize
-repackageclasses ''
-allowaccessmodification
-optimizationpasses 5
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepattributes *Annotation*
-dontwarn sun.misc.Unsafe
-dontwarn com.actionbarsherlock.**
-dontwarn com.google.common.**
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
-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
-keep public class * extends android.view.ViewGroup
-keep public class * extends android.support.v4.app.Fragment
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers class android.support.v4.app.Fragment {
*** getActivity();
public *** onCreate();
public *** onCreateOptionsMenu(...);
}
-keepclassmembers class * extends com.actionbarsherlock.ActionBarSherlock {
public <init>(...);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-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*(...);
}
-keepclassmembers class * implements android.os.Parcelable {
static android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
-keepclassmembers class com.brainyoo.brainyoo2android.ui.html.BYJavaScriptInterface {
public *;
}
First, I thought activities can´t be obfuscated because of the reflection call
myactivity.class
I've tried to add:
- keep public class mypackege.myactivity.class
But it does not solve the problem. Any ideas how to obfuscate the gui elements?
Thanks
Christine
But my gui classes, which extends acitvit, view and sherlockactivtiy are not obfusecated.
That is because your ProGuard configuration file says to not obfuscate them. Moreover, this is important, as if they are obfuscated, your app will not run, because:
Android will not find your renamed activities
Android will not find your activity lifecycle methods
Android will not find your widgets (for use with reflection in interpreting layout resources)
etc.
I´ve tried to added: -keep public class mypackege.myactivity.class But it does not solve the problem.
That is because you are telling ProGuard to not obfuscate that class.
Any ideas how to obfusecate the gui elements?
You don't, if you wish to have a working app when you are done.
I try to use proguard android tools. But obfuscated apk is not installed (i use eclipse export wizard). dex2jar show me that all classes were in jar but named with *.dump.txt and contains something like this:
//class:0002 access:0x0011
public final class com.company.android.BuildConfig
//field:0000 access:0x0019
//Lcom/company/android/BuildConfig;.DEBUG Z
public static final Z DEBUG=true;
//method:0000 access:0x10001
//Lcom/company/android/BuildConfig;.<init>()V
public V <init>()
this:v0 //com.company.android.BuildConfig
LOCAL_VARIABLE LL0 ~ LL1 v0 -> this // Lcom/company/android/BuildConfig;
LABEL | LL0: line 4
INVOKE_DIRECT | |v0.<init>() //Ljava/lang/Object;.<init>()V
RETURN_VOID | |return
LABEL | LL1:
Where i'm wrong? How can i make working obfuscating apk?
My proguard.cfg:
-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 *;
}
My project.properties:
target=android-8
proguard.config=proguard.cfg
Thanks!