How to tell ProGuard to keep everything in a particular package?

AndroidProguard

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 *;
}

Solution 2 - Android

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>;
}

Solution 3 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionggurovView Question on Stackoverflow
Solution 1 - AndroidCaspar HarmerView Answer on Stackoverflow
Solution 2 - AndroidggurovView Answer on Stackoverflow
Solution 3 - AndroidTrueStoryView Answer on Stackoverflow