Android数据绑定:为混淆的AAR生成错误的BR类 [英] Android Data Binding: Wrong BR class generated for obfuscated AAR

查看:1809
本文介绍了Android数据绑定:为混淆的AAR生成错误的BR类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个使用Android数据绑定的混淆的AAR库。当我使用测试应用程序的库时,只要库未被ProGuard精简,一切都可以正常工作。但是,在启用ProGuard之后,测试应用程序不再编译,因为无法找到生成的数据绑定类中的BR字段。

I'd like to provide an obfuscated AAR library which makes use of Android data binding. When I use the library from a test app, everything works fine as long as the library isn't minified by ProGuard. However, after enabling ProGuard the test app doesn't compile any more since BR fields in the generated data binding classes can't be found.

由于我找不到任何关于这个具体主题的官方文件,我试图了解Android数据绑定后的魔法。机制似乎是这样的(请纠正我,如果我错了):

Since I couldn't find any official documentation on this "specific" subject, I tried to understand the magic behind Android data binding. The mechanism seem to be like this (please correct me, if I'm wrong):


  • 为了在AAR中使用数据绑定lib,嵌入式应用程序也必须启用数据绑定。

  • 这是因为包含数据绑定说明的布局资源在AAR中没有修改。

  • 因此,嵌入式应用程序有责任为lib中的布局生成相应的数据绑定类。 (这就是为什么lib的视图模型类不能被模糊,BTW。)

  • Android数据绑定生成器的挑战是告诉软件包名称,除了库和嵌入应用程序:库的BR类必须在库的包中生成(例如com.example.lib.databinding),因为该类是从库的视图模型类访问的。另一方面,嵌入式应用程序的BR类通常应在应用程序的软件包(com.example.app.databinding)中生成。

  • In order to use data binding within an AAR lib, the embedding app must enable data binding, too.
  • This is because the the layout resources that contain data binding instructions are included without modification in the AAR.
  • It's therefore the embedding app's responsibility to generate the corresponding data binding classes for the layouts in the lib. (That's why the lib's view model classes must not be obfuscated, BTW.)
  • The challenge for the Android data binding generator is to tell the package names apart from both library and embedding app: The BR class for the library must be generated in the library's package (e. g. com.example.lib.databinding), since this class is accessed from the library's view model classes. On the other hand, the BR class of the embedding app should normally be generated within the app's package (com.example.app.databinding).

这正是我的问题开始的地方。我不知道Android完全是因为这个挑战,我只知道在我的情况下,它与一个无模糊的lib一起使用,并没有一个混淆的。当我研究嵌入式应用程序的生成源时,我看到:

And this is exactly where my problems begin. I don't know Android exactly rises to this challenge, I only know that in my case, it works with an unobfuscated lib, and it doesn't with an obfuscated one. When I look into the generated source of the embedding app, I see:


  • 当使用无模糊化的lib时,BR和所有*绑定.java类在库的包中生成,应用程序编译。

  • 在使用混淆的lib时,BR和所有* Binding.java类都在应用程序包中生成。更糟糕的是,BR只包含XML资源中的模型变量名的常量,而不是视图模型类中的属性。因此,该应用程序不编译。

  • 我试图将数据绑定类的包明确地设置到XML声明中的lib的包中,但这并不能解决问题不完整的BR类。

我不知道这些差异来自哪里,我已经担心唯一的解决方案可能会删除所有我的好的数据绑定的东西从lib ...有谁做了类似的经验,可以给我一个提示吗?

I have no clue where these differences come from and I' already fearing that the only solution could be removing all my nice data binding stuff from the lib ... Does anybody made similar experiences and could give me a hint, please?

这些是我已经添加的ProGuard例外到我的lib:

These are the ProGuard exceptions I've already added to my lib:

-keep public class **.BR { public *; }
-keep public class **.BR$* { public *; }
-keepclassmembers class **.BR$* {
    public static <fields>;
}
-keepclassmembers class **.R$* {
    public static <fields>;
}
-keep class android.databinding.** { *; }
-keep class * extends android.databinding.** { *; }
-keep class com.example.lib.databinding.** { *; }


推荐答案

显然Google已经解决了Gradle插件2.3 .0,请参阅 https://code.google.com/p/ android / issues / detail?id = 229684

Apparently Google has resolved the issue with Gradle plugin 2.3.0, see https://code.google.com/p/android/issues/detail?id=229684.

这篇关于Android数据绑定:为混淆的AAR生成错误的BR类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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