android:onClick not working with ProGuard

不问归期 提交于 2019-12-03 20:14:06

问题


Since today, something weird is happening with my application. Every time I click a button that has set the android:onClick attribute, I get an IllegalStateException: Could not find a method ...

I noticed that only happens when I enable Proguard in the file: default.properties

This is my proguard.cfg:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

The thing is that doesn't happened last week.. (I was using proguard too). Any ideas?

EDIT I found another solution to this problem: The project with problems was created with an old version of the ADT plugin (Eclipse). I created a new project with the same parameters and copied the src/, res/ and Manifest, and problem solved!


回答1:


You need to tell proguard not to mutate the method associated with your android:onClick tag.

Here is an example rule (taken from the proguard website):

-keep class mypackage.MyCallbackClass {
    void myCallbackMethod(java.lang.String);
}



回答2:


in the example file in the android framework tools (YOUR_ANDROID_DIR/tools/proguard/proguard-android.txt), you can find the following rule:

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

With the comment it's quite explicit.



来源:https://stackoverflow.com/questions/7796190/androidonclick-not-working-with-proguard

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