ProGuard 混淆、java、Google Gson 和通用集合 - 如何保留成员?

发布于 2024-12-26 07:35:03 字数 1480 浏览 1 评论 0原文

我有一个这样的类:

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

我的问题是,我无法让 ProGuard 在使用 Google Gson 序列化为 json 后保留 myQueue。发生的情况是成员名称“myQueue”被序列化为“a”。显然,反序列化就会中断。

以下是我尝试过的一些 ProGuard 配置。

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

随着

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

...ProGuard 抱怨该类未知。消息是:

注意:配置引用了未知类java.util.Queue'

使用

private java.* myQueue; 

.... 消除了 ProGuard 警告,但是,正如我所说,成员 myQueue 不保留在 json 输出中。它被序列化为“a”。

其余相关 ProGuard 配置如下:

-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]
}

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]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

—━☆沉默づ 2025-01-02 07:35:03

由于 java 类文件包含已擦除的泛型,因此 ProGuard 也期望已擦除的类型。因此,java.util.Queue 应指定为 java.util.Queue

如果您指定 java.** (也使用双 ** 来匹配子包中的类),则使用通配符的替代方法有效。

Cfr。 ProGuard 手册

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文