How to tell ProGuard to keep everything in a particular package?
Asked Answered
P

3

62

My application has many activities and uses native library too. With the default ProGuard configuration which Eclipse generates ProGuard removes many things - OnClick methods, static members, callback methods which my native library uses... Is it there a simple way to instruct ProGuard to NOT remove anything from my package? Removing things saves only about 2.5% of the application size, but breaks my application completely. Configuring, testing and maintaining it class by class in ProGuard configuration would be a pain.

Period answered 18/9, 2011 at 19:52 Comment(4)
You mean, for example, like simply not using proguard?Turbary
No, I want the obfuscation, I just dont want removing of any classes and class members from my package.Period
I had the same problem. This should help you. #6633911Londrina
Thank you, Vinoth, "-dontshrink" was useful, but not enought in my case. I will post later what I did.Period
P
29

As final result I found that just keeping all class members is not enough for the correct work of my application, nor necessary. I addded to the settings file this:

-keepclasseswithmembers class * {
    void onClick*(...);
}
-keepclasseswithmembers class * {
    *** *Callback(...);
}

The case with onClick* is for all methods which I address in android:onClick atribute in .xml layout files (I begin the names of all such methods with 'onClick').

The case with *Callback is for all callback methods which I call from my native code (through JNI). I place a suffix 'Callback' to the name of every such method.

Also I added few rows for some special cases with variables which I use from native code, like:

-keep class com.myapp.mypackage.SomeMyClass {
    *** position;
}

(for a varible with name 'position' in a class with name 'SomeMyClass' from package com.myapp.mypackage)

All this is because these onClick, callback etc. must not only be present but also kept with their original names. The other things ProGuard can optimize freely.

The case with the native methods is important also, but for it there was a declaration in the generated from Eclipse file:

-keepclasseswithmembernames class * {
    native <methods>;
}
Period answered 19/9, 2011 at 10:20 Comment(0)
S
62

EDIT This answer is 10 years old - it may not apply to newer proguard versions.

I think you need to add these flags at the very least (modify for you individual package names):

-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }

Also, add these flags:

-dontshrink
-dontoptimize
-dontpreverify

Here's my whole config file: of my Proguard.cfg:

-dontshrink
-dontoptimize
-dontpreverify
-verbose

-dontwarn javax.management.**
-dontwarn java.lang.management.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.slf4j.**
-dontwarn org.json.**


-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 class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }

-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 *;
}
Scapegoat answered 18/9, 2011 at 20:58 Comment(3)
My final solution doesn't use anything from your answer (check my last edit), because my understanding evolved, but anyway you deserved my upvote and you have it - because the answer is good about the initial question.Period
I was having similar problems, where the classes were kept, but not the content. Adding { *; }, rather than just { * } solved it for me. Thanks!Housum
this is what worked for me: -keep class com.APP_DOMAIN.** { *; }Parathion
P
29

As final result I found that just keeping all class members is not enough for the correct work of my application, nor necessary. I addded to the settings file this:

-keepclasseswithmembers class * {
    void onClick*(...);
}
-keepclasseswithmembers class * {
    *** *Callback(...);
}

The case with onClick* is for all methods which I address in android:onClick atribute in .xml layout files (I begin the names of all such methods with 'onClick').

The case with *Callback is for all callback methods which I call from my native code (through JNI). I place a suffix 'Callback' to the name of every such method.

Also I added few rows for some special cases with variables which I use from native code, like:

-keep class com.myapp.mypackage.SomeMyClass {
    *** position;
}

(for a varible with name 'position' in a class with name 'SomeMyClass' from package com.myapp.mypackage)

All this is because these onClick, callback etc. must not only be present but also kept with their original names. The other things ProGuard can optimize freely.

The case with the native methods is important also, but for it there was a declaration in the generated from Eclipse file:

-keepclasseswithmembernames class * {
    native <methods>;
}
Period answered 19/9, 2011 at 10:20 Comment(0)
H
6

I know this is an old question but I hope that the following information might help other people.

You can prevent ProGuard from removing anything in a certain package as follows;

-keep,allowoptimization,allowobfuscation class com.example.mypackage.** { *; }

Using the modifiers allowoptimization and allowobfuscation will make sure that ProGuard still obfuscates and optimizes the code. Shrinking will of course be disabled as intended.

You can easily verify how these -keep rules affect the code without the need to (re-)build using the ProGuard Playground.

Hypesthesia answered 3/3, 2021 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.