From a726f36f75010e76654747ef26f909f502b0f5b0 Mon Sep 17 00:00:00 2001 From: Oxore Date: Wed, 9 Oct 2019 03:14:23 +0300 Subject: Initial commit --- jni/hello-jni.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 jni/hello-jni.c (limited to 'jni/hello-jni.c') 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 +#include +#include + +#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 -- cgit v1.2.3