我制作了一个Android库项目,它包装了C ++本机代码并将其公开为Java API。在本机代码中,我通过标准JNI调用调用一些自定义Java类方法。这是一个简化的例子:
public class CustomClass {
private int myInt;
public CustomClass(int myInt) {
this.myInt = myInt;
}
public int getMyInt() {
return myInt;
}
}
public class ApiClass {
static {
System.loadLibrary("myNativeLibrary");
}
private native void jniDoStuffWithCustomClass(CustomClass customClass);
public void doStuffWithCustomClass(CustomClass customClass) {
jniDoStuffWithCustomClass(customClass);
}
}
void jniDoStuffWithCustomClass(JNIEnv* env, jobject obj, jobject customObj)
{
jclass classref = env->GetObjectClass(customObj);
jmethodID midGetMyInt = env->GetMethodID(classref, "getMyInt", "()I");
jint val = env->CallIntMethod(customObj, midGetMyInt);
// Do something with int...
}
我通过调用new ApiClass().doStuffWithCustomClass(new CustomClass(5));
这在检测测试中非常有效,当我将库依赖项添加到同一个库项目中的demo-app模块时。但是当我在另一个项目中导入my-library.aar时,在本机代码中执行NoSuchMethodError
时会出现env->CallIntMethod(customObj, midGetMyInt)
异常。
为了让事情变得更奇怪,如果我在调用此新项目中的本机方法之前从Java调用CustomClass.getMyInt()
,则不会发生崩溃:
final CustomClass customClass = new CustomClass(5);
customClass.getMyInt(); // This call makes things not crash???
new ApiClass().doStuffWithCustomClass(customClass);
当然,在使用库API之前,我不想在Java中调用每个单独的类方法。那么,任何人都可以解释这里发生了什么吗?