看起来,当涉及内部类类型的参数时,JDK 中存在错误(或至少不一致) 。
这是一个重现问题的示例类:
public class A {
public native void a(android.graphics.Bitmap.Config b);
public native void a(android.graphics.Bitmap.Config b, int c);
static {
System.loadLibrary("hello-libs");
a(null);
}
}
如果您使用javah
生成本机标头,您将获得
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_hellolibs_MainActivity */
#ifndef _Included_com_example_hellolibs_MainActivity
#define _Included_com_example_hellolibs_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: A
* Method: a
* Signature: (Landroid/graphics/Bitmap/Config;)V
*/
JNIEXPORT void JNICALL A_a__Landroid_graphics_Bitmap_Config_2
(JNIEnv *, jobject, jobject);
/*
* Class: A
* Method: a
* Signature: (Landroid/graphics/Bitmap/Config;I)V
*/
JNIEXPORT void JNICALL Java_A_a__Landroid_graphics_Bitmap_Config_2I
(JNIEnv *, jobject, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
和 -
java.lang.UnsatisfiedLinkError: No implementation found for void Aa(android.graphics.Bitmap$Config) (试过 Java_A_a 和 Java_A_a__Landroid_graphics_Bitmap_00024Config_2)
但是这个错误很少影响javah
or生成的头文件javac -h dir
,因为通常本地方法是用“短”名称生成的,例如Java_A_a
它不关心参数的类型。
解决方案是按照https://bugs.openjdk.java.net/browse/JDK-8145897中的建议手动更改方法签名。