İbrahim
İbrahim

Reputation: 1021

How to use jni.h for Android?

I want to call Java codes included Android SDK classes from C++. I can call pure Java codes from C++ for desktop console application. But I cannot call Java codes included Android SDK classes from C++ for Android (I get this error: JNI_CreateJavaVM was not declared in this scope.). I want an example for Java Native Interface for Android. Have you got examples about JNI for Android? Also, I found JNI examples and codes for desktop only, no Android. Also I found different jni.h and libjvm.so files positions:

jni.h Directories: ------------------------------------

/usr/lib/jvm/java-8-oracle/include/jni.h
Android/Sdk/ndk-bundle/platforms/android-*/arch-arm/usr/include/jni.h
android-studio/jre/include/jni.h

libjvm.so Directories: --------------------------------

/home/username/android-studio/jre/jre/lib/amd64/server/libjvm.so
/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so

Upvotes: 2

Views: 1471

Answers (1)

ortisenderos
ortisenderos

Reputation: 370

To explain it simple, if you want to call a Java function from C++, you should implement a jni Callback.

You could follow the hello-jniCallback project from this android ndk examples repository.

Most of the examples are written in C rather than in C++, so you should do some changes. For example those functions in C like:

FindClass(env, someString)

In C++ you should use:

env->FindClass(someString)

Upvotes: 2

Related Questions