17

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.

Wojtek Okoński
  • 448
  • 1
  • 4
  • 12

4 Answers4

17

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.

mmm111mmm
  • 3,607
  • 4
  • 27
  • 44
  • 3
    Using Java 8 is a valid workaround - app builds. Is it possible though to run the build using JDK 11 somehow? – jskierbi Apr 12 '19 at 18:26
  • https://medium.com/@huih1108/debug-data-binding-with-kotlin-processdatabinding-unable-to-get-public-no-arg-constructor-5f52a710f3e5 – John Sep 24 '20 at 10:40
15

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"
}
Hieu Rocker
  • 1,070
  • 12
  • 19
0

Its because of Java version > 8

Point your JAVA_HOME to Java 8 version, it will work smoothly

Java 8 Download link

Details solution is here

John
  • 8,846
  • 8
  • 50
  • 85
0
java.util.ServiceConfigurationError: javax.annotation.processing.Processor: android.databinding.annotationprocessor.ProcessDataBinding Unable to get public no-arg constructor

I was getting this error because my 'build.gradle' , 'kotlin_version' and 'gradle wrapper' were not upto date.

ext.kotlin_version = '1.3.10' >> '1.3.20'  
build:gradle:3.5.2  >>  build:gradle:4.2.1  
distributions/gradle-5.4.1-all.zip >> distributions/gradle-6.7.1-all.zip
Prabs
  • 4,923
  • 6
  • 38
  • 59