亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

Library module 生成 class

在 library module 下啟用 Data Binding 很簡單,跟 application module 一樣,加上:

android { 
 dataBinding {
  enabled = true
 }
}

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

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

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

WTF?!

What happened

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

是我們姿勢錯了?Dagger2 生成哪里出問題了?還是 Data Binding 的 bug 呢?

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

在 CompilerChief 的 writeViewBinderInterfaces 中:

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

對應(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)類會調(diào)用 writeViewBinder):

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

可以看到如果是 library module,我們會做特殊的編譯,而不會生成真正的實(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()
}

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

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

規(guī)避方案

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

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

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

相關(guān)文章

  • Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條

    Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條

    這篇文章主要介紹了Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 深入Android 五大布局對象的應(yīng)用

    深入Android 五大布局對象的應(yīng)用

    本篇文章小編為大家介紹,深入Android 五大布局對象的應(yīng)用。需要的朋友參考下
    2013-04-04
  • Android 自定義Dialog 實(shí)例

    Android 自定義Dialog 實(shí)例

    本篇文章主要介紹Android自定義Dialog,在Android開發(fā)中,出于美觀系統(tǒng)自帶的Dialog一般都會被重新,這里給大家寫個小實(shí)例,大家可以參考下
    2016-07-07
  • Android隨手筆記44之JSON數(shù)據(jù)解析

    Android隨手筆記44之JSON數(shù)據(jù)解析

    本文將主要介紹在Android開發(fā)中,如何在服務(wù)器端創(chuàng)建JSON數(shù)據(jù),以及如何在Android客戶端對JSON數(shù)據(jù)進(jìn)行解析,對android json解析 相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android通過Java sdk的方式接入OpenCv的方法

    Android通過Java sdk的方式接入OpenCv的方法

    這篇文章主要介紹了Android通過Java sdk的方式接入OpenCv的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Android 有道詞典的簡單實(shí)現(xiàn)方法介紹

    Android 有道詞典的簡單實(shí)現(xiàn)方法介紹

    本篇文章小編為大家介紹,Android 有道詞典的簡單實(shí)現(xiàn)方法介紹。需要的朋友參考下
    2013-04-04
  • 基于Flutter制作一個火箭發(fā)射動畫

    基于Flutter制作一個火箭發(fā)射動畫

    北京時間10月16日0時23分,神舟十三號飛船成功發(fā)射,為慶祝這一喜事,本文將用Flutter制作一個火箭發(fā)射動畫,感興趣的小伙伴可以動手試一試
    2022-03-03
  • 利用百度地圖Android sdk高仿微信發(fā)送位置功能及遇到的問題

    利用百度地圖Android sdk高仿微信發(fā)送位置功能及遇到的問題

    這篇文章給大家介紹了利用百度地圖Android sdk高仿微信發(fā)送位置功能,在實(shí)現(xiàn)此功能的時候遇到點(diǎn)小問題,下面小編給大家列出來,需要的朋友參考下吧
    2017-12-12
  • Android 實(shí)例開發(fā)基于ArcSoft實(shí)現(xiàn)人臉識別

    Android 實(shí)例開發(fā)基于ArcSoft實(shí)現(xiàn)人臉識別

    人臉識別,是基于人的臉部特征信息進(jìn)行身份識別的一種生物識別技術(shù)。用攝像機(jī)或攝像頭采集含有人臉的圖像或視頻流,并自動在圖像中檢測和跟蹤人臉,進(jìn)而對檢測到的人臉進(jìn)行識別的一系列相關(guān)技術(shù),通常也叫做人像識別、面部識別
    2021-11-11
  • Android面試題問答整理

    Android面試題問答整理

    今天小編就為大家分享一篇關(guān)于Android面試題問答整理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12

最新評論