How do you stop Proguard from removing type parameters?

Nick Johnson picture Nick Johnson · Oct 17, 2012 · Viewed 12.7k times · Source

I am currently attempting to obfuscate a series of libraries. My base library, which contains several classes and methods that use type parameters, is unusable by other code due to the type parameters being removed by Proguard obfuscation. Eliminating the obfuscation removes these issues. I have read through all of the ProGuard usage documents, examples, and troubleshooting, but have been unable to find any documentation on how to deal with type parameters or which aspect of ProGuard strips the type parameters.

Constructor Type Parameter Issue:

Library 1 contains the following class:

public abstract class AbstractFactoryFactory<T>

Library 2 contains several classes that extend the above class but the constructor throws a compiler error that states:

error: type AbstractFactoryFactory does not take parameters

Return Type Parameter Issue:

Library 1 has a Foo class with the following method:

public List<String> doSomething()

Libary 2 tries to use the doSomething method, but after obfuscation the method returns an untyped list that generates the following compiler error that states:

error: incompatible types Object

Proguard.cfg

-dontoptimize

-renamesourcefileattribute SourceFile
-keepparameternames
-keepattributes Exceptions,*Annotation*,InnerClasses,SourceFile,LineNumberTable,Deprecated

-keep public class * {
    public protected *;
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

Answer

Nick Johnson picture Nick Johnson · Oct 17, 2012

According to the ProGuard Typical Library usage guide:

The "Signature" attribute is required to be able to access generic types when compiling in JDK 5.0 and higher.

Adding the following line fixed my issues with missing type parameters:

-keepattributes Signature