未能安装 android-sdk: “java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema”

2022-08-31 04:28:50

安装 android sdk 工具时,会发出以下错误:

java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

为什么会发生这种情况,如何解决?

调试输出:

$ java --version
java 9
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
$ brew cask install android-sdk
==> Caveats
We will install android-sdk-tools, platform-tools, and build-tools for you.
You can control android sdk packages via the sdkmanager command.
You may want to add to your profile:
  'export ANDROID_SDK_ROOT=/usr/local/share/android-sdk'

This operation may take up to 10 minutes depending on your internet connection.
Please, be patient.

==> Satisfying dependencies
==> Downloading https://dl.google.com/android/repository/sdk-tools-darwin-3859397.zip
Already downloaded: /Users/tomasnovella/Library/Caches/Homebrew/Cask/android-sdk--3859397,26.0.1.zip
==> Verifying checksum for Cask android-sdk
==> Installing Cask android-sdk
==> 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.SdkManagerCli.main(SdkManagerCli.java:117)
==>     at com.android.sdklib.tool.SdkManagerCli.main(SdkManagerCli.java:93)
==> Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
==>     at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
==>     at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
==>     at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
==>     ... 5 more
Error: Command failed to execute!

==> Failed command:
/usr/local/Caskroom/android-sdk/3859397,26.0.1/tools/bin/sdkmanager tools platform-tools build-tools;26.0.1

==> Standard Output of failed command:


==> Standard Error of failed command:
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.SdkManagerCli.main(SdkManagerCli.java:117)

答案 1

刚刚有这个错误,通过在Android Studio上下载,在并重新运行来解决Android SDK Command-line Tools (latest)Preferences > Appearance & Behavior > System Settings > Android SDK > SDK Toolsflutter doctor --android-licenses

Android SDK path on Android Studio

Command-line Tools

最后,将新工具添加到 PATH 中,在过时的工具之前,或类似工具中:.bashrc.zshrc

export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin

答案 2

今天早上我遇到了类似的问题(尝试使用Unity3D为Android构建)。我最终卸载了JDK9并安装了Java SE开发工具包8u144。希望这有帮助。

  1. brew cask uninstall java# 卸载 java9
  2. brew tap homebrew/cask-versions
  3. brew cask install java8# 安装 java8
  4. touch ~/.android/repositories.cfg# 没有此文件,下一步将发生错误
  5. brew install --cask android-sdk

推荐