Intellij 安卓项目在启用数据绑定的情况下无法启动



我正在关注Codelabs上的android课程,他们正在介绍databinding。根据代码,我需要做的就是在build.gradle文件中启用databinding,如下所示:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.lesson4"
minSdkVersion 18
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

但是,该项目无法构建到模拟器中,并且我看到错误代码:Unable to load class 'javax.xml.bind.JAXBException'.

  1. 我还进一步修改了activity_mainXML文件,以将ConstraintLayout包装在Layout标签中,但我认为错误出在 gradle 构建部分的某个地方。

  2. 我已经尝试了各种方法,例如apply plugin: 'kotlin-kapt'以及包含 kotlin 依赖项,但相同的错误仍然存在。

  3. 我也尝试清理项目并重建它。

  4. 我还尝试创建一个全新的项目,并且只启用databinding...相同的错误仍然存在。

希望有人能启发我如何在 smart 的 intellij 中让数据绑定工作?

经过一些研究,我发现在使用Java 9+时数据绑定会抛出错误。

您所要做的就是将Java设置为版本8,它应该可以工作。

我不想使用 Java 8

然后,您必须手动添加 JAX-B 依赖项

kapt "com.sun.xml.bind:jaxb-core:2.3.0.1"
kapt "javax.xml.bind:jaxb-api:2.3.1"
kapt "com.sun.xml.bind:jaxb-impl:2.3.2"
annotationProcessor "com.sun.xml.bind:jaxb-core:2.3.0.1" 
annotationProcessor "javax.xml.bind:jaxb-api:2.3.1"

并通过以下方式应用 kotlin-kapt 插件:

apply plugin: 'kotlin-kapt'

相关内容

最新更新