gpt4 book ai didi

java - 伴随对象 Kotlin JNI 中的 UnsatisfiedLinkError

转载 作者:行者123 更新时间:2023-12-01 19:53:13 27 4
gpt4 key购买 nike

我正在我的一个移动应用程序中将代码从 Java 转换为 Kotlin,在 Java 中运行的代码在 Kotlin 中停止运行。它使用JNI桥来调用C++代码。

Kotlin 方法声明:

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方法(这个效果很好)

protected static native void convertNative(
int width, int height, int row, byte[] input, int ft, byte[] output);

在这两种情况下,库都使用 System.loadLibrary 正确加载(我看到正确的日志输出,没有错误)。

最佳答案

如果您想避免更改 C++ 代码,您应该使用 @JvmStatic 注释 Kotlin 函数,即:

@JvmStatic external fun convertNative(width: Int, height: Int, row: Int, input: ByteArray, ft: Int, output: ByteArray)

顺便说一句,您的 C++ 函数声明在技术上是不正确的:对于静态方法,第二个参数是 jclass,而不是 jobject

关于java - 伴随对象 Kotlin JNI 中的 UnsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59068495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com