最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年03月17日 08:39:31   作者:魔都三帥  
這篇文章主要介紹了Android   Data Binding 在 library module 中遇到錯(cuò)誤及解決辦法的相關(guān)資料,需要的朋友可以參考下

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

使用 Data Binding 也有半年多了,從最初的 setVariable,替換 findViewById,到比較高級(jí)的雙向綁定,自定義 Adapter、Component,查看源碼了解編譯、運(yùn)行流程,也算是小有成果,且沒有碰到 Data Binding 本身實(shí)現(xiàn)上的問題。

然而,最近在一次重構(gòu)組件化(見 MDCC 上馮森林的《回歸初心,從容器化到組件化》)的過程中,碰到了一個(gè)比較嚴(yán)重的 BUG。已經(jīng)提交 issue(#224048)到了 AOSP,雖然改起來是不麻煩,但是因?yàn)槭?gradle plugin,所以 - -,還是讓 Google 自己來吧。希望能早日修復(fù)。

Library module 生成 class

在 library module 下啟用 Data Binding 很簡(jiǎn)單,跟 application module 一樣,加上:

android { 
 dataBinding {
  enabled = true
 }
}

對(duì)應(yīng)生成的 binding 類會(huì)在 manifest 里面指定的 package name 下的 databinding 包下。

于是坑的地方就在這里了,編譯不過了…

為啥呢?報(bào)錯(cuò)說 symbol 找不到…于是在 module 的 build 下查看生成的 Binding 類…?!怎么是 abstract 的?怎么都找不到那些 get 方法了?雖然我也不知道為什么我們會(huì)從 binding 類里面去拿之前 set 進(jìn)去的 ViewModel。

WTF?!

What happened

究竟怎么回事還是要研究一下的。

是我們姿勢(shì)錯(cuò)了?Dagger2 生成哪里出問題了?還是 Data Binding 的 bug 呢?

因?yàn)橹耙惭芯窟^ data binding 生成部分的代碼,所以找到問題所在沒有花太多時(shí)間,這里不多啰嗦,直接看對(duì)應(yīng)位置。

在 CompilerChief 的 writeViewBinderInterfaces 中:

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

對(duì)應(yīng) 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();
  }
 }
}

這里調(diào)用了 LayoutBinder(真正的實(shí)現(xiàn)類會(huì)調(diào)用 writeViewBinder):

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

可以看到如果是 library module,我們會(huì)做特殊的編譯,而不會(huì)生成真正的實(shí)現(xiàn):

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()
}

那么問題來了,這里的這個(gè)只是用來使 library module 編譯能通過的 abstract class,只生成了所有 variable 的 setter 方法啊,getter 呢?坑爹呢?

看來是 Google 壓根沒考慮到還需要這個(gè)。寫 Kotlin 的都少根筋嗎?

規(guī)避方案

為了讓 library module 能編譯通過(這樣才能在 application module 生成真正的 Binding 實(shí)現(xiàn)),只好避免使用 getter 方法,幸而通過之前開發(fā)的 DataBindingAdapter 和 lambda presenter 確實(shí)能規(guī)避使用 getter 去拿 viewmodel。

不管怎么說,希望 Google 能在下個(gè)版本修復(fù)這個(gè)問題。就是 iterator 一下,寫個(gè) abstract 接口而已。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Android Handler消息派發(fā)機(jī)制源碼分析

    Android Handler消息派發(fā)機(jī)制源碼分析

    這篇文章主要為大家詳細(xì)分析了Android Handler消息派發(fā)機(jī)制源碼,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 如何在Android studio 中使用單例模式

    如何在Android studio 中使用單例模式

    這篇文章主要介紹了如何在Android studio 中使用單例模式,幫助大家更好的理解和學(xué)習(xí)Android開發(fā),感興趣的朋友可以了解下
    2021-03-03
  • Android仿淘寶搜索聯(lián)想功能的示例代碼

    Android仿淘寶搜索聯(lián)想功能的示例代碼

    本篇文章主要介紹了Android仿淘寶搜索聯(lián)想功能的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 如何使用Flutter實(shí)現(xiàn)58同城中的加載動(dòng)畫詳解

    如何使用Flutter實(shí)現(xiàn)58同城中的加載動(dòng)畫詳解

    這篇文章主要給大家介紹了關(guān)于如何使用Flutter實(shí)現(xiàn)58同城中加載動(dòng)畫詳?shù)南嚓P(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Android dumpsys簡(jiǎn)介

    Android dumpsys簡(jiǎn)介

    本文詳細(xì)介紹了Android系統(tǒng)中的dumpsys工具,包括其原理、使用方法、環(huán)境要求和相關(guān)概念,dumpsys是一個(gè)強(qiáng)大的系統(tǒng)診斷工具,可以幫助開發(fā)者了解系統(tǒng)服務(wù)狀態(tài),分析問題,并設(shè)計(jì)新功能,感興趣的朋友一起看看吧
    2025-03-03
  • Android三級(jí)緩存原理講解

    Android三級(jí)緩存原理講解

    今天小編就為大家分享一篇關(guān)于Android三級(jí)緩存原理講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Android權(quán)限操作之uses-permission詳解

    Android權(quán)限操作之uses-permission詳解

    這篇文章主要介紹了Android權(quán)限操作之uses-permission,較為詳細(xì)的分析了uses-permission常見權(quán)限操作類型與功能,需要的朋友可以參考下
    2016-10-10
  • Android仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄的問題

    Android仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄的問題

    這篇文章主要介紹了Android之仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄問題,本文實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 分享幾個(gè)Android開發(fā)有用的程序代碼

    分享幾個(gè)Android開發(fā)有用的程序代碼

    本文主要是給大家分享了幾個(gè)常用而且很實(shí)用的程序代碼片段,都是個(gè)人項(xiàng)目中提取出來的,有需要的小伙伴可以直接拿走使用
    2015-02-02
  • Flutter使用Overlay與ColorFiltered新手引導(dǎo)實(shí)現(xiàn)示例

    Flutter使用Overlay與ColorFiltered新手引導(dǎo)實(shí)現(xiàn)示例

    這篇文章主要介紹了Flutter使用Overlay與ColorFiltered新手引導(dǎo)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評(píng)論

安龙县| 阿勒泰市| 泌阳县| 万载县| 错那县| 定西市| 阿拉善右旗| 巴楚县| 镇安县| 沈阳市| 鞍山市| 镇赉县| 林周县| 山东| 莎车县| 西平县| 孟州市| 桐乡市| 乐亭县| 汝阳县| 米脂县| 莱州市| 乌苏市| 阿荣旗| 扬州市| 香河县| 区。| 涿州市| 金阳县| 神池县| 铅山县| 阳曲县| 贵溪市| 甘孜县| 崇礼县| 沂源县| 松潘县| 金阳县| 陇川县| 武定县| 开原市|