Search code examples
javaandroidc++qtjava-native-interface

calling java method from c++ in qt


I am trying to call a method defined in android activity in c++ qt using QAndroidJniObject. here is my call in c++ class

QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod("com/android/app/appActivity",
                                                                      "appData",
                                                                      "(I)Ljava/lang/String;");
QString dataValue = data.toString();
qDebug() <<"Data is " << dataValue;

this appData is defined in appActiviy android class and it returns a String this is defined method I want to call and get the returned string value

static  String appData(){
    Log.d("App Data is ", "Working");
    return data;
}

but I am getting null is dataValue and it is not throwing any error too.


Solution

  • Thanks guys for your answer, finally I figured it out. It was way simple then I was trying

    QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod("com/android/app/appActivity",
                                                                      "appData",
                                                                      "(I)Ljava/lang/String;");
    

    In this code I was not aware that this(I)Ljava/lang/String; means type of parameter your Java method is accepting but in my case there were none. So the correct answer is

    QAndroidJniObject data =  QAndroidJniObject::callStaticObjectMethod<jstring>("com/android/app/appActivity",
                                                                      "appData")`
    

    denotes return type of my defined java method. I guess it was silly mistake from my end...thanks again