Proguard causing NoSuchMethodException

爷,独闯天下 提交于 2019-12-13 04:23:52

问题


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) {
....
}

回答1:


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.



来源:https://stackoverflow.com/questions/16680813/proguard-causing-nosuchmethodexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!