How to use javax.xml.transform.stax in Android Studio? - java

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.

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.

Related

Is there a way to import a specific version of a java sdk in a project with multiple versions of the same sdk?

Because of, reasons, I cannot just update the old version of the aws sdk I'm working with, but I also need some new things that are in a more recent version.
The problem is that if put both version of the sdk the project I get a "java.lang.NoSuchMethodError" because I think it's trying to use the old version of the sdk. If I delete the old one and just use the updated one it works fine. Is there a way to keep both version of the sdk and tell my java class which one to exclusively import?
There are a couple of ways, but they're pretty nasty.
The arguably "more correct" approach is to could use a custom classloader - see this answer for details. However, that's not exactly simple, and can lead to weird outcomes.
A simpler, but somewhat nastier approach is to get the source code of both SDKs (if available), and rename the packages. For example if we have sdk_v1 and sdk_v2, we can rename the packages to com.example.sdk.v1 and com.example.sdk.v2,
Once there's no package name collision, there's no problem using two different SDKs, even in the same class - just use fully qualified imports (see answer):
com.example.sdk.v1.SomeClass.someFunc() will not collide with com.example.sdk.v2.SomeClass.someFunc()

Android - redefining class removed from support library (AsyncTaskCompat)

Firstly, apologies if I have not included all the required info for this question to be answered. I am somewhat new to Android development and am still getting my head around the build tools, API levels etc. So please let me know if there is any additional info I should provide to help you help me!
After updating my Android project compile sdk version to 27, I realised that version 27.0.2 of com.android.support:support-v4 no longer includes AsyncTaskCompat (that class has been deprecated & removed).
I have a third party library that is not open source, not easily replaceable, is no longer supported and still uses AsyncTaskCompat.
Since AsyncTaskCompat is open source, I was thinking I could simply reintroduce it somehow by redefining it in my project.
I've tried redefining it under my project in com.android.support.v4.os but even though the project compiles without any issues, when I run the section of the app that uses the third party library I get a crash with a class not found error for AsyncTaskCompat.
Is there something obvious I might be missing?
No need to add a specific Android Library module.
Only add the classes bellow to your project using the package name "android.support.v4.os":
AsyncTaskCompat
AsyncTaskCompatHoneycomb
Answering my own question here after another day of hacking away.
It is in fact possible to re-implement these deprecated/removed classes in a way that the dependency will be able to use it.
The steps are described here in case anyone needs it in the future
Create a new Android Library module for your app
Reimplement the missing classes using the appropriate namespace
In my case I needed to reimplement android.support.v4.os.AsyncTaskCompat which is open sourced so all I had to do was copy the code from source.
Add the module as a dependency of your main app module.

How can I use the nonfree modules in OpenCV for Java?

I want to use SURF for feature detection found here and use it in a Java applicaion, however the nonfree modules are not included in the library by default, as they are patented.
How do I access this module? I have searched and tried a few things but none have worked; many focusing on Android, which I don't fully understand.
Can I add it when using cmake to build the library? or is there a better mean?
Actually when I was using cmake, nonfree library module was selected as default. So you should have this module built in your lib folder. Nonfree module includes some functionality that may be patented in some countries. So you should be careful if you are building a commercial application adn you will sell it in one of these countries.
To use it, you need to add it to your references and include the headers.
EDIT
I checked the docs here : http://docs.opencv.org/java/ there seems to be no module with name nonfree and there is no class related to sift or surf. I thought it should be same with c++ library but I was mistaken.
On the other hand, people claim that they built it for OpenCV4Android. That means it can be somehow compiled unofficially for java as well but no one seems to overcome this. Like here : http://answers.opencv.org/question/11185/how-can-i-generate-java-bindings-for-non-free/
Also there is a issue here about this : http://code.opencv.org/issues/2825
So at the moment no solution out yet. Implementing a JNI and loading compiled c++ nonfree lib may solve the problem by the way.
One more edit :)
In a tutorial, someone implemented JNI for nonfree module to be used with android. I don't have enough knowledge to try it for java at the moment. But a volunteer would be nice to try this with java :
http://web.guohuiwang.com/technical-notes/opencv_nonfree_android_jni_demo

java.lang.NoClassDefFoundError in android

I am new to Android development. I'm working on an project which involves using stubs for web services. When I try to use it, I get the following error:
I've been stuck here for a week, so some help would be highly appreciated.
It seems that there is some packaging or deployment issues with the included libraries that you use in you project. This error is thrown when the Java Virtual Machine or a ClassLoader instace try to load the definition of the a class but cannot find it anywhere. In most cases, this occurs when something is messed up in your project configurations but i cannot tell what from the information provided. A solution would be to configure your project from the scratch since most of the times this is easier than finding what causes the problem. Also in case you had an older version of ADT (<17) the answer of this SO question might be usefull. Finally, if you are importing any javax libararies(or libraries that have javax components in them), this maybe the source of your problem since Adroid does not support the javax library.

Are there any tools to re-namespace a Java library and its references?

This must be quite a common problem. An application depends on Library A and Library B. Library A depends on Library C-v1 (version 1 of Library C) and Library B depends on some newly introduced features in Library C-v2 (version 2 of Library C). And of course, there was some major refactoring in Library C-v2 which means it can't be backwards compatible with Library C-v1. But Library C-v1 and Library C-v2 have the same package names and class names.
Now, it should be possible to effectively "re-namespace" Library C-v1. For example, a class called com.example.MyClass could be re-namespaced to old.com.example.MyClass. It should be completely possible to build an automated tool that takes a binary JAR and changes all the package names in this fashion.
It should also be possible to change the references in a dependent library. For example, any reference in Library A to Library C should be changed to point to the new package names.
Surely there must already be a tool available to do this?
(PS: I'm sure some clever person will mention OSGi, but I want to do this as a one-time activity).
jarjar does what you are asking:
You can avoid problems where your library depends on a specific version of a library, which may conflict with the dependencies of another library.
The libraries are repackaged into your jar and renamed in the process. It also includes an Ant-Task.
Try the maven-shade-plugin. It will change packages you specify: http://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html
No, what you're talking about is major surgery, not a cosmetic renaming. There's no tool to help do this except for an IDE and lots of hard thinking.
You can't expect any warranty or certification of correct behavior to hold if those libraries are from a 3rd party.
I agree that it's a problem (e.g. the conflicts between Spring and Hibernate dependencies), but your solution isn't the way to go. This is what OSGi was born for.
If you had the source code, you could create an Ant task to do the refactoring, then reJar the old library.

Categories