Android Jetpack 之 ViewBinding

在 Kotlin 1.4.20-M2 中舍扰,JetBrains 廢棄了 Kotlin Android Extensions 編譯插件。

不要與Data Binding混淆侣诵。View Binding是一種功能痢法,它允許您更容易地編寫與視圖交互的代碼。
一旦在一個模塊中啟用了View Binding窝趣,它就會為該模塊中存在的每個 XML 布局文件生成一個綁定類疯暑。

  • 綁定類的實例包含對相應布局中具有ID的所有VIew的直接引用。
  • View Binding對于在多個配置中定義的布局來說是Null-safe的哑舒。
  • View Binding將檢測視圖是否只存在于某些配置中妇拯,并創(chuàng)建一個@Nullable屬性。
  • View Binding適用于Java和Kotlin。

禁用某個布局文件使用 ViewBinding越锈,則需要在布局文件的根節(jié)點中添加 tools:viewBindingIgnore = "true"仗嗦。

環(huán)境配置

  1. Android Studio 3.6 及以上;
  2. Android Gradle 插件 3.6.0 及以上甘凭;
  3. Gradle 版本 5.6.4 及以上稀拐;
  4. 在模塊的 build.gradle 文件中添加 android { buildFeatures { viewBinding = true } }android { viewBinding.enabled = true } 方式已廢棄丹弱。
    DSL element 'android.viewBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.viewBinding'.

GradlePlugin 和 Gradle 要對應德撬,否則會不兼容,出現(xiàn)同步失敗的問題躲胳。查看 Android Gradle 插件版本說明蜓洪。

使用

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.textView.text = "text"
    }
}

Activity 對應的 activity_main.xml 文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

生成的 ActivityMainBinding 在 module/build/generated/data_binding_base_class_source_out/${buildTypes}/out/${包名}/databinding 目錄,源碼如下:

public final class ActivityMainBinding implements ViewBinding {
  @NonNull
  private final LinearLayout rootView;

  @NonNull
  public final TextView textView;

  private ActivityMainBinding(@NonNull LinearLayout rootView, @NonNull TextView textView) {
    this.rootView = rootView;
    this.textView = textView;
  }

  @Override
  @NonNull
  public LinearLayout getRoot() {
    return rootView;
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater) {
    return inflate(inflater, null, false);
  }

  @NonNull
  public static ActivityMainBinding inflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent,
      boolean attachToParent) {
    View root = inflater.inflate(R.layout.activity_main, parent, false);
    if (attachToParent) {
      parent.addView(root);
    }
    return bind(root);
  }

  @NonNull
  public static ActivityMainBinding bind(@NonNull View rootView) {
    // The body of this method is generated in a way you would not otherwise write.
    // This is done to optimize the compiled bytecode for size and performance.
    int id;
    missingId: {
      id = R.id.textView;
      TextView textView = rootView.findViewById(id);
      if (textView == null) {
        break missingId;
      }

      return new ActivityMainBinding((LinearLayout) rootView, textView);
    }
    String missingId = rootView.getResources().getResourceName(id);
    throw new NullPointerException("Missing required view with ID: ".concat(missingId));
  }
}

Binding 類生成原理

由于 ViewBinding 類是通過 GradlePlugin 支持的坯苹,編譯時包含有 DataBinding 相關的 task Task :app:dataBindingGenBaseClassesDebug隆檀。

com.android.tools.build:gradle:4.1.3 依賴 androidx.databinding:databinding-compiler-common:4.1.3,下載 databinding-compiler-common jar 包粹湃,解壓到文件夾后使用 AS 打開查看源碼恐仑。

解析 xml 文件

/**
 * Processes the layout XML, stripping the binding attributes and elements
 * and writes the information into an annotated class file for the annotation
 * processor to work with.
 */
public class LayoutXmlProcessor {
  public boolean processResources(ResourceInput input, boolean isViewBindingEnabled,
        boolean isDataBindingEnabled) {

        ProcessFileCallback callback = new ProcessFileCallback() {
            @Override
            public void processLayoutFile(File file) {
                processSingleFile(...);
            }

            // 其他回調方法
        };

        if (input.isIncremental()) {
            processIncrementalInputFiles(input, callback);
        } else {
            processAllInputFiles(input, callback);
        }
    }

    private static void processAllInputFiles(ResourceInput input, ProcessFileCallback callback) {
        for (File firstLevel : input.getRootInputFolder().listFiles()) {
            if (firstLevel.isDirectory()) {
                // 判斷文件名是否已 layout 開頭
                if (LAYOUT_FOLDER_FILTER.accept(firstLevel, firstLevel.getName())) {
                    callback.processLayoutFolder(firstLevel);
                    // 過濾以 “.xml” 結尾的文件
                    for (File xmlFile : firstLevel.listFiles(XML_FILE_FILTER)) {
                        callback.processLayoutFile(xmlFile);
                    }
                }
            }
        }
    }

    public boolean processSingleFile(RelativizableFile input, File output,
                        boolean isViewBindingEnabled,
                        boolean isDataBindingEnabled) {
        // 解析 xml
        final ResourceBundle.LayoutFileBundle bindingLayout = LayoutFileParser.parseXml(input, output, ...);
        // 緩存解析的 xml
        mResourceBundle.addLayoutBundle(bindingLayout, true);
    }
}
public final class LayoutFileParser {

    public static ResourceBundle.LayoutFileBundle parseXml(final RelativizableFile input,
            final File outputFile, ...) {
        return parseOriginalXml(
                RelativizableFile.fromAbsoluteFile(originalFile, input.getBaseDir()),...);
    }

    private static ResourceBundle.LayoutFileBundle parseOriginalXml(
            final RelativizableFile originalFile, ...) {
        File original = originalFile.getAbsoluteFile();
        // 是否是 DataBinding
        if (isBindingData) {
            data = getDataNode(root);
            rootView = getViewNode(original, root);
        } else if (isViewBindingEnabled) {
            // 排除不生成 Binding 類的 xml
            if ("true".equalsIgnoreCase(attributeMap(root).get("tools:viewBindingIgnore"))) {
                L.d("Ignoring %s for view binding", originalFile);
                return null;
            }
            data = null;
            rootView = root;
        } else {
            return null;
        }

        // 判斷是否是 merge 節(jié)點
        boolean isMerge = "merge".equals(rootView.elmName.getText());
        if (isBindingData && isMerge && !filter(rootView, "include").isEmpty()) {
            L.e(ErrorMessages.INCLUDE_INSIDE_MERGE);
            return null;
        }

        String rootViewType = getViewName(rootView);
        // 獲取 View ID
        String rootViewId = attributeMap(rootView).get("android:id");
        // 創(chuàng)建 LayoutFileBundle 對象
        ResourceBundle.LayoutFileBundle bundle =
            new ResourceBundle.LayoutFileBundle(originalFile, ...);
        // ViewBinding data == null,不會解析
        parseData(original, data, bundle);
        // 解析表達式
        parseExpressions(newTag, rootView, isMerge, bundle);

        return bundle;
    }

    private static String getViewName(XMLParser.ElementContext elm) {
        String viewName = elm.elmName.getText();
        if ("view".equals(viewName)) {
            String classNode = attributeMap(elm).get("class");
            if (Strings.isNullOrEmpty(classNode)) {
                L.e("No class attribute for 'view' node");
            }
            return classNode;
        }
        if ("include".equals(viewName) && !XmlEditor.hasExpressionAttributes(elm)) {
            return "android.view.View";
        }
        if ("fragment".equals(viewName)) {
            return "android.view.View";
        }
        return viewName;
    }
}

生成 xml 文件

public class LayoutXmlProcessor {
    public void writeLayoutInfoFiles(File xmlOutDir, JavaFileWriter writer) {
        for (ResourceBundle.LayoutFileBundle layout : mResourceBundle
                .getAllLayoutFileBundlesInSource()) {
            writeXmlFile(writer, xmlOutDir, layout);
        }
    }

    private void writeXmlFile(JavaFileWriter writer, File xmlOutDir,
            ResourceBundle.LayoutFileBundle layout) {
        // layout.getFileName() + '-' + layout.getDirectory() + ".xml
        // 如 activity_main.xml -> activity_main-layout.xml
        String filename = generateExportFileName(layout);
        writer.writeToFile(new File(xmlOutDir, filename), layout.toXML());
    }
}

生成的 xml 文件路徑為 module/build/intermediates/data_binding_layout_info_type_merge/${buildTypes}/out/activity_main-layout.xml为鳄,描述了原始布局文件的相關信息裳仆。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Layout
    directory="layout"
    filePath="app\src\main\res\layout\activity_main.xml"
    isBindingData="false"
    isMerge="false"
    layout="activity_main"
    modulePackage="com.king.app.workhelper"
    rootNodeType="android.widget.LinearLayout">

    <Targets>

        <Target
            tag="layout/activity_main_0"
            view="LinearLayout">

            <Expressions />

            <location
                endLine="11"
                endOffset="14"
                startLine="1"
                startOffset="0" />
        </Target>

        <Target
            id="@+id/textView"
            view="TextView">

            <Expressions />

            <location
                endLine="9"
                endOffset="46"
                startLine="6"
                startOffset="4" />
        </Target>
    </Targets>
</Layout>

生成 Binding 類

class BaseDataBinder(val input : LayoutInfoInput) {
    fun generateAll(writer : JavaFileWriter) {
        input.invalidatedClasses.forEach {
            writer.deleteFile(it)
        }

        val useAndroidX = input.args.useAndroidX
        val libTypes = LibTypes(useAndroidX = useAndroidX)

        // 獲取所有的 LayoutFileBundle,并根據(jù)文件名進行分組排序
        val layoutBindings = resourceBundle.allLayoutFileBundlesInSource
            .groupBy(LayoutFileBundle::getFileName)

        // 遍歷 layoutBindings
        layoutBindings.forEach { layoutName, variations ->
            // 將 LayoutFileBundle 信息包裝成 BaseLayoutModel
            val layoutModel = BaseLayoutModel(variations)

            val javaFile: JavaFile
            val classInfo: GenClassInfoLog.GenClass
            // 處理 DataBinding
            if (variations.first().isBindingData) {
                //...
            } else {
                // 處理 ViewBinding
                // 創(chuàng)建 ViewBinder 對象
                val viewBinder = layoutModel.toViewBinder()
                // 通過 ViewBinder 擴展方法济赎,調用到 ViewBinderGenerateJava#create() 方法
                javaFile = viewBinder.toJavaFile(useLegacyAnnotations = !useAndroidX)
                classInfo = viewBinder.generatedClassInfo()
            }

            writer.writeToFile(javaFile)
        }
    }
}
// ViewBinderGenerateJava.kt

fun ViewBinder.toJavaFile(useLegacyAnnotations: Boolean = false) =
    JavaFileGenerator(this, useLegacyAnnotations).create()

private class JavaFileGenerator(
    private val binder: ViewBinder,
    private val useLegacyAnnotations: Boolean) {
    
    // 使用了 com.squareup.javapoet 庫
    fun create() = javaFile(binder.generatedTypeName.packageName(), typeSpec()) {
        addFileComment("Generated by view binder compiler. Do not edit!")
    }

    private fun typeSpec() = classSpec(binder.generatedTypeName) {
        addModifiers(PUBLIC, FINAL)

        val viewBindingPackage = if (useLegacyAnnotations) "android" else "androidx"
        addSuperinterface(ClassName.get("$viewBindingPackage.viewbinding", "ViewBinding"))

        // 生成 rootView 字段
        addField(rootViewField())
        // 生成 View 字段
        addFields(bindingFields())

        // 生成構造方法
        addMethod(constructor())
        // 生成獲取 rootView 的方法
        addMethod(rootViewGetter())

        if (binder.rootNode is RootNode.Merge) {
            addMethod(mergeInflate())
        } else {
            // 生成 inflate(LayoutInflater inflater) 方法
            addMethod(oneParamInflate())
            // 生成 inflate(LayoutInflater inflater, ViewGroup parent, boolean attachToParent) 方法
            addMethod(threeParamInflate())
        }

        addMethod(bind())
    }
}
  1. 遍歷收集的 Set<LayoutFileBundle>;
  2. 根據(jù) LayoutFileBundle 生成 ViewBinder 對象鉴逞;
  3. ViewBinder 傳參給 JavaFileGenerator,通過 JavaPoet(com.squareup.javapoet) 庫生成 Binding Class 文件司训。

參考

[1] ViewBinding - Developer
[2] 終于有人寫:「新技術ViewBinding」 的本質了
[3] [譯]深入研究ViewBinding 在 include, merge, adapter, fragment, activity 中使用
[4] hoc081098/ViewBindingDelegate

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末构捡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子壳猜,更是在濱河造成了極大的恐慌勾徽,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件统扳,死亡現(xiàn)場離奇詭異喘帚,居然都是意外死亡,警方通過查閱死者的電腦和手機咒钟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門吹由,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人朱嘴,你說我怎么就攤上這事倾鲫∮涫剩” “怎么了捷犹?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵揽乱,是天一觀的道長尾组。 經(jīng)常有香客問我,道長磕道,這世上最難降的妖魔是什么供屉? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮溺蕉,結果婚禮上伶丐,老公的妹妹穿的比我還像新娘。我一直安慰自己焙贷,他們只是感情好撵割,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布贿堰。 她就那樣靜靜地躺著辙芍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪羹与。 梳的紋絲不亂的頭發(fā)上故硅,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音纵搁,去河邊找鬼吃衅。 笑死,一個胖子當著我的面吹牛腾誉,可吹牛的內容都是我干的徘层。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼利职,長吁一口氣:“原來是場噩夢啊……” “哼趣效!你這毒婦竟也來了?” 一聲冷哼從身側響起猪贪,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤跷敬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后热押,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體西傀,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年桶癣,在試婚紗的時候發(fā)現(xiàn)自己被綠了拥褂。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡牙寞,死狀恐怖饺鹃,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤尤慰,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布馏锡,位于F島的核電站,受9級特大地震影響伟端,放射性物質發(fā)生泄漏杯道。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一责蝠、第九天 我趴在偏房一處隱蔽的房頂上張望党巾。 院中可真熱鬧,春花似錦霜医、人聲如沸齿拂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽署海。三九已至,卻和暖如春医男,著一層夾襖步出監(jiān)牢的瞬間砸狞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工镀梭, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留刀森,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓报账,卻偏偏與公主長得像研底,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子透罢,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容