簡體   English   中英

在Android上的注釋中使用XmlPullParser

[英]using XmlPullParser in annotation on Android

我正在嘗試編寫一個注釋處理器來解析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

我不知道如何解決這個問題。 是否無法將XPP包含在注解處理器的Jar中,但將其從最終的APK中排除呢? 我還缺少其他方法嗎? 我不能成為第一個嘗試在注釋處理器中解析XML的人。

我認為您可能想“遮蓋” XPP罐。 基本上,您需要下載aar,提取jar文件,然后將其重新打包到其他名稱空間中(例如shadow gradle plugin ),並更改導入,以便它們引用有陰影的jar。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM