Saturday, 12 December 2015

Proguard: rules for building a library

So you have a library defined and you want to run proguard on it when building it to remove unused code and to obfuscate code. Of course you do not want to remove or obfuscate classes/interfaces/enums/methods/fields that are public since these will be called on by the projects that depend on your library.

Here are the rules that you need in your proguard config file:

-keepparameternames

-keep public interface com.mycompany.mylibrary.** {
    <methods>;
}

-keep public class com.mycompany.mylibrary.** {
    public <init>(...);
    public <fields>;
    public static <fields>;
    public <methods>;
    public static <methods>;
}

-keep public enum com.mycompany.mylibrary.** {
    public <init>(...);
    public <fields>;
    public static <fields>;
    public <methods>;
    public static <methods>;
}

The above is not an exhaustive list of all the rules which you might need but it's a starting point and should suffice for most libraries. If you need to add some more rules you'll need to get a grasp of the proguard rules syntax here: http://proguard.sourceforge.net/manual/usage.html

1 comment:

adil said...

If you want to simply keep all the properties and methods inside of your public interfaces, classes and enums as they are, the rules that you want are the following:

-keep public interface com.mycompany.mylibrary.** { *; }
-keep public class com.mycompany.mylibrary.** { *; }
-keep public enum com.mycompany.mylibrary.** { *; }