javax.xml.bind 在 Android Studio(Gradle)中不起作用

Posted

技术标签:

【中文标题】javax.xml.bind 在 Android Studio(Gradle)中不起作用【英文标题】:javax.xml.bind not working in Android Studio(Gradle) 【发布时间】:2016-02-26 16:42:08 【问题描述】:

我最近将我的 android 项目从 maven 迁移到了 gradle。这样做之后,我无法在我的 java 文件中 import javax.xml.bind 。当项目在 maven 中时,这运行良好(可能是因为它是 java 项目而不是 android 项目)。

我在网上搜索,发现 JVM 支持 javax.xml.bind 但 DVM(Dalvik Virtual Machine) 不支持。所以我尝试将此库作为依赖项添加到我的 build.gradle 文件中,如下所示:

dependencies 
    ....
    compile 'javax.xml.bind:jaxb-api:2.2.x'
    ....

在进行上述更改后构建应用时,出现以下构建错误:

:dexDebug
Unknown source file : trouble processing "javax/xml/bind/annotation/XmlID.class":
Unknown source file : Ill-advised or mistaken usage of a core class (java.* or javax.*)
Unknown source file : when not building a core library.
Unknown source file : This is often due to inadvertently including a core library file
Unknown source file : in your application's project, when using an IDE (such as
Unknown source file : Eclipse). If you are sure you're not intentionally defining a
Unknown source file : core class, then this is the most likely explanation of what's
Unknown source file : going on.
Unknown source file : However, you might actually be trying to define a class in a core
Unknown source file : namespace, the source of which you may have taken, for example,
Unknown source file : from a non-Android virtual machine project. This will most
Unknown source file : assuredly not work. At a minimum, it jeopardizes the
Unknown source file : compatibility of your app with future versions of the platform.
Unknown source file : It is also often of questionable legality.
Unknown source file : If you really intend to build a core library -- which is only
Unknown source file : appropriate as part of creating a full virtual machine
Unknown source file : distribution, as opposed to compiling an application -- then use
Unknown source file : the "--core-library" option to suppress this error message.
Unknown source file : If you go ahead and use "--core-library" but are in fact
Unknown source file : building an application, then be forewarned that your application
Unknown source file : will still fail to build or run, at some point. Please be
Unknown source file : prepared for angry customers who find, for example, that your
Unknown source file : application ceases to function once they upgrade their operating
Unknown source file : system. You will be to blame for this problem.
Unknown source file : If you are legitimately using some code that happens to be in a
Unknown source file : core package, then the easiest safe alternative you have is to
Unknown source file : repackage that code. That is, move the classes in question into
Unknown source file : your own package namespace. This means that they will never be in
Unknown source file : conflict with core system classes. JarJar is a tool that may help
Unknown source file : you in this endeavor. If you find that you cannot do this, then
Unknown source file : that is an indication that the path you are on will ultimately
Unknown source file : lead to pain, suffering, grief, and lamentation.
Unknown source file : 1 error; aborting

:dexDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

我怎样才能成功导入这个库?我是 android 和 gradle 的初学者,所以任何帮助对我都有很大用处:)

【问题讨论】:

***.com/questions/7838675/… @IntelliJAmiya 上面的链接是 maven 特定的,但我们使用的是 gradle。另外,这是一个 android 项目,所以它在 DVM 下。 好的设置2.2.9而不是2.2.x @IntelliJAmiya 还是同样的错误 【参考方案1】:

您的项目正在导入一个核心类(java.* 或 javax.*)。

这是一种可以使用“JarJar”工具将“javax.xml.bind”命名空间更改为另一个(例如:“oop.xml.bind”)的方法,因此您可以解决此问题并将其导入为第三-Party 库,可以避免与核心类同命名空间。

祝你好运!

【讨论】:

【参考方案2】:

你试过了吗

 xjc 'javax.xml.bind:jaxb-api:2.2.x'

【讨论】:

以上是关于javax.xml.bind 在 Android Studio(Gradle)中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Android老項目出现javax/xml/bind/JAXBException异常问题解决

android studio 初始化必备控件 > Failed to notify project evaluation listener. > javax/xml/bind/annotation/

Android项目 app项目 java.lang.ClassNotFoundException: javax.xml.bind.JAXBException 出错解决办法不是java或spring项目

jaxb 编组时出现 javax.xml.bind.PropertyException

2021-8-15 SpringBoot 出现 javax.xml.bind.JAXBContext 错误

如何解决 java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException