新版Android Studio(4.2) 无法使用Databinding问题

1,402 阅读1分钟

对于多个Module并且还有build.gradle.kts(即Java+Kotlin混编项目且build.gradle是用Kotlin编写的),按照网上写的,所有的Module的build.gradle都加上以下配置:

android {
    ...
    buildFeatures {
        viewBinding =  true
    }
}

虽可能解决问题,但每个Module都得修改,且仍旧可能出现“Failed resolution of: Landroidx/databinding/DataBinderMapperImpl”问题。

终极办法是在工程的gradle.properties文件下,加入以下代码即可完美解决:

android.defaults.buildfeatures.databinding=true

来源于ApplicationBuildFeatures.kt源码:

/**
 * Flag to enable Data Binding.
 *
 * Setting the value to `null` resets to the default value.
 * Default value is `false`.
 *
 * `You can override the default for this for all projects in your build by adding the line
 *     `android.defaults.buildfeatures.databinding=true`
 * in the `gradle.properties` file at the root project of your build.`
 *
 * More information about this feature at: TBD
 */
var dataBinding: Boolean?