简体   繁体   中英

Android JNI ndk-build error: 'jni' has not been declared

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>

In Android NDK, <jni.h> does not define a jni namespace. Simply remove all jni::

#include <jni.h>
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {

   JNIEnv env;
   vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
   jclass nativeClass = env->FindClass("com/test/NativeClass");

… and so on.

将标头位置添加到您的android.mk

LOCAL_C_INCLUDES := "path to your header location"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM