Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding with Kotlin: ProcessDataBinding Unable to get public no-arg constructor

I get this error when I type ./gradlew test in the command line:

e: java.util.ServiceConfigurationError: javax.annotation.processing.Processor: android.databinding.annotationprocessor.ProcessDataBinding Unable to get public no-arg constructor
        at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:581)
        at ...
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:566)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
        ... 59 more


> Task :app:kaptDebugKotlin FAILED

FAILURE: Build failed with an exception.

It occurs also when I have kapt "androidx.databinding:databinding-compiler:$gradlePluginVersion" (both for 3.2.0 and 3.3.0-alpha13 versions) in build.gradle. I don't have other kapt dependencies. I have data binding enabled, it works and I can run tests via Android Studio (gradle task testDebugUnitTest works). I use embedded JDK.

like image 481
Wojtek Okoński Avatar asked Oct 07 '18 13:10

Wojtek Okoński


2 Answers

I fixed this problem by using Java 8 instead of Java 11. The same problem may appear with Java 9 and 10. I set my JAVA_HOME via export JAVA_HOME=/path/to/java/8 and then ran ./gradlew build. And voila, no error.

I had the exception e: java.util.ServiceConfigurationError: javax.annotation.processing.Processor: android.databinding.annotationprocessor.ProcessDataBinding Unable to get public no-arg constructor and java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException when I ran the build from the command line using gradle. But these didn't appear when I run the build from Android Studio. I suspect this is because Android Studio is somehow using its own version of Java.

like image 89
mmm111mmm Avatar answered Nov 19 '22 03:11

mmm111mmm


If you need to use databinding on Java 9+, you'll need to add JAX-B dependencies. Add this to your build.gradle's dependencies {} block:

// Add missing dependencies for JDK 9+
if (JavaVersion.current().ordinal() >= JavaVersion.VERSION_1_9.ordinal()) {
    // Add both if you're using both Kotlin and Java

    // If you're using Kotlin
    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"

    // If you're using Java
    annotationProcessor "com.sun.xml.bind:jaxb-core:2.3.0.1"
    annotationProcessor "javax.xml.bind:jaxb-api:2.3.1"
}
like image 21
Hieu Rocker Avatar answered Nov 19 '22 03:11

Hieu Rocker