jeudi 2 avril 2015

NoSuchMethodException when accessing method via reflection after using Proguard

I wish to use Proguard to reduce the size and method count in an Android app. Currently obfuscating the code is not necessary. The REST api for the app is using reflection to call the appropriate method for the endpoint. This will be changing to Callbacks the in the future, but for the moment I need to work with this method.


This method is called when the request is successful and calls an appropriate method, saltSucceeded in the error message below.



public void onSuccess(int statusCode, String response) {
try {
getClass().getDeclaredMethod(action + "Succeeded", Integer.TYPE, String.class).invoke(this, statusCode, response);

} catch (Exception e) {
Logger.error(e);
callFailedMethod();
}
}


When Proguard has been run this error results when onSuccess is called from the salt action.



ERROR: ResponseHandler.onSuccess(113) ->
java.lang.NoSuchMethodException: saltSucceeded [int, class java.lang.String]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getDeclaredMethod(Class.java:640)
at com.someapp.api.response.ResponseHandler.onSuccess(ResponseHandler.java:110)
at com.someapp.api.response.ResponseHandler.onSuccess(ResponseHandler.java:94)
at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:359)
at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:183)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)


Here is the proguard-rules.pro file.



-dontobfuscate
-dontoptimize
-dontpreverify

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-printconfiguration configuration.txt

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!code/allocation/variable

-keepclasseswithmembers class com.someapp.** { *; }

# Library Specific Configurations follows
...


It is called thus from app/build.gradle:



proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


I have looked at several sites and references talking about trouble with Proguard and reflection, however the various recommendations have not solved the problem. Currently I am trying to stop proguard from modifying the com.someapp code at all to try and get back to a working state. From there I can spend more time trying to only preserve methods: *Failed, *Succeeded, *Complete, *NoConnection.


Thank you for any input you have.


http://ift.tt/ZZPEDY

Proguard and reflection in Android






Aucun commentaire:

Enregistrer un commentaire