As seen in the previous JNI example, it is not very convenient to replace the Qt main activity with an extended one and add static wrapper methods to it. There must be a more straight forward way to use Android Java code in Qt5.

The reason for replacing the main activity was to get a handle on the main activity object. Without it we cannot access the services and items the activity owns. But there is a different way to get a handle on that object. We can use the activity() method of the QtNative class, like so:

We place the classes in the qmake directory “ANDROID_PACKAGE_SOURCE_DIR” in the subpath “src/org/openterrain/example/Main.java” resp. “MyJavaClass.java” and add them to “OTHER_FILES”. Back in C++ we create an instance of that class and start a method call: