1、使用ndk-build时如果找不到某个类,可以使用下面两种方法解决:

1、1 进入src目录

D:\project3\JNIAndroid\src>set classpath=D:\project3\JNIAndroid\src

1、2 设置classpath

classpath    .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\tools.jar;E:\developLib\android\platforms\android-8\android.jar

2、基础类

ublic class NativeModule {

    public native int testArg(int i, boolean b, char c, double d);

    public native byte[] testByte(byte[] b);

    public native String[] testString(String s, String[] sarr);

    public native int setInfo(MyInfo info);

    public native MyInfo getInfo();

    static {

       System.loadLibrary("NativeModule");

    }

}
//其中MyInfo类定义如下:

public class Record {

    int id;

    String name;

    byte[] data;

}

public class MyInfo {

    public boolean b;

    public char c;

    public double d;

    public int i;

    public byte[] array;

    public String s;

    public Record rec;

}

c 结构体定义

typedef struct{

    int id;

    char name[255];

    char data[255];

}Record;

typedef struct{

    BOOL b;

    char c;

    double d;

    int i;

    char arr[255];

    char sz[255];

    Record rec;

}MyInfo;

3、编写C库

3.1) Java与C不同类型参数转换实例

//不同类型参数处理

JNIEXPORT jintJNICALL Java_com_jinhill_util_NativeModule_testArg

  (JNIEnv *env, jobject jo, jint ji, jbooleanjb,  jchar jc, jdouble jd){
//获取jint型值
int i = ji;
//获取jboolean型值
BOOL b = jb;
//获取jdouble型值
double d = jd;
//获取jchar型值,Java的char两字节
char ch[5] = {0};
int size = 0;
size = WideCharToMultiByte(CP_ACP,NULL, (LPCWSTR)&jc, -1, ch, 5, NULL, FALSE);
if(size <= 0) {
return -1;
}
Trace("ji=%d,jb=%d,jc=%s,jd=%lf",i, b, ch, d);
return 0; }

3.2) Java byte与C char数组类型数组转换实例

//btye数组处理,形参作为输入或输出,返回btye数组

JNIEXPORTjbyteArray JNICALL Java_com_jinhill_util_NativeModule_testByte

  (JNIEnv *env, jobject jo, jbyteArray jbArr){
char chTmp[] = "Hello JNI!";
int nTmpLen = strlen(chTmp);
//获取jbyteArray
char *chArr = (char*)env->GetByteArrayElements(jbArr,0);
//获取jbyteArray长度
int nArrLen = env->GetArrayLength(jbArr);
char *szStrBuf =(char*)malloc(nArrLen*2+10);
memset(szStrBuf, 0, nArrLen*2+10);
Bytes2String(chArr, nArrLen, szStrBuf,nArrLen*2+10);
Trace("jbArr=%s", szStrBuf);
//将jbArr作为输出形参
memset(chArr, 0, nArrLen);
memcpy(chArr, chTmp, nTmpLen);
//返回jbyteArray
jbyteArray jarrRV =env->NewByteArray(nTmpLen);
jbyte *jby =env->GetByteArrayElements(jarrRV, 0);
memcpy(jby, chTmp, strlen(chTmp));
env->SetByteArrayRegion(jarrRV, 0,nTmpLen, jby);
return jarrRV;
}

3.3)   Java String与C char数组类型转换实例

//String 和String[]处理

JNIEXPORTjobjectArray JNICALL Java_com_jinhill_util_NativeModule_testString

  (JNIEnv *env, jobject jo, jstring jstr,jobjectArray joarr){
int i = 0; char chTmp[50] = {0}; //获取jstring值 const char* pszStr = (char*)env->GetStringUTFChars(jstr, 0); Trace("jstr=%s", pszStr); //获取jobjectArray值 int nArrLen =env->GetArrayLength(joarr); Trace("joarr len=%d",nArrLen); for(i=0; i<nArrLen; i++) {
jstring js =(jstring)env->GetObjectArrayElement(joarr, i);
const char* psz = (char*)env->GetStringUTFChars(js, 0);
Trace("joarr[%d]=%s",i, psz);
} //将joarr作为输出形参 jstring jstrTmp = NULL; for(i=0; i<nArrLen; i++) {
sprintf(chTmp, "No.%dHello JNI!", i);
jstrTmp =env->NewStringUTF(chTmp); env->SetObjectArrayElement(joarr,i, jstrTmp);
env->DeleteLocalRef(jstrTmp);
} //返回jobjectArray jclass jstrCls =env->FindClass("Ljava/lang/String;"); jobjectArray jstrArray =env->NewObjectArray(2, jstrCls, NULL); for(i=0; i<2; i++) {
sprintf(chTmp, "No. %dReturn JNI!", i);
jstrTmp =env->NewStringUTF(chTmp);
env->SetObjectArrayElement(jstrArray,i, jstrTmp);
env->DeleteLocalRef(jstrTmp);
} return jstrArray; }

3.4)   Java 类与C结构体类型转换实例

JNIEXPORT jint JNICALL Java_com_jinhill_util_NativeModule_setInfo

  (JNIEnv *env, jobject jo, jobject jobj){

    char chHexTmp[512] = {0};

    //将Java类转换成C结构体

    MyInfo mi;

    //获取Java中的实例类Record

    jclass jcRec = env->FindClass("com/jinhill/util/Record");

    //int id

    jfieldID jfid = env->GetFieldID(jcRec, "id", "I");

    //String name

    jfieldID jfname = env->GetFieldID(jcRec, "name", "Ljava/lang/String;");

    //byte[] data;

    jfieldID jfdata = env->GetFieldID(jcRec, "data", "[B");

    //获取Java中的实例类MyInfo
jclass jcInfo = env->FindClass("com/jinhill/util/MyInfo"); //获取类中每一个变量的定义
//boolean b
jfieldID jfb = env->GetFieldID(jcInfo, "b", "Z"); //char c
jfieldID jfc = env->GetFieldID(jcInfo, "c", "C"); //double d
jfieldID jfd = env->GetFieldID(jcInfo, "d", "D"); //int i
jfieldID jfi = env->GetFieldID(jcInfo, "i", "I"); //byte[] array
jfieldID jfa = env->GetFieldID(jcInfo, "array", "[B"); //String s
jfieldID jfs = env->GetFieldID(jcInfo, "s", "Ljava/lang/String;"); //Record rec;
jfieldID jfrec = env->GetFieldID(jcInfo, "rec", "Lcom/jinhill/util/Record;"); //获取实例的变量b的值
mi.b = env->GetBooleanField(jobj, jfb); //获取实例的变量c的值
jchar jc = env->GetCharField(jobj, jfc); char ch[5] = {0}; int size = 0; size = WideCharToMultiByte(CP_ACP, NULL, (LPCWSTR)&jc, -1, ch, 5, NULL, FALSE); mi.c = ch[0]; //获取实例的变量d的值
mi.d = env->GetDoubleField(jobj, jfd); //获取实例的变量i的值
mi.i = env->GetIntField(jobj, jfi); //获取实例的变量array的值
jbyteArray ja = (jbyteArray)env->GetObjectField(jobj, jfa); int nArrLen = env->GetArrayLength(ja); char *chArr = (char*)env->GetByteArrayElements(ja, 0); memcpy(mi.arr, chArr, nArrLen); //获取实例的变量s的值
jstring jstr = (jstring)env->GetObjectField(jobj, jfs); const char* pszStr = (char*)env->GetStringUTFChars(jstr, 0); strcpy(mi.sz, pszStr); //获取Record对象 jobject joRec = env->GetObjectField(jobj, jfrec); //获取Record对象id值
mi.rec.id = env->GetIntField(joRec, jfid); Trace("mi.rec.id=%d",mi.rec.id); //获取Record对象name值
jstring jstrn = (jstring)env->GetObjectField(joRec, jfname); pszStr = (char*)env->GetStringUTFChars(jstrn, 0); strcpy(mi.rec.name, pszStr); //获取Record对象data值
jbyteArray jbd = (jbyteArray)env->GetObjectField(joRec, jfdata);
nArrLen = env->GetArrayLength(jbd);
chArr = (char*)env->GetByteArrayElements(jbd, 0);
memcpy(mi.rec.data, chArr, nArrLen); //日志输出 Bytes2String(mi.arr, nArrLen, chHexTmp, sizeof(chHexTmp));
Trace("mi.arr=%s, mi.b=%d, mi.c=%c, mi.d=%lf, mi.i=%d, \n mi.sz=%s\n mi.rec.id=%d, mi.rec.name=%s", chHexTmp, mi.b, mi.c, mi.d, mi.i, mi.sz, mi.rec.id, mi.rec.name);
return 0; }

3.5)   C结构体类型与Java 类转换实例

JNIEXPORT jobject JNICALL Java_com_jinhill_util_NativeModule_getInfo

  (JNIEnv *env, jobject jo){
wchar_t wStr[255] = {0};
char chTmp[] = "Hello JNI";
int nTmpLen = strlen(chTmp);
//将C结构体转换成Java类
MyInfo mi;
memcpy(mi.arr, chTmp, strlen(chTmp));
mi.b = TRUE;
mi.c = 'B';
mi.d = 2000.9;
mi.i = 8; strcpy(mi.sz, "Hello World!");
mi.rec.id = 2011;
memcpy(mi.rec.data, "\x01\x02\x03\x04\x05\x06", 6);
strcpy(mi.rec.name, "My JNI"); //获取Java中的实例类Record
jclass jcRec = env->FindClass("com/jinhill/util/Record"); //int id
jfieldID jfid = env->GetFieldID(jcRec, "id", "I"); //String name
jfieldID jfname = env->GetFieldID(jcRec, "name", "Ljava/lang/String;"); //byte[] data;
jfieldID jfdata = env->GetFieldID(jcRec, "data", "[B"); //获取Java中的实例类
jclass jcInfo = env->FindClass("com/jinhill/util/MyInfo"); //获取类中每一个变量的定义
//boolean b
jfieldID jfb = env->GetFieldID(jcInfo, "b", "Z"); //char c
jfieldID jfc = env->GetFieldID(jcInfo, "c", "C"); //double d
jfieldID jfd = env->GetFieldID(jcInfo, "d", "D"); //int i
jfieldID jfi = env->GetFieldID(jcInfo, "i", "I"); //byte[] array
jfieldID jfa = env->GetFieldID(jcInfo, "array", "[B"); //String s
jfieldID jfs = env->GetFieldID(jcInfo, "s", "Ljava/lang/String;"); //Record rec;
jfieldID jfrec = env->GetFieldID(jcInfo, "rec", "Lcom/jinhill/util/Record;"); //创建新的对象
jobject joRec = env->AllocObject(jcRec); env->SetIntField(joRec, jfid, mi.rec.id); jstring jstrn = env->NewStringUTF(mi.rec.name); env->SetObjectField(joRec, jfname, jstrn); jbyteArray jbarr = env->NewByteArray(6); jbyte *jb = env->GetByteArrayElements(jbarr, 0); memcpy(jb, mi.rec.data, 6); env->SetByteArrayRegion(jbarr, 0, 6, jb); env->SetObjectField(joRec, jfdata, jbarr); //创建新的对象
jobject joInfo = env->AllocObject(jcInfo); //给类成员赋值
env->SetBooleanField(joInfo, jfb, mi.b); // MultiByteToWideChar (CP_ACP, 0, mi.c, -1, wStr, 255);
// env->SetCharField(joInfo, jfc, (jchar)wStr); env->SetCharField(joInfo, jfc, (jchar)mi.c); env->SetDoubleField(joInfo, jfd, mi.d); env->SetIntField(joInfo, jfi, mi.i); jbyteArray jarr = env->NewByteArray(nTmpLen); jbyte *jby = env->GetByteArrayElements(jarr, 0);
memcpy(jby, mi.arr, nTmpLen); env->SetByteArrayRegion(jarr, 0, nTmpLen, jby);
env->SetObjectField(joInfo, jfa, jarr);
jstring jstrTmp = env->NewStringUTF(chTmp);
env->SetObjectField(joInfo, jfs, jstrTmp); env->SetObjectField(joInfo, jfrec, joRec);
return joInfo; }

3.6)   Java String与 C char数组转换时的中文问题

//将jstring类型转换成windows类型
char* jstringToWindows( JNIEnv *env, jstring jstr )
{
<span style="white-space:pre"> </span>int length = (env)->GetStringLength(jstr );
<span style="white-space:pre"> </span>const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
<span style="white-space:pre"> </span>char* rtn = (char*)malloc( length*2+1 );
<span style="white-space:pre"> </span>int size = 0;
<span style="white-space:pre"> </span>size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length,rtn,(length*2+1), NULL, NULL );
<span style="white-space:pre"> </span>if( size <= 0 )
<span style="white-space:pre"> </span>return NULL;
<span style="white-space:pre"> </span>(env)->ReleaseStringChars(jstr, jcstr );
<span style="white-space:pre"> </span>rtn[size] = 0;
<span style="white-space:pre"> </span>return rtn;
} //将windows类型转换成jstring类型
jstring WindowsTojstring( JNIEnv* env, char* str )
{
<span style="white-space:pre"> </span>jstring rtn = 0;
<span style="white-space:pre"> </span>int slen = strlen(str);
<span style="white-space:pre"> </span>unsigned short * buffer = 0;
<span style="white-space:pre"> </span>if( slen == 0 )
<span style="white-space:pre"> </span>rtn = (env)->NewStringUTF(str );
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
<span style="white-space:pre"> </span>buffer = (unsigned short *)malloc( length*2 + 1 );
<span style="white-space:pre"> </span>if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length )>0 )
<span style="white-space:pre"> </span>rtn = (env)->NewString( (jchar*)buffer, length );
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if( buffer )
<span style="white-space:pre"> </span>free( buffer );
<span style="white-space:pre"> </span>return rtn;
}

JNI由浅入深_10_JNI 综合开发的更多相关文章

  1. JNI由浅入深_9_JNI 异常处理

    1 .本地代码中如何缓存和抛出异常 下面的代码中演示了如何声明一个会抛出异常的本地方法.CatchThrow这个类声明了一个会抛出IllegalArgumentException异常的名叫doit的本 ...

  2. JNI由浅入深_8_JNI缓存字段和方法ID

    获取字段ID和方法ID时,需要用字段.方法的名字和描述符进行一个检索.检索过程相对比较费时,因此本节讨论用缓存技术来减少这个过程带来的消耗.缓存字段ID和方法ID的方法主要有两种.两种区别主要在于缓存 ...

  3. JNI由浅入深_7_c调用Java方法一

    1.在Java中声明方法 <span style="font-size:14px;">/** * javah -encoding utf-8 -jni com.exam ...

  4. JNI由浅入深_6_简单对象的应用

    1.声明native方法 public class ComplexObject { /** * 返回一个对象数组 * @param val * @return */ public native Per ...

  5. JNI由浅入深_5_基本类型应用

    1.基本类型应用 对于JNI处理基本类型还是比较简单的,下面是Java代码: <span style="font-size:14px;"> public native ...

  6. JNI由浅入深_4_JNI基础知识详解

    Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) 内部运行的 ...

  7. JNI由浅入深_3_Hello World

    1.需要准备的工具,eclipse,cdt(c++)插件,cygwin(unix)和 android ndk. 在cygwin的etc目录下将ndk的路径引入到profile文件中,可以在cygwin ...

  8. JNI由浅入深_2_C语言基础

    *含义 1.乘法 3*5 2.定义指针变量 int * p://定义了一个名字叫p的变量,能够存放int数据类型的地址 3.指针运算符, //如果p是一个已经定义好的指针变量则*p表示以p的内容为地址 ...

  9. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测

    目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...

随机推荐

  1. 关于容器、虚拟机以及 Docker 的一个入门教程

    Yves yao · 2017-09-05翻译 · 1315阅读 原文链接 huangxiaolu审校   源地址:http://zcfy.cc/article/a-beginner-friendly ...

  2. AngularJS - Directive Restrictions

    While it’s cool to make a custom element like we did the the previous cast, it’s actually more commo ...

  3. 洛谷P2792 [JSOI2008]小店购物(最小树形图)

    题意 题目链接 Sol 一开始的思路:新建一个虚点向每个点连边,再加上题面中给出的边,边权均为大小*需要购买的数量 然后发现死活都过不去 看了题解才发现题目中有个细节--买了\(A\)就可以买\(B\ ...

  4. PC端-上传头像并裁剪

    界面一: <link href="../theme/js/layui.layim/src/css/layui.css" rel="stylesheet"/ ...

  5. Android学习笔记(3)----手机调试[OFFLINE]的解决方式

    问题描述 今天用Android Studio开发了一个简单的调用摄像头的App,结果想调试的时候发现选择调试设备的对话框中,手机名称后面总是跟着一个[OFFLINE]的标识,只能选择启动AVD来进行调 ...

  6. 解决nexus3报Cannot open local storage 'component' with mode=rw的异常问题

    起因:正常使用的公司Maven.Docker私有仓库,突然镜像不能推送,返回getsockopt: connection refused,用docker客户端登录也是返回同样的错误,查看nexus3日 ...

  7. NuGet 2.0 (.NET软件包管理器) 发布了-现在升级吧

    原文:https://blogs.msdn.microsoft.com/scott_hanselman/2012/07/10/nuget-2-0-net/ [原文发表地址]  NuGet 2.0 (. ...

  8. 工具Sequel Pro简介

    从图中可以看到,sequel工作界面主要分成三部分,左边侧边显示的是当前所连接的数据库中存在的数据表,右侧上半部分则是用于写sql的地方,在Mac环境下按command+R键会执行你所写的sql,右侧 ...

  9. spring cloud zuul 配置

    参考:http://www.ityouknow.com/springcloud/2017/06/01/gateway-service-zuul.html spring boot版本:2.0.3.REL ...

  10. PyCharm2018 安装及破解方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012278016/article/details/81738676 目录 1>. 安装 2& ...