0

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>

kevin4z
  • 349
  • 4
  • 12

2 Answers2

2

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.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • 1
    But if I remove all `jni::`, the error become `jni_version_1_6`, `GetEnv` or others filed was not declared in this scope. – kevin4z May 05 '16 at 12:30
  • I have upload this project to [github.com](https://github.com/kevin4z/DynamicJNI), can you help me modify that? I really don't know how to use `JNI`. Thanks :) – kevin4z May 05 '16 at 12:45
  • please see the update. I don't know where you took your template, but it is not correct. – Alex Cohn May 05 '16 at 17:47
0

Add header location to your android.mk

LOCAL_C_INCLUDES := "path to your header location"
Striker
  • 507
  • 4
  • 11
  • Is the `jni.h` file location? I add this line but it also has seemly build error.`LOCAL_C_INCLUDES := D:\Android\android-ndk-r10e\platforms\android-21\arch-arm\usr\include` – kevin4z May 05 '16 at 06:55
  • Try this: http://stackoverflow.com/questions/13466777/jni-h-no-such-file-or-directory – Striker May 05 '16 at 07:07