I am trying to call some Java code that I wrote from C using the Android NDK. The application is a NativeActivity application. I have to access some functionality that is only available in Java, and the functionality requires you to subclass another class, so I can't just directly do the calls from C. Thus, I have Java code like this:
// src/com/example/my/package/SubClass.java
package com.example.my.package;
import android.foo.TheSuperClass;
public class SubClass extends TheSuperClass {
public SubClass() {
setImportantProperty(true);
}
}
I also have C code like this:
// Some file.c
void setThatImportantJavaProperty() {
JavaVM *vm = AndroidGetJavaVM(); // This returns a valid JavaVM object
JNIEnv* env;
(*vm)->AttachCurrentThread(vm, &env, 0);
jclass theSubClass = (*env)->FindClass(env, "com/example/my/package/SubClass");
jthrowable exception = (*env)->ExceptionOccurred(env);
if (exception) {
(*env)->ExceptionDescribe(env);
// This gives me: "java.lang.NoClassDefFoundError: [generic]".
// Also, theSubClass is null, so the next line causes a segfault.
}
jmethodID theSubClassConstructor = (*env)->GetMethodID(env, theSubClass, "<init>", "()V");
jobject theSubClassObject = (*env)->NewObject(env, theSubClass, theSubClassConstructor);
(*env)->DeleteLocalRef(env, theSubClass);
(*env)->DeleteLocalRef(env, theSubClassConstructor);
(*env)->DeleteLocalRef(env, theSubClassObject);
(*vm)->DetachCurrentThread(vm);
}
As the inline comments say, running this gives me a "java.lang.NoClassDefFoundError: [generic]" error. When I unpack my apk, it shows me the classes.dex file, which appears to have my class in it. My guess is that there is some subtlety that I am missing regarding classpaths, but I am unable to resolve it thus far.
Incidentally, I am able to make similar calls to standard Android libraries from C without a problem (in the same C function above). Specifically, I tested calling Log.v and that works and prints the output correctly.
It seems that all the examples that I am finding only show how to call normal Java libraries from C, not Java libraries you have written yourself, so I haven't even found an example project to compare against.