1

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?

Community
  • 1
  • 1
Dieghitus
  • 55
  • 1
  • 9
  • "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" -- that is because they are not a part of Android. The `android.jar` that you compile against is a series of stubs, sufficient to satisfy the compiler. The real equivalent of that JAR is part of the Android firmware and is loaded into your process at runtime. Neither has those classes. See if there is a different version of Bluemix that is Android-compatible. – CommonsWare Jun 12 '16 at 20:28
  • If you're not able to find an Android library, you could always make a Java Web server that runs your IBM stuff and have the Android device call methods of that – OneCricketeer Jun 12 '16 at 20:31
  • Thanks for the help! I've commented also in the accepted answer, writing an additional explanation of the problem. – Dieghitus Jun 12 '16 at 21:29

1 Answers1

3

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).

Chris Thompson
  • 33,380
  • 11
  • 76
  • 105
  • Thank you for the clarification, I suspected this but it wasn't clear in my mind. In the application I'm using the Java SDK that supports Android: https://github.com/watson-developer-cloud/java-sdk#android The error that appears in runtime is this: "Could not find class 'javax.naming.InitialContext', referenced from method com.ibm.watson.developer_cloud.util.CredentialUtils.getKeyUsingJNDI". This part should work on Android 2.3 and higher, the API calls are done specifically for Android use... I don't know how to solve this problem :/ – Dieghitus Jun 12 '16 at 21:24
  • @Dieghitus I don't believe Android supports JDNI, which is the root of the problem. That's more of a Java EE feature – Chris Thompson Jun 12 '16 at 21:25
  • @Dieghitus ah, I suspect I see the issue. I'd be willing to bet (as other frameworks work this way) that you don't have your credentials configured in a properties file correctly, so it's trying to look them up via other means (JNDI being one of them). However, that isn't supported on Android. See https://github.com/watson-developer-cloud/java-sdk#getting-the-service-credentials for instructions on how to correctly configure the credentials. – Chris Thompson Jun 12 '16 at 21:27
  • Thanks for the tip. I've spotted this problem only with Visual Recognition service, all others work well. I'll work on this tomorrow, I hope to resolve. I'll write here news. Thank you! – Dieghitus Jun 12 '16 at 21:43
  • I have tried to resolve without success. It's from the update 2.7.0 that this error appears, not only to me but also to other users, as you can see in this other discussion on Bluemix forum: https://developer.ibm.com/answers/questions/260244/javax-content-not-accessible/ Is it possibile that this error may be caused by the version of the device (Android 4.4)? The other users also have this problem on 4.4. I've tried in every manner but nothing to do... – Dieghitus Jun 13 '16 at 10:15
  • @Dieghitus entirely possible. I'd try running on an emulated version that's running a different version of Android. However, more likely it's a bug in the IBM framework. No version of Android supports `javax.naming` – Chris Thompson Jun 13 '16 at 14:03
  • I've privately contacted the supervisor of the java-sdk library,explaining in detail the problem, and he has yet replayed that his staff will consider this bug and will try to resolve it. So I wait for some news on GitHub page. Thank you Chris! – Dieghitus Jun 13 '16 at 18:37