标签:
package.name.ClassName.methodName()
;Java方法:public native String methodName(String value)
JNIEXPORT jstring JNICALL Java_package_name_ClassName_methodName(JNIEnv *env,jobject obj,jstring value)
//声明
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){...}
char * str = (*env)->GetStringUTFChars(env,value,NULL);
const char * str = env->GetStringUTFChars(value,NULL);
(*env)->NewStringUTF(env, str);
env->NewStringUTF(str);
char * str;
char * str = (char*)malloc(sizeof("size you need"))
build.gradle
android {
//...
defaultConfig {
//...
ndk{
//...
ldLibs "log", "z", "m"
}
}
}
#include <android/log.h>
__android_log_write(ANDROID_LOG_ERROR, "tag","message");
当jni文件夹下文件过多不好管理时,可以再jni下新建子文件夹来将文件分模块管理,引用时写上子文件名即可。例文件jni/file.h
移到jni/file/file.h
,对应的include "file.h"
改为include "file/file.h"
。
public native void fun(Bitmap bmp);
#include <android/bitmap.h>
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);//操作结束后需要解锁改地址
}
android {
//...
defaultConfig {
//...
ndk{
//...
ldLibs "jnigraphics"
}
}
}
LOCAL_LDLIBS := -ljnigraphics
Application.mk
并写入APP_STL := stlport_static
android {
//...
defaultConfig {
//...
ndk{
//...
stl "stlport_static"
}
}
}
开启异常控制(可以在cpp中throw)
android {
//...
defaultConfig {
//...
ndk{
//...
cFlags = "-fexceptions"
}
}
}
error: undefined reference to xxx
一般该错误会在cpp中报出,找到对应的h文件,做如下修改
#ifndef _A_H
#define _A_H
#ifdef __cplusplus
extern "C"{
#endif
...(文件原有代码)
#ifdef __cplusplus
}
#endif
#endif // endof _A_H
const char *
转unsignde char *
//const cahr * str=...;
unsigned char * data=(unsigned char *)(const_cast<char*>(str));
标签:
原文地址:http://blog.csdn.net/xmh19936688/article/details/51482679