1、MainActivity.kt
class MainActivity : AppCompatActivity() {
private val TAG = "MainActivity"
var strings = arrayOf("apple", "pear", "banana")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val jNIBaseicType = JNIBaseicType()
sample_text.setOnClickListener {
sample_text.text =
jNIBaseicType.callNativeInt(2).toString() + "\r\n" + jNIBaseicType.callNativeShort(2.toShort()).toString() + "\r\n" + jNIBaseicType.callNativeLong(
2.toLong()
).toString() + "\r\n" + jNIBaseicType.callNativeFloat(2.toFloat()).toString() + "\r\n" + jNIBaseicType.callNativeDouble(
2.toDouble()
).toString() + "\r\n" + jNIBaseicType.callNativeChar('A').toString() + "\r\n" + jNIBaseicType.callNativeByte(
2.toByte()
).toString() + "\r\n" + jNIBaseicType.callNativeBoolean(false)
}
}
}
2、JNIBaseicType.java
package com.example.demo.jni;
/**
* author : huangyi
* date : 2020/2/9
* desc :jni基础数据类型
*/
public class JNIBaseicType {
static {
System.loadLibrary("baseictype-lib");
}
public native int callNativeInt(int num);
public native int callNativeByte(byte b);
public native int callNativeChar(char ch);
public native int callNativeShort(short sh);
public native int callNativeLong(long l);
public native int callNativeFloat(float f);
public native int callNativeDouble(double d);
public native int callNativeBoolean(boolean b);
}
3、baseictype.cpp
//
// Created by huangyi on 2020/2/9.
//
#include <base.h>
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeInt(JNIEnv *env,
jobject thiz,
jint num) {
LOGD("java int value is %d:", num);
int c_num = num * 2;
return c_num;
}
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeByte(JNIEnv *env,
jobject thiz,
jbyte b) {
LOGD("java byte value is %d:", b);
jbyte c_byte = b + (jbyte) 10;
return c_byte;
}
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeChar(JNIEnv *env,
jobject thiz,
jchar ch) {
LOGD("java char value is %d:", ch);
jchar c_char = ch + (jchar) 3;
return c_char;
}
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeShort(JNIEnv *env,
jobject thiz,
jshort sh) {
LOGD("java short value is %d:", sh);
jshort c_short = sh + (jshort) 10;
return c_short;
}
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeLong(JNIEnv *env,
jobject thiz,
jlong l) {
LOGD("java long value is %d:", l);
jlong c_long = l + (jlong) 100;
return c_long;
}
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeFloat(JNIEnv *env,
jobject thiz,
jfloat f) {
LOGD("java float value is %d:", f);
jfloat c_float = f + (jfloat) 10.0;
return c_float;
}
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeDouble(JNIEnv *env,
jobject thiz,
jdouble d) {
LOGD("java double value is %d:", d);
jdouble c_double = d + 20.0;
return c_double;
}
extern "C" JNIEXPORT jint JNICALL Java_com_example_demo_jni_JNIBaseicType_callNativeBoolean(JNIEnv *env,
jobject thiz,
jboolean b) {
LOGD("java boolean value is %d:", b);
jboolean c_bool = (jboolean) !b;
return c_bool;
}