java数组和jni数组对应关系
kotlin | jni |
---|---|
IntArray | jintArray |
DoubleArray | jdoubleArray |
LongArray | jlongArray |
CharArray | jcharArray |
ShortArray | jshortArray |
Array | jobjectArray |
//kotlin
external fun arrayInt(array: IntArray): String
external fun arrayDouble(array: DoubleArray): String
external fun arrayLong(array: LongArray): String
external fun arrayChar(array: CharArray): String
external fun arrayShort(array: ShortArray): String
external fun arrayString(array: Array<String>): String
//jni
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_arrayInt(JNIEnv *env, jobject thiz, jintArray array) {
// TODO: implement arrayInt()
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_arrayDouble(JNIEnv *env, jobject thiz,
jdoubleArray array) {
// TODO: implement arrayDouble()
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_arrayLong(JNIEnv *env, jobject thiz, jlongArray array) {
// TODO: implement arrayLong()
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_arrayChar(JNIEnv *env, jobject thiz, jcharArray array) {
// TODO: implement arrayChar()
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_arrayShort(JNIEnv *env, jobject thiz,
jshortArray array) {
// TODO: implement arrayShort()
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_arrayString(JNIEnv *env, jobject thiz,
jobjectArray array) {
// TODO: implement arrayString()
}
jobjectArray 实战
获取数组长度
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val array = arrayOf("first", "two")
binding.sampleText.text = "长度为:" + arrayLength(array)
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
external fun arrayLength(array: Array<String>): Int
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
}
native-lib.cpp
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_myapplication_MainActivity_arrayLength(JNIEnv *env, jobject thiz,
jobjectArray array) {
//获取数组长度
int length = env->GetArrayLength(array);
return length;
}
获取数组第一个元素
lass MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val array = arrayOf("first", "two")
binding.sampleText.text = "第一个元素:" + arrayFirst(array)
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
external fun arrayFirst(array: Array<String>): String
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
}
native-lib.cpp
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_arrayFirst(JNIEnv *env, jobject thiz,
jobjectArray array) {
//获取array第一个元素
jobject first = env->GetObjectArrayElement(array, 0);
//把jobject强转成jstring
return static_cast<jstring>(first);
}
jintArray 实战
获取第一个元素
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val array = intArrayOf(1, 2, 3)
binding.sampleText.text = "第一个元素:" + arrayInt(array)
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
external fun arrayInt(array: IntArray): Int
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
}
native-lib.cpp
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_myapplication_MainActivity_arrayInt(JNIEnv *env, jobject thiz, jintArray array) {
jint *result = env->GetIntArrayElements(array, JNI_FALSE);
//获取第一个元素
return result[0];
}