Search code examples
androidc++qtjava-native-interfaceclassnotfoundexception

How can I resolve a ClassNotFoundEception of Qt app on an Android device?


I'm using Qt for Android and JNI to access Android APIs. This works fine, if I run the app in simulators with a virtual Android device but not on a physical Android device. This is confusing.

I develop with Qt Creator 4.11.0 based on Qt 5.14.0 (Clang 10.0 (Apple), 64 bit). Here is the stack trace of the exception:

W System.err: java.lang.ClassNotFoundException: Didn't find class "com.myapp.launcher.worker.AppWorker" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib]]
W System.err:   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
W System.err:   at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
W System.err:   at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
W System.err:   at org.qtproject.qt5.android.QtNative.startQtApplication(Native Method)
W System.err:   at org.qtproject.qt5.android.QtNative$7.run(QtNative.java:387)
W System.err:   at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61)
W System.err:   at java.lang.Thread.run(Thread.java:764)

Has someone any idea? I assume it's not a bug in the source code, but somewhere else, maybe in the kit settings. I have used a Qt template for my project.


Solution

  • Here is my solution:

    I have created a new app from the template of Qt Creator and compared it with the example project for Qt Android:

    I conclude, that I have to modify the .pro file. First of all I had to add the following line:

    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android-sources
    

    Then I had to replace DISTFILES, that is automatically added by Qt Creator, if I create a Java class:

    OTHER_FILES += \
        main.qml \
        android-sources/src/com/hello/android/Backend.java \
        android-sources/AndroidManifest.xml
    

    Finally I had to edit the AndroidManifest.xml, that I have copied from the exmaple project, in the text editor mode:

    <activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="com.hello.android.Backend" android:label="Hello Android" android:screenOrientation="unspecified">
    

    The last step was not necessary in the project of my initial question.