diff options
Diffstat (limited to 'jni/hello-jni.c')
-rw-r--r-- | jni/hello-jni.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/jni/hello-jni.c b/jni/hello-jni.c new file mode 100644 index 0000000..5b838d7 --- /dev/null +++ b/jni/hello-jni.c @@ -0,0 +1,73 @@ +#include <string.h> +#include <jni.h> +#include <android/log.h> + +#if 1 // Simple JNI Implementation +JNIEXPORT +jstring +JNICALL +Java_tk_oxore_NativeArmv6_DummyAct_helloJni(JNIEnv* env, jobject thiz) +{ + + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "Debug Log"); + return (*env)->NewStringUTF(env, "Hello Form JNI"); +} + +#else // Alternative JNI imlementation +jstring +helloJni(JNIEnv* env, jobject thiz) +{ + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "Debug Log"); + return (*env)->NewStringUTF(env, "Hello Form JNI"); +} + +jint +JNI_OnLoad(JavaVM* vm, void* reserved) +{ + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "Running JNI_OnLoad"); + + if (vm == NULL) { + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "vm == NULL"); + return JNI_ERR; + } + + JavaVMAttachArgs args; + args.version = JNI_VERSION_1_6; // choose your JNI version + args.name = NULL; // you might want to give the java thread a name + args.group = NULL; // you might want to assign the java thread to a ThreadGroup + JNIEnv* env; + if ((*vm)->AttachCurrentThread(vm, &env, &args) != JNI_OK) { + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "GetEnv != JNI_OK"); + return JNI_ERR; + } + + if (env == NULL) { + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "env == NULL"); + return JNI_ERR; + } + + if ((*env)->FindClass == NULL) { + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "No `FindClass` function provided"); + return JNI_ERR; + } + + // Find your class. JNI_OnLoad is called from the correct class loader context for this to work. + jclass c = (*env)->FindClass(env, "tk/oxore/NativeArmv6/DummyAct"); + if (c == NULL) { + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "c == NULL"); + return JNI_ERR; + } + + // Register your class' native methods. + static const JNINativeMethod methods[] = { + {"helloJni", "()Ljava/lang/String;", (void*)&helloJni}, + }; + int rc = (*env)->RegisterNatives(env, c, methods, sizeof(methods)/sizeof(*methods)); + if (rc != JNI_OK) { + __android_log_write(ANDROID_LOG_DEBUG, "HELLOJNI", "rc != JNI_OK"); + return rc; + } + + return JNI_VERSION_1_6; +} +#endif |