你如何阻止 Proguard 删除类型参数? [英] How do you stop Proguard from removing type parameters?

查看:21
本文介绍了你如何阻止 Proguard 删除类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

构造函数类型参数问题:

库 1 包含以下类:

public abstract class AbstractFactoryFactory<T>

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

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

返回类型参数问题:

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

Library 1 has a Foo class with the following method:

public List<String> doSomething()

Libary 2 尝试使用 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();
}

推荐答案

根据 ProGuard 典型的库使用指南:

According to the ProGuard Typical Library usage guide:

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

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

这篇关于你如何阻止 Proguard 删除类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆