怎么在Android中l(wèi)ibrarymodule生成class-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關怎么在Android中 library module 生成 class,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

成都創(chuàng)新互聯(lián)公司專注于臨渭區(qū)網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供臨渭區(qū)營銷型網站建設,臨渭區(qū)網站制作、臨渭區(qū)網頁設計、臨渭區(qū)網站官網定制、小程序制作服務,打造臨渭區(qū)網絡公司原創(chuàng)品牌,更為您提供臨渭區(qū)網站排名全網營銷落地服務。

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(真正的實現(xiàn)類會調用 writeViewBinder):

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

可以看到如果是 library module,我們會做特殊的編譯,而不會生成真正的實現(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 實現(xiàn)),只好避免使用 getter 方法,幸而通過之前開發(fā)的 DataBindingAdapter 和 lambda presenter 確實能規(guī)避使用 getter 去拿 viewmodel。

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

關于怎么在Android中 library module 生成 class就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網站題目:怎么在Android中l(wèi)ibrarymodule生成class-創(chuàng)新互聯(lián)
分享路徑:http://muchs.cn/article2/dchjoc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、靜態(tài)網站、營銷型網站建設、建站公司、企業(yè)網站制作定制開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網站建設