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
- Getstringutfchars
- C:
char * str = (*env)->GetStringUTFChars(env,value,NULL);
- C++:
const char * str = env->GetStringUTFChars(value,NULL);
- Newstringutf
- C:
(*env)->NewStringUTF(env, str);
- C++:
env->NewStringUTF(str);
- sprintf
- C: Only need to define the first parameter before use
char * str;
- C + +: Define the first parameter and allocate space for it before you use it
char * str = (char*)malloc(sizeof("size you need"))
Using Androidlog
- Configuration
build.gradle
android {
//...
defaultConfig {
//...
ndk{
//...
ldLibs "log", "z", "m"
}
}
}
- Include
In a C + + box that uses log#include <android/log.h>
- 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
- String in Java
Use Jstring to receive, with char* operation, Jstring and char* conversion in C + +, see above (c2c++)
- int in Java
Received in C + + with int.
Operation Bitmap
- Java code
public native void fun(Bitmap bmp);
- C + + header file
#include <android/bitmap.h>
- 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);//操作结束后需要解锁改地址
}
- Configure Build.gradle
android {
//...
defaultConfig {
//...
ndk{
//...
ldLibs "jnigraphics"
}
}
}
- Configure ANDROID.MK
LOCAL_LDLIBS := -ljnigraphics
- Clean Project
Other
- 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"
}
}
}
-
Turn on exception control (can throw in CPP)
android { //... defaultConfig { //... ndk{ //... "-fexceptions" } }}
-
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
-
const char *Turnunsignde char *
//const cahr * str=...;unsignedchar * data=(unsignedchar *)(const_cast<char*>(str));
Android Buffet's JNI (v) Advanced