Search code examples
androidkotlinintellij-ideaandroid-databinding

Intellij android project unable to launch with databinding enabled


I was following the android lessons on codelabs and they were introducing databinding. According to the code, all i needed to do was to enable databinding within the build.gradle file as below:

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'
}

However, the project fails to build into the emulator, and I am presented with the error code: Unable to load class 'javax.xml.bind.JAXBException'.

  1. I also went further to amend the XML file of activity_main to wrap the ConstraintLayout within a Layout tag, but I think the error is somewhere within the gradle build portion instead.

  2. I have tried various methods, such as apply plugin: 'kotlin-kapt' as well as including the kotlin dependencies, but the same error still persist.

  3. I have also tried cleaning the project and rebuilding it.

  4. I have also tried creating a brand new project, and only enabling databinding... The same error persists.

Hope someone can enlighten me about how to get databinding working in intellij for android?


Solution

  • Upon some research I have found that data binding will throw errors while using Java 9+.

    All you have to do is set your Java to version 8 and it should work.

    I don't want to use Java 8

    Then you have to manually add the JAX-B dependencies

    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"
    

    And apply the kotlin-kapt plugin with:

    apply plugin: 'kotlin-kapt'