简体   繁体   English

Android Proguard - 如何保留仅从 XML 布局引用的 onClick 处理程序

[英]Android Proguard - how to keep onClick handlers only referenced from XML layouts

In my Android app I ofter don't create a View's on-click handler in code, but rely on the ability to specify it in the XML layout file, like this:在我的 Android 应用程序中,我通常不在代码中创建视图的点击处理程序,而是依赖于在 XML 布局文件中指定它的能力,如下所示:

   <Button
        ....
        android:onClick="onSearchClicked"
       ...../>

And then have the method in the Activity like this:然后在 Activity 中有这样的方法:

    public void onSearchClicked( View v ) {
    ........}

Meaning there is no obvious reference to this method in my own code.这意味着在我自己的代码中没有明显引用此方法。

When running Proguard for a production release it seems to remove this method and the on-click fails.在为生产版本运行 Proguard 时,它似乎删除了此方法并且单击失败。

What can I add to my proguard config file to avoid this that won't oblige me to rename all these methods?我可以在我的 proguard 配置文件中添加什么来避免这种不需要我重命名所有这些方法的情况?

  • An annotation I could add to the method and have proguard take notice of?我可以添加到方法中并让 proguard 注意到的注释?
  • Somehow specify these types of methods referenced from xml?以某种方式指定从 xml 引用的这些类型的方法?
  • I suppose I can add a false reference in the code, but would like to avoid that if I can as I won't always remember to put it in!我想我可以在代码中添加一个错误的引用,但如果可以的话,我想避免这种情况,因为我不会总是记得把它放进去!

I have looked through the proguard examples for Android and can't see anything for this particular need.我查看了 Android 的 proguard 示例,看不到任何满足这种特殊需求的东西。

This seems to be the best answer, as it is 100% robust to naming of such methods:这似乎是最好的答案,因为它对这些方法的命名是 100% 稳健的:

# This will avoid all the onClick listeners referenced from XML Layouts from being removed
-keepclassmembers class * extends android.app.Activity { 
       public void *(android.view.View); 
}

Hope it helps.希望能帮助到你。

-keepclasseswithmembers class * {
   public void onSearchClicked(android.view.View );
}

but double check it from proguard doc: http://proguard.sourceforge.net/index.html#/manual/refcard.html但从proguard doc仔细检查: http://proguard.sourceforge.net/index.html#/manual/refcard.html

I use:我用:

-keepclassmembers class * extends android.app.Activity { 
       public void on*Click(android.view.View); 
}

and then I name all onClick methods like: onCancelBtnClick(), onBackgroundClick(), etc.然后我将所有 onClick 方法命名为:onCancelBtnClick()、onBackgroundClick() 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM