Unable to find custom java class from jar into a Android Native-Activity app

190 Views Asked by At

I'm working on an Android Native-Activity application with Visual Studio, starting from scratch (I'm trying to make it work in a basic test app first). It uses Ant to build the apk.

In my native code, I need to call a custom java method (not java native) coming from a jar file, so I plan on using JNI. I found that the jni->findClass wouldn't work because my java function is not native, thus not loaded in the classpath. I'm using this code instead to load the jniHelpers class from my jar :

    const ANativeActivity* activity = state->activity;
    activity->vm->AttachCurrentThread(&env, 0);

    jobject   jobj = activity->clazz;
    jclass    clazz = env->GetObjectClass(jobj);
    jmethodID getClassLoader = env->GetMethodID(clazz, "getClassLoader", "()Ljava/lang/ClassLoader;");
    jobject   cls = env->CallObjectMethod(jobj, getClassLoader);
    jclass    classLoader = env->FindClass("java/lang/ClassLoader");
    jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
    jstring strClassName = env->NewStringUTF("com.vivox.sdk.JniHelpers");
    jclass  jniHelpersClass = (jclass)(env->CallObjectMethod(cls, findClass, strClassName));
    env->DeleteLocalRef(strClassName);
    if (NULL == jniHelpersClass)
    {
        LOGW("Can't find the lib");
    }

Now my question is how can my java class be found ?

Following a couple of tutorials (listed below), I understand that my jar should be under a libs folder at the root of my *.Packaging, and loaded from the build.xml here :

    <target name="-pre-compile">
      <path id="project.all.jars.path">
        <path path="${toString:project.all.jars.path}"/>
        <fileset dir="${jar.libs.dir}">
          <include name="*.jar"/>
        </fileset>
      </path>
    </target>

My jar file seems to be properly copied (I think?) into the generated build, but it still not found by JNI/in the classpath. In the tutorials, they all create a custom activity to use System.LoadLibrary, but as soon as I set the AndroidManifest.xml-application:hasCode to true (thus not using the built-in NativeActivity framework class), I can't reach my c++ code anymore. Is it possible to load the jar file without creating a custom activity ? Or if not, how can I reach my native code after entering my custom activity ?

Thanks a lot, I've been stuck on this for way too long already, all help is more than appreciated !

Tutorials :

0

There are 0 best solutions below