How can I use QAndroidJniObject to call Java code from Qt?

Starting with an empty Qt Android project created with Qt Creator, how can I execute native Java code?

For example, if I have this java method:

class HelloJava {
    public static String sayHello(int a, int b) {
        return "Hello from Java, a+b=" + (a+b);
    }
}

      

And I have this Qt method that should call the method HelloJava.sayHello()

:

QString call_HelloJava_sayHello(int a, int b) {
    // What goes in here?
}

      

Where do I put Java code in my project and what goes into the method call_HelloJava_sayHello()

?

+3


source to share


1 answer


The Qt doc is quite detailed on this topic:

// Java class
package org.qtproject.qt5;
class TestClass
{
   static String fromNumber(int x) { ... }
   static String[] stringArray(String s1, String s2) { ... }
}


// C++ code

// The signature for the first function is "(I)Ljava/lang/String;"
QAndroidJniObject stringNumber = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                                                                           "fromNumber"
                                                                           "(I)Ljava/lang/String;",
                                                                           10);

// the signature for the second function is "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"
QAndroidJniObject string1 = QAndroidJniObject::fromString("String1");
QAndroidJniObject string2 = QAndroidJniObject::fromString("String2");
QAndroidJniObject stringArray = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/TestClass",
                                                                          "stringArray"
                                                                          "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;"
                                                                           string1.object<jstring>(),
                                                                           string2.object<jstring>());

      

So the sig of your function should be "(I;I;)Ljava/lang/String;"

And also this:

ANDROID_PACKAGE_SOURCE_DIR: This variable can be used to specify the directory where additions and changes can be made to the default Android Package Template. The androiddeployqt tool will copy the app template from Qt to the build directory and then it will copy the contents of ANDROID_PACKAGE_SOURCE_DIR over that, overwriting any existing files. An upgrade step where parts of the original files are automatically modified to reflect the customizations are then run into the resulting merged package. If you, per instance, want to create your own AndroidManifest.xml for your application, then put that directly in the folder pointed to by this variable. You can also add custom Java files to ANDROID_PACKAGE_SOURCE_DIR / CSI.

Note. When adding custom versions of assembly files (e.g. strings.xml, libs.xml, AndroidManifest.xml, etc.) to your project, make sure you copy them from the package template, which is located at $ QT / SRC / Android / Java. You should never copy files from an assembly, as these files have been modified to match the current assembly setup.



So, you need to specify this and the java files will be automatically expanded.

Just add this to your .pro file (if your java sources are hosted in /path/to/project/myjava/src

:

android {
    ANDROID_PACKAGE_SOURCE_DIR=$$_PRO_FILE_PWD_/android
    QT += androidextras
}

      

_PRO_FILE_PWD_

is a qmake variable that will be resolved for the project directory.

+1


source







All Articles