javax.xml.bind无法在Android Studio中工作(Gradle)

时间:2015-11-24 09:49:30

标签: java android xml android-gradle

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

我在网上搜索并发现JVM支持 javax.xml.bind ,但DVM(Dalvik虚拟机)并不支持。所以我尝试在 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的初学者,所以任何帮助都对我有用:)

2 个答案:

答案 0 :(得分:0)

它看到您的项目正在导入核心类(java。*或javax。*)。

这是一种可以使用" JarJar"工具改变" javax.xml.bind"命名空间到另一个(例如:" oop.xml.bind"),因此您可以解决此问题并将其作为第三方库导入,您可以避免使用具有核心类的相同名称空间。

祝你好运!

答案 1 :(得分:0)

你试过吗

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