Proguard-PersistenceException:构造函数与类不匹配 [英] Proguard - PersistenceException: Constructor not matched for class

查看:151
本文介绍了Proguard-PersistenceException:构造函数与类不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在具有simpleframework.xml库的应用中使用retrofit2.0.

I am using retrofit2.0 in my app with simpleframework.xml library.

问题是,当我在不使用proguard的情况下运行该应用程序时,它运行正常,但是在运行proguard时,我在日志中收到以下错误消息.

The problem is when I run the app without proguard it works fine however when I run proguard I get the following Error in logs.

E/ERROR: java.lang.RuntimeException: org.simpleframework.xml.core.PersistenceException: Constructor not matched for class A

类A没有/默认构造函数应该起作用.仍然我添加了无自变量构造函数.但这并不能解决问题.

The class A has no/default constructor which should work. Still I added a No Argument Constructor. But that didn't rectify the issue.

Class A

@Root(name = "data",strict = false)
public class A {
    @Element(name = "baseurl",required = false)
    private String baseURl;
    @Element(name = "country_code")
    private String country_code;

    //  Setters and getters
}

如您所见,没有构造函数(添加默认的空构造函数可解决此问题).因此,默认的无参数构造器也应该可以正常工作.但是,我尝试使用以下构造函数,这消除了错误.

As you can see there is no constructor (Adding the default empty constructor keeps the issue). So default No Argument Constructor should work just as well. However I tried with the following constructor and this removes the error.

public A(@ELement(name = "baseurl") String baseUrl,
         @Element(name = "country_code") String country_code) {    // Add all the elements from the xml in the constructor i.e. if a new element is added a new constructor would have to be written.
    baseURl = baseUrl;
    this.country_code = country_code;
}

但是,如果我想这样做的话,我有太多文件需要更改.除了构造函数要求所有映射的值都不需要.我有很多类可以托管50个以上的成员变量(我将示例类简化为仅包含两个成员变量).该课程包含大约30个课程,代码太久了,无法在此处发布.

But I have too many files to change if I want to do it this way. Besides a constructor requiring all the values mapped shouldn't be required. I have quite a few classes which host more than 50 member variables (I simplified the example class to include only two member variables). This class contains about 30 and code would've been simply too long to post here.

问题是我有很多类在假设每个类都没有参数构造函数的情况下工作.

The thing is I have loads of classes working on the assumption of No Argument constructor for each class.

仅为所有对象添加构造函数是不可行的.

Simply adding constructors for all is not feasible.

我的 proguard-rules.pro (仅具有相关的lib模糊处理规则).

My proguard-rules.pro (with only relevant lib obfuscation rules).

#-keepattributes *Annotation*

-dontwarn retrofit2.**
-keep class retrofit2.** { *; }

-dontwarn com.bea.xml.stream.**
-dontwarn org.simpleframework.xml.stream.**
-keep class org.simpleframework.xml.**{ *; }
-keepclassmembers,allowobfuscation class * {
    @org.simpleframework.xml.* <fields>;
    @org.simpleframework.xml.* <init>(...);
}

可能值得注意的是,在出现此错误之前,我已经获得

It might be worth noting that before this Error I was getting

E/ERROR: java.lang.RuntimeException: org.simpleframework.xml.core.ElementException: Element 'version' does not have a match in class A at line 1

通过在@Element注释中添加'name'参数来解决该问题.因此,我不愿更改所有文件的原因之一就是如果出现另一个错误.

Solved that by adding 'name' argument in @Element Annotation. So one of the reasons I'm reluctant to change all the files is what if another Error creeps up.

因此,在寻找解决方案两天后,我放弃了,最后将构造函数添加到所有类中.问题是库调用仅适用于可用的 xml-tags 的构造函数.如果xml中只有 country_code 可用,请对上述A类说吧

EDIT 1: So after 2 days of searching for a solution I gave up and finally added constructors to all the classes. The thing is the library calls constructor for only the available xml-tags. Say for the above class A if only country_code was available in the xml

<xml>
    <data>
        <country_code>PK</country_code>
    </data>
</xml>

然后我需要一个仅带有country_code参数的构造函数来使其工作

Then I'd need a constructor with only one argument of country_code to make it work

public A(@Element(name = "country_code") String country_code) {
    this.country_code = country_code;
}

这使找到的解决方案不可用.

Which makes the found solution unusable.

找到了解决方法!将POJO类保留在proguard规则中可解决此错误.但是我不想保留这些课程.

EDIT 2: Found a workaround! Keeping the POJO classes in the proguard rules fixes this error. But I would rather not keep these classes.

因此,我暂时至少要保留这个问题,直到有人告诉我为什么我应该保留这些文件为止.

And so I'm keeping this question open at-least for now or until someone can tell me why I should keep these files.

推荐答案

我想您的问题是您没有保留任何属性,并且显然取决于所使用的属性.就我而言,这就是我的处理方式,请告知它是否对您有用:

I guess that your problem is that you are not keeping any attributes and that obviously depends in which attributes you are using. In my case, this is how I dealt with it, let me know if it works for you:

## https://square.github.io/retrofit/ ##
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
 -keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

## Simple XML ##
-dontwarn org.simpleframework.xml.stream.**
-keep public class org.simpleframework.** { *; }
-keep class org.simpleframework.xml.** { *; }
-keep class org.simpleframework.xml.core.** { *; }
-keep class org.simpleframework.xml.util.** { *; }

-keepattributes ElementList, Root, *Annotation*

-keepclassmembers class * {
    @org.simpleframework.xml.* *;
}

这篇关于Proguard-PersistenceException:构造函数与类不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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