啓用視圖綁定,其中一個 Module 編譯時報:Unresolved reference: databinding

AS 版本:3.6.3,Gradle 版本:6.5-milestone-1

啓用前,編譯正常。修改 build.gradle(Module),增加 

viewBinding{
        enabled = true
    }

結果編譯時,提示無法找到生成的視圖 binding 類。

令人奇怪的是,主程序的 Module 編譯正常,可以使用視圖 binding 類,而包含進來的 module 無法通過。報錯:

Unresolved reference: databinding

仔細查看 layout.xml 文件,發現主程序的 tools:context=XXX 使用的是相對包名,而子 module 的 layout.xml 文件使用的是絕對的包名。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:padding="5dp"
        tools:context="com.domain.project.components.myview">

子 module 的根包名在 manifest.xml 是定義爲:com.domain.project.components,嘗試將 tools.context 的值修改爲 “.myview”,再次編譯成功,不再提示 unresolved reference: databinding。

不是很確定這是不是問題所在,作爲記錄,僅參考。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章