【发布时间】:2015-12-08 08:22:40
【问题描述】:
我正在尝试编写一个注释处理器来解析 XML 文件并从中生成类。注释将在Android应用程序中使用,但我的理解是注释处理器本身运行在编译器的Java环境中。因此,我无法访问内置的 Android XmlPullParser,因此我在注释处理器的 gradle 文件中将 org.ogce:xpp3:1.1.6 列为依赖项。我的注释处理器是 Android Studio 中与应用程序(Java 库模块)分开的模块。我的应用有一个依赖这个模块。
这主要是有效的!我的注释处理器运行并可以成功解析 XML。不幸的是,在为我的 android 应用生成 APK 时出现此错误:
trouble processing "javax/xml/namespace/QName.class":
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.
However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.
If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.
If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.
If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.
1 error; aborting
Error:Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
我认为,尽管错误中出现了意外的类名,但我不喜欢在我的应用程序中包含 XmlPullParser,因为它是由系统提供的。所以我在我的应用程序的 gradle 文件中执行了此操作,以将其从应用程序的 APK 中排除:
compile(project(':annotation-module')) {
exclude module: 'xpp3' // Android provides the XmlPull API so we can't include it
}
这会导致 Javac 在处理注解时出现编译时异常:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
我不知道如何解决这个问题。有没有办法我可以在我的注释处理器的 Jar 中包含 XPP 但从最终的 APK 中排除它?我还缺少其他方法吗?我不能成为第一个尝试在注释处理器中解析 XML 的人。
【问题讨论】:
标签: java android gradle annotations dependencies