Android   Data Binding 在 library module 中遇到错误及解决办法

所属分类: 软件编程 / Android 阅读数: 13
收藏 0 赞 0 分享

记一次 Data Binding 在 library module 中遇到的大坑

使用 Data Binding 也有半年多了,从最初的 setVariable,替换 findViewById,到比较高级的双向绑定,自定义 Adapter、Component,查看源码了解编译、运行流程,也算是小有成果,且没有碰到 Data Binding 本身实现上的问题。

然而,最近在一次重构组件化(见 MDCC 上冯森林的《回归初心,从容器化到组件化》)的过程中,碰到了一个比较严重的 BUG。已经提交 issue(#224048)到了 AOSP,虽然改起来是不麻烦,但是因为是 gradle plugin,所以 - -,还是让 Google 自己来吧。希望能早日修复。

Library module 生成 class

在 library module 下启用 Data Binding 很简单,跟 application module 一样,加上:

android { 
 dataBinding {
  enabled = true
 }
}

对应生成的 binding 类会在 manifest 里面指定的 package name 下的 databinding 包下。

于是坑的地方就在这里了,编译不过了…

为啥呢?报错说 symbol 找不到…于是在 module 的 build 下查看生成的 Binding 类…卧槽?!怎么是 abstract 的?怎么都找不到那些 get 方法了?虽然我也不知道为什么我们会从 binding 类里面去拿之前 set 进去的 ViewModel。

WTF?!

What happened

Fuck 归 fuck,究竟怎么回事还是要研究一下的。

是我们姿势错了?Dagger2 生成哪里出问题了?还是 Data Binding 的 bug 呢?

因为之前也研究过 data binding 生成部分的代码,所以找到问题所在没有花太多时间,这里不多啰嗦,直接看对应位置。

在 CompilerChief 的 writeViewBinderInterfaces 中:

public void writeViewBinderInterfaces(boolean isLibrary) {
 ensureDataBinder();
 mDataBinder.writerBaseClasses(isLibrary);
}

对应 DataBinder:

public void writerBaseClasses(boolean isLibrary) {
 for (LayoutBinder layoutBinder : mLayoutBinders) {
  try {
   Scope.enter(layoutBinder);
   if (isLibrary || layoutBinder.hasVariations()) {
    String className = layoutBinder.getClassName();
    String canonicalName = layoutBinder.getPackage() + "." + className;
    if (mWrittenClasses.contains(canonicalName)) {
     continue;
    }
    L.d("writing data binder base %s", canonicalName);
    mFileWriter.writeToFile(canonicalName,
      layoutBinder.writeViewBinderBaseClass(isLibrary));
    mWrittenClasses.add(canonicalName);
   }
  } catch (ScopedException ex){
   Scope.defer(ex);
  } finally {
   Scope.exit();
  }
 }
}

这里调用了 LayoutBinder(真正的实现类会调用 writeViewBinder):

public String writeViewBinderBaseClass(boolean forLibrary) {
 ensureWriter();
 return mWriter.writeBaseClass(forLibrary);
}

可以看到如果是 library module,我们会做特殊的编译,而不会生成真正的实现:

public fun writeBaseClass(forLibrary : Boolean) : String =
 kcode("package ${layoutBinder.`package`};") {
  Scope.reset()
  nl("import android.databinding.Bindable;")
  nl("import android.databinding.DataBindingUtil;")
  nl("import android.databinding.ViewDataBinding;")
  nl("public abstract class $baseClassName extends ViewDataBinding {")
  layoutBinder.sortedTargets.filter{it.id != null}.forEach {
   tab("public final ${it.interfaceClass} ${it.fieldName};")
  }
  nl("")
  tab("protected $baseClassName(android.databinding.DataBindingComponent bindingComponent, android.view.View root_, int localFieldCount") {
   layoutBinder.sortedTargets.filter{it.id != null}.forEach {
    tab(", ${it.interfaceClass} ${it.constructorParamName}")
   }
  }
  tab(") {") {
   tab("super(bindingComponent, root_, localFieldCount);")
   layoutBinder.sortedTargets.filter{it.id != null}.forEach {
    tab("this.${it.fieldName} = ${it.constructorParamName};")
   }
  }
  tab("}")
  nl("")
  variables.forEach {
   if (it.userDefinedType != null) {
    val type = ModelAnalyzer.getInstance().applyImports(it.userDefinedType, model.imports)
    tab("public abstract void ${it.setterName}($type ${it.readableName});")
   }
  }
  tab("public static $baseClassName inflate(android.view.LayoutInflater inflater, android.view.ViewGroup root, boolean attachToRoot) {") {
   tab("return inflate(inflater, root, attachToRoot, android.databinding.DataBindingUtil.getDefaultComponent());")
  }
  tab("}")
  tab("public static $baseClassName inflate(android.view.LayoutInflater inflater) {") {
   tab("return inflate(inflater, android.databinding.DataBindingUtil.getDefaultComponent());")
  }
  tab("}")
  tab("public static $baseClassName bind(android.view.View view) {") {
   if (forLibrary) {
    tab("return null;")
   } else {
    tab("return bind(view, android.databinding.DataBindingUtil.getDefaultComponent());")
   }
  }
  tab("}")
  tab("public static $baseClassName inflate(android.view.LayoutInflater inflater, android.view.ViewGroup root, boolean attachToRoot, android.databinding.DataBindingComponent bindingComponent) {") {
   if (forLibrary) {
    tab("return null;")
   } else {
    tab("return DataBindingUtil.<$baseClassName>inflate(inflater, ${layoutBinder.modulePackage}.R.layout.${layoutBinder.layoutname}, root, attachToRoot, bindingComponent);")
   }
  }
  tab("}")
  tab("public static $baseClassName inflate(android.view.LayoutInflater inflater, android.databinding.DataBindingComponent bindingComponent) {") {
   if (forLibrary) {
    tab("return null;")
   } else {
    tab("return DataBindingUtil.<$baseClassName>inflate(inflater, ${layoutBinder.modulePackage}.R.layout.${layoutBinder.layoutname}, null, false, bindingComponent);")
   }
  }
  tab("}")
  tab("public static $baseClassName bind(android.view.View view, android.databinding.DataBindingComponent bindingComponent) {") {
   if (forLibrary) {
    tab("return null;")
   } else {
    tab("return ($baseClassName)bind(bindingComponent, view, ${layoutBinder.modulePackage}.R.layout.${layoutBinder.layoutname});")
   }
  }
  tab("}")
  nl("}")
 }.generate()
}

那么问题来了,这里的这个只是用来使 library module 编译能通过的 abstract class,只生成了所有 variable 的 setter 方法啊,getter 呢?坑爹呢?

看来是 Google 压根没考虑到还需要这个。写 Kotlin 的都少根筋吗?

规避方案

为了让 library module 能编译通过(这样才能在 application module 生成真正的 Binding 实现),只好避免使用 getter 方法,幸而通过之前开发的 DataBindingAdapter 和 lambda presenter 确实能规避使用 getter 去拿 viewmodel。

不管怎么说,希望 Google 能在下个版本修复这个问题。就是 iterator 一下,写个 abstract 接口而已。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多