Java类型 | 本地类型 | 说明 |
boolean | jboolean | 无符号,8位 |
byte | jbyte | 无符号,8位 |
char | jchar | 无符号,16位 |
short | jshort | 有符号,16位 |
int | jint | 有符号,32位 |
long | jlong | 有符号,64位 |
float | jfloat | 32位 |
double | jdouble | |
void | void | N/A |
从上表可以看jni的基本类型还是很容易记住的,所有数据类型都在头文件 jni.h中定义,需要详细了解可以查看这个头文件。
为熟练各个类型的使用,通过一个demo来加强记忆,demo很简单在android 定义各个类型的本地方法,传入相关类型的值,然后从本地方法中返回相应的数据类型值。
本地方法定义类 代码如下:
package com.aidesudi.testjni;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean b = passBooleanReturnBoolean(true);
byte b1 = passByteReturnByte((byte) 10);
char c = passCharReturnChar('#');
short s = passShortReturnShort((short) 100);
int i = passIntReturnInt(1000);
long l = passLongReturnLong(234234242L);
float f = passFloatReturnFloat(10.0f);
double d = passDoubleReturnDouble(11.11);
// 在Java中打印输出
Log.i("MainActivity","boolean: "+b);
Log.i("MainActivity","byte: "+b1);
Log.i("MainActivity","char: "+c);
Log.i("MainActivity","short: "+s);
Log.i("MainActivity","int: "+i);
Log.i("MainActivity","long: "+l);
Log.i("MainActivity","float: "+f);
Log.i("MainActivity","double: "+d);
}
// 定义本地方法
public native boolean passBooleanReturnBoolean(boolean b);
public native byte passByteReturnByte(byte b);
public native char passCharReturnChar(char c);
public native short passShortReturnShort(short s);
public native int passIntReturnInt(int i);
public native long passLongReturnLong(long l);
public native float passFloatReturnFloat(float f);
public native double passDoubleReturnDouble(double d);
static {
System.loadLibrary("native-lib");
}
}
#include
#include
#include "mylog.h"
#include "../java/com_aidesudi_testjni_MainActivity.h"
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passBooleanReturnBoolean
* Signature: (Z)Z
*/
JNIEXPORT jboolean JNICALL Java_com_aidesudi_testjni_MainActivity_passBooleanReturnBoolean
(JNIEnv *, jobject, jboolean b){
LOGI("%d in %d bytes", b, sizeof(b));
return !b;
}
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passByteReturnByte
* Signature: (B)B
*/
JNIEXPORT jbyte JNICALL Java_com_aidesudi_testjni_MainActivity_passByteReturnByte
(JNIEnv *, jobject, jbyte b){
LOGI("%d in %d bytes", b, sizeof(b));
return b+1;
}
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passCharReturnChar
* Signature: (C)C
*/
JNIEXPORT jchar JNICALL Java_com_aidesudi_testjni_MainActivity_passCharReturnChar
(JNIEnv *, jobject, jchar c){
LOGI("%d in %d bytes", c, sizeof(c));
c = '*';
return c;
}
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passShortReturnShort
* Signature: (S)S
*/
JNIEXPORT jshort JNICALL Java_com_aidesudi_testjni_MainActivity_passShortReturnShort
(JNIEnv *, jobject, jshort s){
LOGI("%d in %d bytes", s, sizeof(s));
return s - 1;
}
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passIntReturnInt
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_aidesudi_testjni_MainActivity_passIntReturnInt
(JNIEnv *, jobject, jint i){
LOGI("%d in %d bytes", i, sizeof(i));
return i + 1;
}
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passLongReturnLong
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_aidesudi_testjni_MainActivity_passLongReturnLong
(JNIEnv *, jobject, jlong l){
LOGI("%d in %d bytes", l, sizeof(l));
return l + 1;
}
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passFloatReturnFloat
* Signature: (F)F
*/
JNIEXPORT jfloat JNICALL Java_com_aidesudi_testjni_MainActivity_passFloatReturnFloat
(JNIEnv *, jobject, jfloat f){
LOGI("%d in %d bytes", f, sizeof(f));
return f + 1.5;
}
/*
* Class: com_aidesudi_testjni_MainActivity
* Method: passDoubleReturnDouble
* Signature: (D)D
*/
JNIEXPORT jdouble JNICALL Java_com_aidesudi_testjni_MainActivity_passDoubleReturnDouble
(JNIEnv *, jobject, jdouble d){
LOGI("%d in %d bytes", d, sizeof(d));
return d + 1.5;
}