Error in Cordova for Android: JDK/Android Target

Viewed 952

Here's what I got when checking requirements:

C:\Users\Ben\Desktop\app>cordova requirements
Requirements check results for android:
Java JDK: installed 14.0.2
Android SDK: installed true
Android target: not installed
Command failed with exit code 1: avdmanager list target
Picked up _JAVA_OPTIONS: -Xmx512M
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
        at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
        at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
        at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
        at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213)
        at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 5 more
Gradle: installed C:\Users\Ben\Downloads\gradle-6.6-all\gradle-6.6\bin\gradle.BAT
Some of requirements check failed

Here's what I got when trying to build:

C:\Users\Ben\Desktop\app>cordova build android
Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=undefined (recommended setting)
ANDROID_HOME=C:\Users\Ben\AppData\Local\Android\Sdk (DEPRECATED)
Using Android SDK: C:\Users\Ben\AppData\Local\Android\Sdk
Requirements check failed for JDK 8 ('1.8.*')! Detected version: 14.0.2
Check your ANDROID_SDK_ROOT / JAVA_HOME / PATH environment variables.

I have set my environment variables for ANDROID_HOME to C:\Users\Ben\AppData\Local\Android\Sdk, JAVA_HOME to C:\Program Files\Java\jdk-14.0.2, and add added C:\Program Files\Java\jdk-14.0.2\bin, sdk tools, sdk platform tools, and Gradle Bin to my PATH.

Also, I have Android Studio with SDK tool for 9.0 and 10.0+ installed.

How do I get this working?

1 Answers

I had the exact same issue (on Windows 10), and was able to solve it with these fixes:

  1. Check which environment paths are providing you with java by running for /F "tokens=*" %i in ('where java') do ("%i" -version). The first one is the most important, but if you see lots of different versions you might want to clean up your environment variables.
  2. Make sure you have the JDK for Java 8 (aka. 1.8.x) installed. If not you'll have to do that first. Mine ended up in C:\Program Files\Java\jdk1.8.0_201.
  3. Make sure your system environment variable isn't including java in the PATH or it will be hard to override it at the account level.
  4. Edit your account's PATH so it includes %JAVA_HOME%\bin. Remove any other paths to java.
  5. Edit your account's JAVA_HOME environment variable to point to C:\Program Files\Java\jdk1.8.0_201. If you just want to do a quick test you can run set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_201 in the command line.
  6. Add an ANDROID_SDK_ROOT account environment variable pointing to C:\Users\username\AppData\Local\Android\Sdk (with your username). I don't think this is strictly needed with this version, but seems this is recommended over using ANDROID_HOME for the future.
  7. Don't forget to restart your command prompt anytime you make an environment variable change. Test with @echo %JAVA_HOME% && @echo %ANDROID_SDK_ROOT% && where java.
  8. cordova requirements now gives happy "installed" responses for each item.
  9. cordova build android should now work.
Related