0

I have an Android project in pure native code. Now, I need to use a third party jar file with it. How can I build my project with the jar file added?

Here, FindClass returns NULL because my jar file is not added in the JavaVM.

ANativeActivity *activity = __state->activity;
JavaVM *jvm = __state->activity->vm;
JNIEnv *env = NULL;

jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
jint res = jvm->AttachCurrentThread(&env, NULL);

jclass cls = env->FindClass("MyJavaClass"); //cls is NULL

jvm->DetachCurrentThread();

I tried creating an another jvm but it is not supported in Android. Though it's not a good idea even if it is supported.

JavaVMInitArgs vm_args;
JavaVMOption* options = new JavaVMOption[1];
std::string str = "-Djava.class.path=res/test.jar"; //Add the jar in jvm
options[0].optionString = (char*)str.c_str();
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;

JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

How can I add my jar correctly? I'm guessing in Android.mk file but I don't know what or how.

  • This looks wrong: `cls = env->FindClass("MyJavaClass");`. I'd expect to see the name similar to `com/example/MyJavaClass`. (Or maybe I've been using an overly verbose name all this time). (And I'm not sure why you were downvoted - this seems like a interesting question for a unique problem). – jww Sep 13 '14 at 02:26
  • @jww I am using an example jar file and the class in not in a package –  Sep 13 '14 at 03:10
  • I finally found the answer as discussed [here][1]. [1]: http://stackoverflow.com/questions/13263340/findclass-from-any-thread-in-android-jni –  Sep 13 '14 at 06:00

0 Answers0