ProGuard obfuscation, java, Google Gson and generi

2019-05-11 22:33发布

问题:

I have a class like this:

public class MyClass
{
    private Queue<MyOtherClass> myQueue;
}

My problem is that I cannot get ProGuard to keep myQueue after serialization to json using Google Gson. What happens is that the member name "myQueue" is serialized as "a". Obviously, deserialization then breaks.

Here are some of the ProGuard configs I have tried.

-keepclassmembers class com.my.package.MyClass {
    #private java.util.Queue<com.my.package.MyOtherClass> myQueue;
    #private java.util.Queue<com.my.package.*> myQueue;
    private java.* myQueue;
}

With

private java.util.Queue<com.my.package.MyOtherClass> myQueue;

...ProGuard complained that the class was unknown. The message is:

Note: the configuration refers to the unknown class java.util.Queue<com.my.Package.MyOtherClass>'

Using

private java.* myQueue; 

....gets rid of the ProGuard warning, but, as I said, the member myQueue is not kept in the json output. It is serialized as "a".

The rest of the relevant ProGuard config is as follows:

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-keepattributes *Annotation*
#gson
-keepattributes Signature
-adaptresourcefilenames    **.properties,**.gif,**.jpg,**.png,**.wav
-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF
-optimizationpasses 3
-overloadaggressively
-repackageclasses ''
-allowaccessmodification

-keep public class com.my.package.MyOtherClass {
}

-keepclassmembers class com.my.package.MyOtherClass {
    [a large number of private members are listed]
}

回答1:

Since java class files contain erased generics, ProGuard expects erased types too. So java.util.Queue<com.my.Package.MyOtherClass> should be specified as java.util.Queue.

The alternative with a wildcard works if you specify java.** (with double ** to match classes in subpackages too).

Cfr. the ProGuard manual