Created
August 1, 2011 22:00
-
-
Save poutyface/1119105 to your computer and use it in GitHub Desktop.
Android RegisterNatives
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <android/log.h> | |
#include "calvals.h" | |
#define EXPORT __attribute__((visibility("default"))) | |
#define LOG_TAG "native" | |
#define log_info(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) | |
#define log_error(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) | |
#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) | |
jint addVals(JNIEnv *env, jobject thiz, jint a, jint b) | |
{ | |
return a + b; | |
} | |
jint subVals(JNIEnv *env, jobject thiz, jint a, jint b) | |
{ | |
return a - b; | |
} | |
jint mulVals(JNIEnv *env, jobject thiz, jint a, jint b) | |
{ | |
return a * b; | |
} | |
jint divVals(JNIEnv *env, jobject thiz, jint a, jint b) | |
{ | |
return a / b; | |
} | |
int jniRegisterNativeMethods(JNIEnv *env, const char *className, | |
const JNINativeMethod *gMethods, int numMethods) | |
{ | |
jclass klass; | |
log_info("Registering %s natives\n", className); | |
klass = (*env)->FindClass(env, className); | |
if(klass == NULL){ | |
log_error("Native registration unable to find class %s\n", className); | |
return -1; | |
} | |
if((*env)->RegisterNatives(env, klass, gMethods, numMethods) < 0){ | |
log_error("RegisterNatives failed ofr %s\n", className); | |
return -1; | |
} | |
return 0; | |
} | |
static JNINativeMethod sMethods[] = { | |
{"addVals", "(II)I", (void*)addVals}, | |
{"subVals", "(II)I", (void*)subVals}, | |
{"mulVals", "(II)I", (void*)mulVals}, | |
{"divVals", "(II)I", (void*)divVals} | |
}; | |
EXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) | |
{ | |
JNIEnv *env = NULL; | |
if((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) | |
return -1; | |
jniRegisterNativeMethods(env, "dev/android/sample/AndroidNDKSampleActivity", sMethods, NELEM(sMethods)); | |
return JNI_VERSION_1_6; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment