Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JNI Error: NoSuchMethodError: no non-static method

Tags:

What I'm trying to do is simplified below.

  1. Java -> Call C++ function A
  2. C++ function A calls C++ function B
  3. C++ function B calls Java method C

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.


Code

Structure

  • Wrapper.java
  • native-lib.cpp
  • Wrapper.cpp

** <-> 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?

like image 338
김선달 Avatar asked Mar 04 '20 02:03

김선달


1 Answers

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{
    *;
}
like image 145
김선달 Avatar answered Sep 30 '22 20:09

김선달