JNI笔记

来源:互联网 发布:美国wpi数据 编辑:程序博客网 时间:2024/05/17 21:46

1、JNI方法,如下所示

JNIEXPORT void JNICALL Java_ClassName_MethodName  (JNIEnv *env, jobject obj){    /*Implement Native Method Here*/}

JNIEnv就是jin.h中的JNINativeInterface结构体。

也就是说,*env->所能调用的所有方法,请到D:\Android\sdk\ndk-bundle\platforms\android-24\arch-arm\usr\include\jni.h中的JNINativeInterface结构体中自行查阅。

建议记住其中一些常用的函数。如findClass等。

如果想看官方详细文档,可以参考这个:

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html


2、上面代码中的jobject obj参数,obj代表执行此JNI操作的类实例引用.


小结:关于JNI匹配函数的两个参数的解释

The following is a JNI wrapper function which has two parameters, and returns a primitive array of objects:jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz );From the function name you have given I don't think it is complete, that is, you haven't respected the obligatory function name convention which is:Start the function with Java_Append the package name separated by _ (undescores) i.e. com_company_awesomeapp. So far the function name is composed of: Java_com_company_awesomeappAppend the Java class name where the native method has been defined, followed by the actual function name. So at this point we should have the following function name: Java_com_company_awesomeapp_MainActivity_TestThe first parameter is a pointer to a structure storing all JNI function pointers, i.e. all the predefined functions you have available after you #include <jni.h>.The second parameter is a reference to the Java object inside which this native method has been declared in. You can use it to call the other methods of the Java object from the current JNI function, i.e. Call Java instance methods from JNI code written in C or C++.If for example you have the following Java class inside the MainActivity.java file:public class MainActivity extends Activity{    static    {        try        {            System.loadLibrary("mynativelib");        }        catch (UnsatisfiedLinkError ule)        {            Log.e(TAG, "WARNING: Could not load native library: " + ule.getMessage());        }    }    public static native Object[] Test();}Then, the jobject thiz parameter of the JNI function would be a reference to an object of type MainActivity.


3、如果java代码声明的native方法名有下划线,那么在.h和.c文件中生成的对应方法名,将会把下划线(_)呈现为(_1),也就是多了一个1。

应该是便于区分.h和.c文件中本来就有的下划线。


4、Intellij Idea目前不支持NDK开发。只能用Android Studio。


5、一个JNI方法的小例子:执行java方法。(该例子用到jobject指针,省的有人妄评他没用!)

#include<jni.h>  #include<stdio.h>  #include<android/log.h>  JNIEXPORT void JNICALL Java_com_test_jniclass_AndroidJniClassDemo_executeMethod (JNIEnv *env, jobject obj)  {      jclass clazz = (*env)->GetObjectClass(env,obj); //通过类的对象        jmethodID mid = (*env)->GetMethodID(env,clazz,"show","()V");//查找java中的show方法的ID,最后的签名符号为void类型      if(mid == NULL)      {          __android_log_print(ANDROID_LOG_INFO,"HGY", "method show ID not found");          return; //如果方法ID没有找到      }        jmethodID intshowID = (*env)->GetMethodID(env,clazz,"intShow","()I");      if(intshowID == NULL)      {          __android_log_print(ANDROID_LOG_INFO,"HGY", "method intShow ID not found");          return; //如果方法ID没有找到      }        __android_log_print(ANDROID_LOG_INFO,"HGY", "will execute show function");      (*env)->CallVoidMethod(env,obj,mid); //执行show方法        __android_log_print(ANDROID_LOG_INFO,"HGY", "will execute intShow function");      (*env)->CallIntMethod(env,obj,intshowID); //执行show方法    }  

6、