问题
I'm running into a strange problem in Android Studio. I'm also using latest JDK. I can't seem to import the following :
- javax.xml.stream
- javax.xml.transform.stax
They both are unavailable and I don't know why. In a non android project I'm able to import these. Any clue why these are missing in android?
If not, the simple solution is that I added these dependencies from maven. But after running I'm getting this error:
"Ill-advised or mistaken usage of a core class (java.* or javax.*) when not building a core library.
回答1:
Android doesn't use the same Java JDK as what you use for a desktop or server app; it uses its own implementation of the JDK, which adds the android.*
hierarchy but leaves out some packages that are in the standard JDK, especially in the javax.*
Java extensions. Unfortunately for you, javax.xml.stream
and javax.xml.transform.stax
are among the things they didn't choose to implement. Moreover, the Android runtime won't allow you to load any java.*
or javax.*
classes from an external JAR file for security reasons**. This answer may describe the situation a little better.
Your best bets are probably: 1) use a different third-party library specifically built for Android, such as SimpleXML; 2) look for a repackaged version of the libraries, like this JAXB effort; or 3) use a tool like Jar Jar Links to do the repackaging for you.
Also see this excellent guide on parsing JSON and XML in Android.
** Note: I can't find any documentation from Google to substantiate this; it's just a commonly repeated "fact". If anyone can point to an authoritative statement about Android's handling of third-party java[x].*
classes, please comment with a link, or edit this answer.
来源:https://stackoverflow.com/questions/36757089/how-to-use-javax-xml-transform-stax-in-android-studio