java 你如何阻止 Proguard 删除类型参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12924425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:48:13  来源:igfitidea点击:

How do you stop Proguard from removing type parameters?

javaandroidmavenproguard

提问by Nick Johnson

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.

我目前正在尝试混淆一系列库。我的基础库包含多个使用类型参数的类和方法,由于 Proguard 混淆删除了类型参数,因此其他代码无法使用。消除混淆消除了这些问题。我已经通读了所有 ProGuard 使用文档、示例和故障排除,但找不到任何关于如何处理类型参数或 ProGuard 的哪个方面剥离类型参数的文档。

Constructor Type Parameter Issue:

构造函数类型参数问题:

Library 1 contains the following class:

库 1 包含以下类:

public abstract class AbstractFactoryFactory<T>

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

库 2 包含几个扩展上述类的类,但构造函数抛出一个编译器错误,指出:

error: type AbstractFactoryFactory does not take parameters

Return Type Parameter Issue:

返回类型参数问题:

Library 1 has a Foo class with the following method:

库 1 有一个具有以下方法的 Foo 类:

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:

Libary 2 尝试使用 doSomething 方法,但在混淆后该方法返回一个无类型列表,该列表生成以下编译器错误,指出:

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

回答by Nick Johnson

According to the ProGuard Typical Library usage guide:

根据 ProGuard典型库使用指南

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

在 JDK 5.0 及更高版本中编译时,需要“签名”属性才能访问泛型类型。

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

添加以下行修复了我缺少类型参数的问题:

-keepattributes Signature