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

  • 相关阅读:
    Python协程
    Python3常用标准库
    温故而知新--day3
    温故而知新--day2
    解决 WPF 绑定集合后数据变动界面却不更新的问题
    WPF 消息框 TextBox 绑定新数据时让光标和滚动条跳到最下面
    C# 枚举转列表
    真・WPF 按钮拖动和调整大小
    使用 GB28181.Solution + ZLMediaKit + MediaServerUI 进行摄像头推流和播放
    将 .NET Framework 项目转换为 .NET Standard 项目
  • 原文地址:https://www.cnblogs.com/qiengo/p/2612878.html
Copyright © 2011-2022 走看看