app的gradle 配置如下
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
...
dependencies {
...
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
...
}
项目的gradle配置如下
buildscript {
ext.kotlin_version = '1.2.30'
...
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
activity 代码
class MainActivity : MyActivity() {
private var mBinding: AppActivityMainBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
init()
}
private fun init() {
mBinding = DataBindingUtil.setContentView(this, R.layout.app_activity_main)
}
}
本来今天开开心心,然后编译报红:
:unresolved reference databinding
修改:
在app的gradle里添加
apply plugin: 'kotlin-kapt'
dependencies {
...
kapt "com.android.databinding:compiler:3.1.3"
}
参考文献:
https://stackoverflow.com/questions/33165324/kotlin-android-unresolved-reference-databinding
/
-
更新 最近笔者把升级到了AndroidStudio3.2版本,以上方式报错
AndroidStudio下kotlin + dataBinding 编译报红
错误: 找不到符号 @Nullable DataBindingComponent component)
笔者错误原因,gradle 配置问题,错误的Gradle如下
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
······
dataBinding {
enabled true
}
}
dependencies {
kapt "com.android.databinding:compiler:3.1.4"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
......
}
解决方式
删除 kapt "com.android.databinding:compiler:3.1.4"