Dieghitus Dieghitus - 5 months ago 184
Android Question

Android Studio - android.jar and rt.jar conflicts

I’m developing an Android Application with Android Studio 2.1.2.

In one of the classes of an external library (in particular the Watson library of IBM Bluemix,

com.ibm.watson.developer_cloud:java-sdk:3.0.1
) I need to import these three classes:

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


The problem is that these importations are not possible (Cannot resolve symbols). This is my scenario:

android.jar

rt.jar

When I try to import a javax package, I only see the ones that are in the android.jar (crypto, microedition, net, security, sql, xml). I don’t see the ones that are in rt.jar, so I can’t use the classes that are present in javax.naming package, for example.

I’ve tried to explicitly add the dependency in this way

compile files ('C:/Program Files/Java/jdk1.8.0_77/jre/lib/rt.jar')


but I think this is not correct, in fact it compiles but I can’t run on the device because of this error:

Error:Execution failed for task ':app:transformClassesWithMultidexlistForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 1


It’s the same problem I’ve encountered in this post: Javax content not accessible, but this time the problem is independent from the specific Watson Library that request the importations.

What is the way to make visible that classes to let the application work?

Answer

You cannot use rt.jar with an Android application (and somewhat overlaps with android.jar). That is the runtime library for the desktop JRE, not Android. Please see the Android API reference for a list of available packages (you'll notice that javax.naming is not one of them.

Clearly that IBM framework is not Android compatible.

My earlier assessment is inaccurate as the README clearly states that it is:

The library supports Android 2.3 and above. For Java, the minimum requirement is 1.7. It depends on OkHttp and gson.

Source.

However, I suspect the issue is related to incorrectly configured credentials (see here) causing the library to then attempt JNDI lookup as a fallback (which definitely isn't supported by Android).

Comments