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。
不是很确定这是不是问题所在,作为记录,仅参考。