Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:onClick not working with ProGuard

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!

like image 412
SERPRO Avatar asked Oct 17 '11 15:10

SERPRO


2 Answers

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.

like image 131
ol_v_er Avatar answered Sep 28 '22 08:09

ol_v_er


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);
}
like image 39
slayton Avatar answered Sep 28 '22 08:09

slayton