gpt4 book ai didi

java - 在Android上的注释中使用XmlPullParser

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:16 25 4
gpt4 key购买 nike

我正在尝试编写一个注释处理器来解析 XML 文件并从中生成类。该注释将在Android应用程序中使用,但我的理解是注释处理器本身运行在编译器的Java环境中。因此,我无法访问内置的 Android XmlPullParser,因此我将 org.ogce:xpp3:1.1.6 列为我的注释处理器的 gradle 文件中的依赖项。我的注释处理器是 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

我不知道如何解决这个问题。有没有办法可以将 XPP 包含在我的注释处理器的 Jar 中,但将其从最终的 APK 中排除?我还缺少其他方法吗?我不是第一个尝试在注释处理器中解析 XML 的人。

最佳答案

我认为您可能想要“隐藏”XPP jar。基本上,您需要下载 aar,提取 jar 文件,然后将其重新打包到不同的命名空间中(例如 shadow gradle plugin )并更改您的导入,以便它们引用隐藏的 jar。

关于java - 在Android上的注释中使用XmlPullParser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com