I want to use dynamic registration in native method, so I need set JNI_onLoad
function. I just write a function to get sum of two numbers. But, it can't build correctly. How can I correct the error?
This is my *.cpp file, I name this file
jni.cpp
#include <jni.h> extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6); jni::jclass& nativeClass = jni::FindClass(env, "com/test/NativeClass"); #define MAKE_NATIVE_METHOD(name, sig) jni::MakeNativeMethod<decltype(name), name>( #name, sig ) jni::RegisterNatives(env, nativeClass, MAKE_NATIVE_METHOD(nativeAddTest, "(II)I") ); return JNI_VERSION_1_6; } jlong nativeAddTest(JNIEnv *env, jni::jobject* obj, jni::jint a, jni::jint b) { return a+b; }
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := test LOCAL_SRC_FILES := jni.cpp LOCAL_LDLIBS := -L/ndk-path/sources/cxx-stl/stlport/libs/armeabi include $(BUILD_SHARED_LIBRARY)
When I use
ndk-build
command, it's wrong. But I really dont't konw the reason...D:\WorkSpaces\Test\app\src\main\jni>ndk-build [x86] Compile++ : test <= jni.cpp D:/WorkSpaces/Test/app/src/main/jni/jni.cpp: In function 'jint JNI_OnLoad(JavaVM*, void*)': D:/WorkSpaces/Test/app/src/main/jni/jni.cpp:9:5: error: 'jni' has not been declared jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6); ^ D:/WorkSpaces/Test/app/src/main/jni/jni.cpp:9:18: error: 'env' was not declared in this scope jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6); ....
It seems can't find jni.h, but I already have #include<jni.h>