如何使用Java注解来指导Android的Proguard?

发布于 2024-10-07 21:22:34 字数 350 浏览 0 评论 0原文

当在 Android 中使用 Proguard 时,仅通过反射调用的方法(例如,onClick XML 属性中定义的回调)会被错误地删除。

此问题的一个解决方案是将每个受影响的类和方法添加到您的混淆器中.cfg。

如何使用Java注解来达到同样的效果?

我觉得这将使代码自我记录,并且可以避免代码和 proguard.cfg 不同步。然而,Android 的 Proguard 似乎没有附带其文档中提到的annotations.jar。

When using Proguard with Android, methods that are only invoked via reflection (e.g., callbacks defined in onClick XML attributes) are erroneously stripped out.

One solution for this issue is to add each affected class and method to your proguard.cfg.

How can I use Java annotations to achieve the same effect?

I feel that would make the code self-documenting and it would avoid code and proguard.cfg drifting out of sync. However, Android's Proguard doesn't seem to ship with the annotations.jar mentioned in its documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄟ。诗瑗 2024-10-14 21:22:34

您可以从官方 ProGuard 版本中检索 annotations.jarannotations.pro。然后,您应该能够使用此处讨论的注释。

所有必要的选项都可以进入 proguard.cfg。

You can retrieve annotations.jar and annotations.pro from an official ProGuard release. You should then be able to use annotations as discussed here

All the necessary options can go in proguard.cfg.

瑾夏年华 2024-10-14 21:22:34

我最近遇到了这个问题。您需要执行以下操作:

要修复 onClick 事件,请将其添加到 Proguard 设置

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

要保留注释,请添加

-keepattributes **-keepattributes *Annotation*

提供更多信息这里http://www.simpligility.com/2010/ 12/hints-for-using-proguard-on-your-android-app/ 我使用 maven-android-plugin 来编译 android 应用程序,这篇文章几乎总结了我为 android 工作所做的工作。希望这有帮助!

I ran in to this problem recently. Here is what you need to do:

To fix the onClick events add this to Proguard settings

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

To keep annotation add

-keepattributes ** or -keepattributes *Annotation*

More information is available here http://www.simpligility.com/2010/12/hints-for-using-proguard-on-your-android-app/ I use maven-android-plugin to compile android app and this article pretty much sums up what I do for the android to get it working. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文