JNI中在C中打印logcat控制台日志(android_log_print.c)
#ifndef_ANDROID_LOG_PRINT_H_
#define_ANDROID_LOG_PRINT_H_
#include
#defineIS_DEBUG
#ifdefIS_DEBUG
#defineLOG_TAG("CUSTOMER_NDK_JNI")
#defineLOGI(...)((void)__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__))
#defineLOGE(...)((void)__android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__))
#else
#defineLOGI(LOG_TAG,...)NULL
#defineLOGE(LOG_TAG,...)NULL
#endif
#endif
把java的string转化成c的字符串
//把java的string转化成c的字符串
char* Jstring2CStr(JNIEnv* env, jstring jstr)
{
char* rtn = NULL;
jclass clsstring = (*env)->FindClass(env,"java/lang/String"); //String
jstring strencode = (*env)->NewStringUTF(env,"GB2312"); //"gb2312"
jmethodID mid = (*env)->GetMethodID(env,clsstring, "getBytes", "(Ljava/lang/String;)[B"); //getBytes(Str);
jbyteArray barr= (jbyteArray)(*env)->CallObjectMethod(env,jstr,mid,strencode); // String .getByte("GB2312");
jsize alen = (*env)->GetArrayLength(env,barr);
jbyte* ba = (*env)->GetByteArrayElements(env,barr,JNI_FALSE);
if(alen > 0)
{
rtn = (char*)malloc(alen+1); //"\0"
memcpy(rtn,ba,alen);
rtn[alen]=0;
}
(*env)->ReleaseByteArrayElements(env,barr,ba,0); //释放内存空间
return rtn;
}
网友评论