Why "java.lang.NoSuchMethodError: no static method" from (JNIenv*)->GetStaticMethodID?

-1

My Android NDK project's Java Code calls C++ function ,

    extern "C" JNIEXPORT jstring JNICALL Java_com_crimson_tub_MainActivity_stringFromJNI(JNIEnv *env, jobject jo)

I want to call

    public static void requestPermissions(@NonNull final Activity activity, @NonNull final String[] permissions, @IntRange(from = 0L) final int requestCode) 

found in Android Document:

https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#requestPermissions(android.app.Activity, java.lang.String[], int)

I have tried few variations of the code where i think the problem is,

jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Landroid/app/Activity;[Ljava/lang/String;I)V;"),
//jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Lcom/company/project/MainActivity;[Ljava/lang/String;I)V;"),
  //jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Ljava/lang/Object;[Ljava/lang/String;I)V;"),

all of the above alternatives lead to same exception,

    No pending exception expected: java.lang.NoSuchMethodError: no static method "Landroid/support/v4/app/ActivityCompat;.requestPermissions(Landroid/app/Activity;[Ljava/lang/String;I)V;"

c++ code,

    extern "C" JNIEXPORT jstring JNICALL
    Java_com_crimson_tub_MainActivity_stringFromJNI(JNIEnv *env,jobject jo) {

        std::string hello;

        jobjectArray ret;
        int i;

        char *data[2]= { "android.permission.CAMERA" ,
                 "android.permission-group.CALENDAR"};

        ret= (jobjectArray)env->NewObjectArray(2,env->FindClass("java/lang/String"),env->NewStringUTF(""));

        for(i=0;i<2;i++) env->SetObjectArrayElement(ret,i,env->NewStringUTF(data[i]));

        jint result = 0xffffffff;

        jclass clzz = env->FindClass("android/support/v4/app/ActivityCompat");
        if (!clzz){
            hello += "-class";
        }else{
            hello += "+class";
        }

        jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Landroid/app/Activity;[Ljava/lang/String;I)V;"),
        //jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Lcom/company/project/MainActivity;[Ljava/lang/String;I)V;"),
          //jmethodID jmid = env->GetStaticMethodID(clzz,"requestPermissions", "(Ljava/lang/Object;[Ljava/lang/String;I)V;"),

        env->CallStaticVoidMethod(clzz,jmid,jo,ret,result);

        env->DeleteGlobalRef(jo);
        return env->NewStringUTF(hello.c_str());

    }

i would like to be able to call Android Java functions via JNI like,

    ActivityCompat.requestPermissions 

directly from c++ so later i can remove Java code from my project.

I have been working on this issue for quite a while now and still unsuccessful.

Gratitude towards All the help and it is greatly appreciated.

Thank you all.

c++
android-ndk
jnienv
asked on Stack Overflow Apr 10, 2019 by DisplayName • edited Apr 10, 2019 by DisplayName

1 Answer

0

Problem was the ; after the V in the signature. now it works. Lol.

answered on Stack Overflow Apr 10, 2019 by DisplayName

User contributions licensed under CC BY-SA 3.0