Android Proguard-如何保持仅从XML布局引用的onClick处理程序 [英] Android Proguard - how to keep onClick handlers only referenced from XML layouts

查看:52
本文介绍了Android Proguard-如何保持仅从XML布局引用的onClick处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中,我通常不会在代码中创建View的单击处理程序,而是依靠在XML布局文件中指定它的能力,如下所示:

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:

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

然后在Activity中使用如下方法:

And then have the method in the Activity like this:

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

这意味着在我自己的代码中没有明显引用此方法.

Meaning there is no obvious reference to this method in my own code.

为生产版本运行Proguard时,似乎删除了此方法,并且单击失败.

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注意到吗?
  • 以某种方式指定从xml引用的这些方法的类型?
  • 我想我可以在代码中添加一个错误的引用,但是如果可以的话,我想避免这种情况,因为我永远不会记得把它放进去!

我浏览了Android的proguard示例,看不到任何满足此特定需求的东西.

I have looked through the proguard examples for Android and can't see anything for this particular need.

推荐答案

这似乎是最好的答案,因为它对此类方法的命名具有100%的稳定性:

This seems to be the best answer, as it is 100% robust to naming of such methods:

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

希望有帮助.

这篇关于Android Proguard-如何保持仅从XML布局引用的onClick处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆