国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Data Binding 在 library module 中遇到錯誤及解決辦法

2019-10-23 18:34:24
字體:
來源:轉載
供稿:網友

記一次 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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西畴县| 蕉岭县| 鄢陵县| 盐亭县| 红安县| 林口县| 汪清县| 繁峙县| 巧家县| 抚宁县| 德州市| 清镇市| 沙洋县| 武定县| 灵山县| 普格县| 白银市| 江华| 乌拉特中旗| 休宁县| 南宫市| 香格里拉县| 台江县| 金溪县| 迁西县| 合肥市| 新河县| 英超| 沅江市| 美姑县| 博白县| 肇庆市| 寿宁县| 宁乡县| 宜春市| 紫金县| 修水县| 贵定县| 汾西县| 华安县| 安陆市|