【问题标题】:android:onClick not working with ProGuardandroid:onClick 不适用于 ProGuard
【发布时间】:2011-10-17 15:44:09
【问题描述】:

从今天开始,我的应用程序发生了一些奇怪的事情。每次我点击一个设置了 android:onClick 属性的按钮时,我都会得到一个 IllegalStateException: Could not find a method ...

我注意到只有当我在文件中启用 Proguard 时才会发生这种情况:default.properties

这是我的 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 *;
}

事情是上周没有发生..(我也在使用 proguard)。有什么想法吗?

编辑 我找到了解决这个问题的另一种方法: 有问题的项目是使用旧版本的 ADT 插件 (Eclipse) 创建的。我新建了一个参数相同的新项目,复制了src/、res/和Manifest,问题就解决了!

【问题讨论】:

  • 在您的帖子中,您不小心注释掉了大部分代码——不确定这是否只是一个错字。
  • 这是 proguard 文件,它有 /* 并且 stackoverflow 将其检测为注释..

标签: android proguard


【解决方案1】:

在android框架工具的示例文件(YOUR_ANDROID_DIR/tools/proguard/proguard-android.txt)中,可以找到如下规则:

# 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);
}

评论很明确。

【讨论】:

    【解决方案2】:

    您需要告诉 proguard 不要改变与您的 android:onClick 标记关联的方法。

    这是一个示例规则(取自the proguard website):

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

    【讨论】:

    • 就是这样.. 谢谢!我仍然不明白为什么以前没有发生这种情况。
    • 我刚刚检查过,android:onClick 在某些 xml 中有效,但在其他 xml 中无效。你知道为什么会这样吗?
    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 2021-04-26
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2019-09-12
    相关资源
    最近更新 更多