What I'm trying to do is simplified below.
I have to store JVM(2) and global jobject(3).
But at part 3,
JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV called with pending exception java.lang.NoSuchMethodError: no non-static method "Lpackage/name/here/d/b;.setInput([F)V"
I always got this error.
Structure
** <-> means communicate
Other Java Classes <-> Wrapper.java
Wrapper.java <-> native-lib.cpp
native-lib.cpp <-> Wrapper.cpp
Wrapper.cpp <-> Other C++ Classes
Wrapper.java
private static long wrapperAddr = 0; // initializes later
private tfModel model;
private native void nativeSetModel(long native_ptr, tfModel model);
public Wrapper(...){
...
model = tfModel.create(tfModel.Model.MNIST, tfModel.Device.CPU, 1);
nativeSetModel(wrapperAddr, model);
}
native-lib.cpp
extern "C" JNIEXPORT void JNICALL
Java_package_name_here_jni_Wrapper_nativeSetModel(JNIEnv *env,
jobject instance,
jlong native_ptr,
jobject model){
auto wrapper = reinterpret_cast<Wrapper *>(native_ptr);
wrapper->setModel(env, model);
}
Wrapper.cpp
void setModel(JNIEnv *env, jobject _model){
env->GetJavaVM(&translater::jvm);
jobject gmodel = env->NewGlobalRef(_model);
translater::model = gmodel;
}
function A
...
translater::setInputTS(input1, 100); // error starts here
...
translater.h / translater.cpp (function B)
class translater{
public:
inline static JavaVM *jvm = nullptr;
inline static jobject model = nullptr;
// from: https://stackoverflow.com/a/30026231/8176989
static bool GetJniEnv(JavaVM *vm, JNIEnv **env);
static void predictTS(std::vector<float> &output);
static void translater::setInputTS(float* input, int len) {
JNIEnv *env;
bool did_attach = GetJniEnv(jvm, &env);
if(did_attach){
jclass clazz = env->GetObjectClass(model);
jmethodID jid_input = env->GetMethodID(clazz, "setInput", "([F)V"); // Here is where error happens
}
...
};
tfModel.java
public abstract class tfModel{
...
public void setInput(float[] array){
if(inputIndex < inputImageNum)
setInput_Image(inputIndex, array);
else
setInput_Tensor(inputIndex - inputImageNum, array);
++inputIndex;
}
}
How can I call
jmethodID jid_input = env->GetMethodID(clazz, "setInput", "([F)V");
correctly?
It was because proguard removes unused code, which can be wrong sometimes. I added the proguard settings, and everything works fine.
https://developer.android.com/studio/build/shrink-code#keep-code
.pro file
...
// added
-keep abstract class package.name.here.module.tfModel{
*;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With