Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

java - How do you stop Proguard from removing type parameters?

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...