Android Buffet's JNI (v) Advanced

Source: Internet
Author: User

Android Buffet's JNI (v) Advanced

    • Android Buffet's JNI five-step
      • Ready to be perfected
        • function declaration
        • Method invocation
      • Using Androidlog
      • Modular
      • Type conversions
      • Operation Bitmap
      • Other

C2c++ (to be perfected) function declaration
    • Java class: package.name.ClassName.methodName() ; Java method:public native String methodName(String value)
    • C:JNIEXPORT jstring JNICALL Java_package_name_ClassName_methodName(JNIEnv *env,jobject obj,jstring value)
    • C++

      //声明
      extern "C"{
      jstring Java_package_name_ClassName_methodName(JNIEnv *env,jobject obj,jstring value);
      }
      //定义:
      jstring Java_package_name_ClassName_methodName(JNIEnv *env,jobject obj,jstring value){...}
Method invocation
    1. Getstringutfchars
      • C:char * str = (*env)->GetStringUTFChars(env,value,NULL);
      • C++:const char * str = env->GetStringUTFChars(value,NULL);
    2. Newstringutf
      • C:(*env)->NewStringUTF(env, str);
      • C++:env->NewStringUTF(str);
    3. sprintf
      • C: Only need to define the first parameter before usechar * str;
      • C + +: Define the first parameter and allocate space for it before you use itchar * str = (char*)malloc(sizeof("size you need"))
Using Androidlog
    1. Configuration build.gradle

      android {
      //...
      defaultConfig {
      //...
      ndk{
      //...
      ldLibs "log", "z", "m"
      }
      }
      }
    2. Include
      In a C + + box that uses log#include <android/log.h>
    3. Call log
      __android_log_write(ANDROID_LOG_ERROR, "tag","message");
Modular

When there are too many files under the Jni folder to manage, it is possible to create a new subfolder to manage the file sub-module, and then write the sub-file name on the reference. The example file jni/file.h is moved to the jni/file/file.h corresponding include "file.h" change include "file/file.h" .

Type conversions
    1. String in Java
      Use Jstring to receive, with char* operation, Jstring and char* conversion in C + +, see above (c2c++)
    2. int in Java
      Received in C + + with int.
Operation Bitmap
    1. Java code

      public native void fun(Bitmap bmp);
    2. C + + header file
      #include <android/bitmap.h>
    3. C + + code

      void Java_package_name_ClassName_fun(JNIEnv *env,jobject obj,jobject bmp){
      AndroidBitmapInfo bmpInfo={0};
      if(AndroidBitmap_getInfo(env,bmp,&bmpInfo)<0){return;}
      unsigned char * dataFromBmp=NULL;//此指针即指向bitmap数据的指针
      if(AndroidBitmap_lockPixels(env,bmp,(void **)&dataFromBmp)){return;}//获取并锁定bitmap在内存的地址
      //do something with dataFromBmp
      AndroidBitmap_unlockPixels(env,bmp);//操作结束后需要解锁改地址
      }
    4. Configure Build.gradle

      android {
      //...
      defaultConfig {
      //...
      ndk{
      //...
      ldLibs "jnigraphics"
      }
      }
      }
    5. Configure ANDROID.MK
      LOCAL_LDLIBS := -ljnigraphics
    6. Clean Project
Other
  1. Using the standard library STL
    • Create a new file and write it under JNI Application.mkAPP_STL := stlport_static
    • Configuring in Build.gradle

      android {
      //...
      defaultConfig {
      //...
      ndk{
      //...
      stl "stlport_static"
      }
      }
      }
  2. Turn on exception control (can throw in CPP)

    android {  //...  defaultConfig {      //...      ndk{         //...         "-fexceptions"      }  }}
  3. error:undefined reference to XXX
    Generally this error will be reported in the CPP, find the corresponding H file, make the following changes

     #ifndef _a_h   #define _A_H   #ifdef __cplusplus  extern " C " {# endif   (The original code of the file)  #ifdef __cplusplus }  #endif   #endif//endof _a_h   
  4. const char *Turnunsignde char *

    //const cahr * str=...;unsignedchar * data=(unsignedchar *)(const_cast<char*>(str));

Android Buffet's JNI (v) Advanced

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.