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

Library module 生成 class
在 library module 下啟用 Data Binding 很簡(jiǎn)單,跟 application module 一樣,加上:
android {
dataBinding {
enabled = true
}
}對(duì)應(yīng)生成的 binding 類會(huì)在 manifest 里面指定的 package name 下的 databinding 包下。
坑
于是坑的地方就在這里了,編譯不過(guò)了…
為啥呢?報(bào)錯(cuò)說(shuō) symbol 找不到…于是在 module 的 build 下查看生成的 Binding 類…臥槽?!怎么是 abstract 的?怎么都找不到那些 get 方法了?雖然我也不知道為什么我們會(huì)從 binding 類里面去拿之前 set 進(jìn)去的 ViewModel。
WTF?!
What happened
Fuck 歸 fuck,究竟怎么回事還是要研究一下的。
是我們姿勢(shì)錯(cuò)了?Dagger2 生成哪里出問(wèn)題了?還是 Data Binding 的 bug 呢?
因?yàn)橹耙惭芯窟^(guò) data binding 生成部分的代碼,所以找到問(wèn)題所在沒(méi)有花太多時(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()
}那么問(wèn)題來(lái)了,這里的這個(gè)只是用來(lái)使 library module 編譯能通過(guò)的 abstract class,只生成了所有 variable 的 setter 方法啊,getter 呢?坑爹呢?
看來(lái)是 Google 壓根沒(méi)考慮到還需要這個(gè)。寫 Kotlin 的都少根筋嗎?
規(guī)避方案
為了讓 library module 能編譯通過(guò)(這樣才能在 application module 生成真正的 Binding 實(shí)現(xiàn)),只好避免使用 getter 方法,幸而通過(guò)之前開(kāi)發(fā)的 DataBindingAdapter 和 lambda presenter 確實(shí)能規(guī)避使用 getter 去拿 viewmodel。
不管怎么說(shuō),希望 Google 能在下個(gè)版本修復(fù)這個(gè)問(wèn)題。就是 iterator 一下,寫個(gè) abstract 接口而已。
關(guān)于怎么在Android中 library module 生成 class就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站題目:怎么在Android中l(wèi)ibrarymodule生成class-創(chuàng)新互聯(lián)
分享路徑:http://www.chinadenli.net/article2/dchjoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、靜態(tài)網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、建站公司、企業(yè)網(wǎng)站制作、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容