广告
返回顶部
首页 > 资讯 > 移动开发 >Android Data Binding 在 library module 中遇到错误及解决办法
  • 137
分享到

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

modulelibraryAndroid 2022-06-06 04:06:55 137人浏览 薄情痞子
摘要

记一次 Data Binding 在 library module 中遇到的大坑 使用 Data Binding 也有半年多了,从最初的 setVariable,替换 find

记一次 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 出现:java.lang.NoClassDefFoundError...错误解决办法Android 出现“Can''t bind to local 8602 for debugger”错误的解决方法Android studio报: java.lang.ExceptionInInitializerError 错误Android studio 出现错误Run with --stacktrace option to get the stack trace. Run with --info or --debu探究Android中ListView复用导致布局错乱的解决方案Android Listview 滑动过程中提示图片重复错乱的原因及解决方法Android模拟器无法启动,报错:Cannot set up guest memory ‘android_arm’ Invalid argument的解决方法Android程序报错程序包org.apache.Http不存在问题的解决方法Android DaggerActivityComponent错误解决办法详解


--结束END--

本文标题: Android Data Binding 在 library module 中遇到错误及解决办法

本文链接: https://www.lsjlt.com/news/22075.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作