Proguard的引发NoSuchMethodException [英] Proguard causing NoSuchMethodException

查看:494
本文介绍了Proguard的引发NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类<> C =的Class.forName(co.uk.MyApp.dir.TargetClass);
方法主要= c.getDeclaredMethod(主,Report_Holder.class);

抛出java.lang.NoSuchMethodException.main [类co.uk.MyApp.classes.Report_Holder]错误一旦我$ P $使用ProGuard ppared发布的应用程序。

我花了很多时间思考的问题是co.uk.MyApp.dir.TargetClass,注释掉的东西,重新发布该应用程序,并重新测试。但事实证明,错误是正确的根在:

 方法主要= c.getDeclaredMethod(主,Report_Holder.class);

然后我更新的ProGuard-project.txt包括:

  -dontobfuscate
-keeppackagenames

(我用的是皮棉建议方法,建议把code到project.properties并把配置在一个文本文件),如:

  proguard.config = $ {} sdk.dir /tool​​s/proguard/proguard-android.txt:proguard-project.txt

但添加这些2行没有任何影响。

所以,现在我想知道如果;

A)我应在其上添加任何'-keeppackagenames等。

b)是proguard.config设置是否正确;应该$ {} sdk.dir实际上是一个正确的URI到SDK

这是针对类是这样的:

 公共静态无效的主要(Report_Holder参数){
....
}

修改

或者是因为我有这种类型的事情的两个实例,都被称为'主'?但是叫不同的活动。

 方法主要= c.getDeclaredMethod(主,Report_Holder.class);
方法主要= c.getDeclaredMethod(主,OtherReport_Holder.class);

和两个目标是这样的:

 公共静态无效的主要(Report_Holder参数){
....
}公共静态无效的主要(OtherReport_Holder参数){
....
}


解决方案

一旦你知道如何使用ProGuard的,你应该添加的选项 -keepattributes签名
。这种使用泛型(集合)的时候是necesary。

有关通过反射beeing调用的所有方法,必须显式从obfsucation排除。使用该选项输出混淆map文件,看看你的规则了预期的效果。

Class<?> c = Class.forName("co.uk.MyApp.dir.TargetClass");
Method main = c.getDeclaredMethod("main", Report_Holder.class);

Throws a 'java.lang.NoSuchMethodException.main[class co.uk.MyApp.classes.Report_Holder]' error once I've prepared the app for release using Proguard.

I spent hours thinking the problem was in 'co.uk.MyApp.dir.TargetClass', commenting out things, re-releasing the app, and re-testing. But it turns out that the error is right at the root, at:

Method main = c.getDeclaredMethod("main", Report_Holder.class);

I then updated proguard-project.txt to include:

-dontobfuscate
-keeppackagenames

(I am using the Lint suggested method which suggested putting code into project.properties and putting the config in a text file), such as:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

But adding those 2 lines didn't have any effect.

So now I am wondering if;

a) I should add anything on top of '-keeppackagenames' etc.

b) Is proguard.config set up correctly; should ${sdk.dir} actually be a proper uri to the sdk

The class that it is targeting is like this:

public static void main(Report_Holder args) {
....
}

Edit

Or is it because I have 2 instances of this type of thing, both called 'main' ? But called in different activities.

Method main = c.getDeclaredMethod("main", Report_Holder.class);
Method main = c.getDeclaredMethod("main", OtherReport_Holder.class);

And both targets being like this:

public static void main(Report_Holder args) {
....
}

public static void main(OtherReport_Holder args) {
....
}

解决方案

Once you know how to use proguard, you should add the option -keepattributes Signature . this is necesary when using generics (collections).

For all methods beeing called via reflection, you must explictly exclude them from obfsucation. use the option to output the obfuscation map file, to see if your rules had the desired effect.

这篇关于Proguard的引发NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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