zoukankan      html  css  js  c++  java
  • NDK Note

     For the detail of JNI functions:

    http://www.public.iastate.edu/~java/docs/guide/nativemethod/functions.doc.html#17314


    1.The Definition about JNIEnv is different in C and C++.

    #if defined(__cplusplus)

    typedef _JNIEnv JNIEnv;
    typedef _JavaVM JavaVM;
    #else
    typedef const struct JNINativeInterface* JNIEnv;
    typedef const struct JNIInvokeInterface* JavaVM;
    #endif

      In C,JNI use(*env)-> to get the value of the method pointer.

        jsize len = (*env)->GetArrayLength(env,array);

    jintArray array = (*env)-> NewIntArray(env, 10);  
    for(; i<= 10; i++){  
        (*env)->SetIntArrayRegion(env, array, i-1, 1, &i);   
    } 

      In C++,JNIEvn has the inner member function which can deal with the pointer.

        jsize len =env->GetArrayLength(array);

    jintArray array= env-> NewIntArray(10);  
    for(; i<= 10; i++){  
        env->SetIntArrayRegion(array, i-1, 1, &i);   
    }

      So you may get the following error if you apply the C's usage to C++:

    $ make APP=hello-jni-C++
    Android NDK: Building for application 'hello-jni-C++'
    Compile++ thumb: hello-jni-C++ <= sources/samples/hello-jni-C++/hello-jni.cpp
    sources/samples/hello-jni-C++/hello-jni.cpp: In function '_jstring* Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv*, _jobject*)':
    sources/samples/hello-jni-C++/hello-jni.cpp:29: error: base operand of '->' has non-pointer type '_JNIEnv'
    make: *** [out/apps/hello-jni-C++/android-1.5-arm/objs/hello-jni-C++/hello-jni.o] Error 1

       JNI can only use C complier,so you have to add extern "C" when you use C++. or you'll get  java.lang.UnsatisfiedLinkError

        JNIEnv * env--> JNI interface pointer

        JObject thiz-->this pointer

    2.Data Type
    Java C/C++ byte
    boolean jboolean 1
    byte jbyte 1
    char jchar 2
    short jshort 2
    int jint 4
    long jlong 8
    float jfloat 4
    double jdouble 8
    Java C/C++
    boolean[ ] JbooleanArray
    byte[ ] JbyteArray
    char[ ] JcharArray
    short[ ] JshortArray
    int[ ] JintArray
    long[ ] JlongArray
    float[ ] JfloatArray
    double[ ] JdoubleArray

      You can use xxx * GetXxxArrayElements(xxxArray array, jboolean *isCopy)  to get a C pointer which point to a java array.And transfer the pointer to ReleaseXxxArrayElemes when needn’t it.

    3.Operations About Java

    jclass FindClass(JNIEnv *env, const char *name); 

      Pay attention to the second param’s type const char *,you may need to convert to const char* of utf8 with the help of GetStringUTFChars if you transfer the java’s unicode jstring to this function.Remember to use ReleaseStringUTFChars after get the class successfully.

      Java's character encoding is unicode,but we need utf8 in jni.so you should always use GetStringUTFChars when you get a jstring from java.Then ReleaseStringUTFChars.

    jclass GetSuperclass(JNIEnv *env, jclass clazz); 

      get parent or super class.

    jboolean IsAssignableFrom(JNIEnv *env, jclass clazz1,jclass clazz2); 

      Judge whether the class1’s object can be converted to class2’s object successfully.

    jclass GetObjectClass(JNIEnv *env, jobject obj);

      get the class by object.

    jboolean IsInstanceOf(JNIEnv *env, jobject obj,jclass clazz);

    jmethodID GetMethodID(JNIEnv *env, jclass clazz,const char *name, const char *sig);

      get the ID of a java method.

    NativeType CallXXXMethod (JNIEnv *env, jobject obj,jmethodID methodID, va_list args);

      CallObjectMethod,CallBooleanMethod,CallByteMethod,CallCharMethod,CallShortMethod

      CallIntMethod,CallLongMethod,CallFloatMethod,CallDoubleMethod,CallVoidMethod.......
      Here the java class is not static.

      va_list args:argument list

    Access the Field of Java

        1. jfieldID GetFieldID(JNIEnv *env, jclass clazz,const char *name, const char *sig)

        2. NativeType GetXXXField(JNIEnv *env, jobject obj,jfieldID fieldID);

      GetObjectField,GetBooleanField,GetByteField,GetCharField,GetShortField,GetIntField,GetLongField,GetFloatField,GetDoubleField。
        3. void SetXXXField(JNIEnv *env, jobject obj, jfieldID fieldID,NativeType value); 
      SetObjectField,SetBooleanField,SetByteField,SetCharField,SetShortField,SetIntField,SetLongField,SetFloatField,SetDoubleField

        4.jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz,const char *name, const char *sig);

        5. NativeType GetStaticXXXField(JNIEnv *env, jclass clazz,jfieldID fieldID);
        6. void SetStaticXXXField(JNIEnv *env, jclass clazz,jfieldID fieldID, NativeType value);

  • 相关阅读:
    js append()和appendChild()和insertBefore()的区别
    webpack打包工具简单案例
    Vue $ref 的用法
    Vue学习笔记-作用域插槽
    Vue学习笔记-插槽基本使用
    Vue学习笔记-父子通信案例
    Echarts案例-折线图
    Echarts案例-柱状图
    软件构造实验三-递归下降分析分析法
    软件构造实验二-拷贝一个c文件 将其中的关键字int替换成float
  • 原文地址:https://www.cnblogs.com/qiengo/p/2612878.html
Copyright © 2011-2022 走看看