Search code examples
c++qtjava-native-interfaceqtandroidextras

Qt does not compile callStaticObjectMethod()


I wrote the following code from this question and it was compiled and worked perfectly:

QAndroidJniObject str =  QAndroidJniObject::callStaticObjectMethod<jstring>(
                               "org/.../TestClass"
                               ,"staticMethod");

Now I have changed the java method and it needs an input parameter of type string.

The Java code:

public class TestClass{
    public string str;
    public TestClass() {
        str = "Test From Java";
    }
    public static String staticMethod(String str) {
        return "Test From Java, "+str;
    }
}

But adding the method signature and input parameter does not work for me. I wrote this code to invoke the static method within the mentioned java class using JNI:

QAndroidJniObject val = QAndroidJniObject::fromString("Test String");
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod<jstring>(
          "org/.../TestClass"
          ,"staticMethod"
          ,"(Ljava/lang/String;)Ljava/lang/String;"
          ,val.object<jstring>());

But Qt creator does not build it, printing this error:

...testclass.cpp:21: error:
no matching function for call to 'QAndroidJniObject::callStaticObjectMethod(
const char [36], const char [13], const char [39], _jstring*)'
                                                        ,val.object<jstring>());
                                                                              ^

Thanks for any help.

I also tried callStaticMethod

For this:

jstring str = QAndroidJniObject::callStaticMethod<jstring>(
                      "org/.../TestClass"
                      ,"staticMethod"
                      ,"(Ljava/lang/String;)Ljava/lang/String;"
                      ,val.object<jstring>());

I got the following error :

...\testclass.cpp:21: error: undefined reference to '_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*, char const*, ...)'

## I also tried callStaticObjectMethod without template parameter ##

QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("org/...TextClass" ,"staticMethod" ,"(Ljava/lang/String;)Ljava/lang/String;",val.object());

It always returns an empty string. I'm not sure if it is really emty or not. I use qCritical() << str.toString(); to print the string but an empty qout is printed then!


Solution

  • Try:

    QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod(
              "org/.../TestClass"
              ,"staticMethod"
              ,"(Ljava/lang/String;)Ljava/lang/String;"
              ,val.object<jstring>());
    

    I think this function doesn't take template parameter.

    Than you can do:

    str.toString() //returns QString
    

    And make sure you have imported the Java source files to your android build. For example if your java classes are under android-sources folder add this to your .pro file:

    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources