Proguard causing NoSuchMethodException

2019-09-09 02:59发布

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条回答
欢心
2楼-- · 2019-09-09 03:22

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.

查看更多
登录 后发表回答