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);

  • 相关阅读:
    完美解决HTML5无法上传大文件方法
    完美解决jquery无法上传大文件方法
    Word图片上传控件+整合富文本
    ckeditor粘贴word文档图片的讨论
    python之理解super及MRO列表 ( 示例版 )
    JavaXXX成长直通车_汇总
    P75 验证码识别原理分析
    P74 复习
    P73 卷积神经网络识别手写数字
    P71 激活层与池化层
  • 原文地址:https://www.cnblogs.com/qiengo/p/2612878.html
Copyright © 2011-2022 走看看