问题描述
我正在我的一个移动应用程序中将代码从Java转换为Kotlin,并且Java中可用的代码停止在Kotlin中工作.它使用JNI桥来调用C ++代码.
I'm converting code from Java to Kotlin in one of my mobile apps, and the code that worked in Java stopped working in Kotlin. It uses JNI bridge to call C++ code.
Kotlin方法声明:
Kotlin method declaration:
class Converter{
companion object {
external fun convertNative(width: Int, height: Int, row: Int, input: ByteArray, ft: Int, output: ByteArray)
}
}
.cc代码:
extern "C" {
JNIEXPORT void JNICALL OBJECT_TRACKER_METHOD(convertNative)(JNIEnv* env, jobject thiz, jint width, jint height, jint row,
jbyteArray input, jint ft, jbyteArray output);
}
JNIEXPORT void JNICALL OBJECT_TRACKER_METHOD(convertNative)(
JNIEnv* env, jobject thiz, jint width, jint height, jint row,
jbyteArray input, jint ft, jbyteArray output) {...}
我得到的错误:
java.lang.UnsatisfiedLinkError:No implementation found for void com.sampleapp.Converter$Companion.convertNative(int,int,int,byte[],int,byte[])(tried Java_com_sampleapp_Converter_00024Companion_convertNative and Java_com_sampleapp_Converter_00024Companion_convertNative__III_3BI_3B) at com.sampleapp.Converter$Companion.convertNative(Native Method)...
原始的JAVA方法(可以正常工作)
The original JAVA method (this works fine)
protected static native void convertNative(
int width, int height, int row, byte[] input, int ft, byte[] output);
在这两种情况下,都可以使用System.loadLibrary(我看到正确的日志输出,没有错误)正确加载该库.
Also the library is properly loaded using System.loadLibrary (I see correct log output, no errors) in both cases.
推荐答案
如果要避免更改C ++代码,应使用@JvmStatic
注释Kotlin函数,即:
If you want to avoid changing the C++ code you should annotate the Kotlin function with @JvmStatic
, i.e:
@JvmStatic external fun convertNative(width: Int, height: Int, row: Int, input: ByteArray, ft: Int, output: ByteArray)
顺便说一句,您的C ++函数声明在技术上是错误的:对于静态方法,第二个参数是jclass
,而不是jobject
.
By the way, your C++ function declaration is technically incorrect: For static methods, the second parameter is a jclass
, not a jobject
.
这篇关于伴随对象Kotlin JNI中的UnsatisfiedLinkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!