Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JNI types to Native types

While there is documentation regarding turning a jstring to a native string (string nativeString = env->GetStringUTFChars(jStringVariable, NULL);) I can't find an example which will convert a jboolean to a bool or a jint to an int.

Can anyone suggest how this is achieved?

like image 984
Graeme Avatar asked Jun 10 '11 15:06

Graeme


People also ask

What is native interface reasons to use JNI?

Wikipedia says “JNI enables programmers to write native methods to handle situations when an application cannot be written entirely in the Java programming language, e.g. when the standard Java class library does not support the platform-specific features or program library” which means that in an Android application ...

What is JNA vs JNI?

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.

What is JNI Jobject?

jobject thiz means the this in java class. Sometimes if you create a static native method like this. void Java_MyClass_method1 (JNIEnv *, jclass); jclass means the class itself. Follow this answer to receive notifications.

What is JNIEnv * env?

JNIEnv – a structure containing methods that we can use our native code to access Java elements. JavaVM – a structure that lets us manipulate a running JVM (or even start a new one) adding threads to it, destroying it, etc…


2 Answers

You just need to cast jintto int using C style casts. Same for jboolean to bool (if you're using C99 bool type) or to uint8_t (if you're using std int types) or to unsigned char.

Open $NDK_ROOT/platforms/android-8/arch-arm/usr/include/jni.h and you'll see jint, jboolean etc are just typedefs.

like image 161
Gregory Pakosz Avatar answered Sep 28 '22 04:09

Gregory Pakosz


To cast a jboolean (which may only contain the values JNI_FALSE or JNI_TRUE) to a native bool I would use something like this :

(bool)(jboolean == JNI_TRUE) 

If perhaps the jboolean isn't coming from the JVM, then testing for jboolean != JNI_FALSE might be considered safer.

like image 22
Guillaume Bréhier Avatar answered Sep 28 '22 04:09

Guillaume Bréhier