Qt5 JNI error: java.lang.ClassNotFoundException: Didn't find class ..
-
Hello, I am trying to make a simple example running ! I am unaware how my folder structure and CMake configuration should be exactly..
Here's what I have at the moment:
main.cpp
#include <QApplication> #include <QLabel> #include <QMainWindow> #include <QAndroidJniObject> int fibonacii(int n) { return QAndroidJniObject::callStaticMethod<jint>("org/qtproject/example/jnimessenger/JniMessenger", "fibonacci", " (I)I", n); } int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow mainWindow; QLabel label(QString::fromStdString(std::to_string(fibonacii(5))), &mainWindow); label.setGeometry(0, 0, 100, 100); mainWindow.show(); return app.exec(); }
JniMessenger.java
package org.qtproject.example.jnimessenger; public class JniMessenger { public static int fibonacii(int n) { if( n < 2) return n; return fibonacii(n-1) + fibonacii(n-2); } }
CMakeLists.txt
cmake_minimum_required(VERSION 3.21) project( Qt5 VERSION 1.0 DESCRIPTION "An example repository to showcase how to build a simple C++ android app with Qt/QML and CMake" LANGUAGES CXX) find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets) set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) if(NOT ANDROID) add_executable(Example) elseif(ANDROID) add_library(Example SHARED) find_package(Qt5 REQUIRED AndroidExtras) target_link_libraries(Example PRIVATE Qt5::AndroidExtras) set_target_properties(Example PROPERTIES LIBRARY_OUTPUT_NAME Qt5) add_dependencies(apk Example) endif() target_sources(Example PRIVATE android/src/org/qtproject/example/jnimessenger/JniMessenger.java src/main.cpp) target_link_libraries(Example PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
Here's my structure folder:
After running the app from adb logcat I get
01-17 00:12:35.384 27970 27999 W System.err: java.lang.ClassNotFoundException: Didn't find class "org.qtproject.example.jnimessenger.JniMessenger" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /system_ext/lib]]
I suppose that is because I need some way to tell CMake that JniMessenger.java exists and should be put somehow in the build folder, however after going through a lot of guides, documentations and other forums I am unaware how that should happen.
I haven't put my AndroidManifest.xml file because I think it is unnecessary to, but I will post it if someone needs to take a look at it.
I'd be grateful to get help for this since I have been trying to resolve that for a couple of days now...
-
@Ivelin the java code will get compiled if the
build.gradle
file points to it from the variablejava.srcDirs
.It is suggested that you copy the auto-generated build.grade file into your sourcetree and adjust it there.
You can double check if your java file gets compiled and included by intentionally making the compiler error on some code and seeing if it actually breaks the compile process. If your build doesn't complain, it didn't try to compile your java file.
I do strongly suggest moving to Qt6 for Android dev. It is much much more stable.
-
@TomZ, hello, thank you for your answer.
I accept your suggestion for Qt6, but I suppose I still have to go through the same stuff and I have a couple questions at the moment. I have found the auto-generated build.gradle file, but what is there to adjust ? Also I am unaware what exactly java.srcDirs is ?
Here's is the auto-generated build.gradle file:
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.6.0' } } repositories { google() jcenter() } apply plugin: 'com.android.application' dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) } android { /******************************************************* * The following variables: * - androidBuildToolsVersion, * - androidCompileSdkVersion * - qt5AndroidDir - holds the path to qt android files * needed to build any Qt application * on Android. * * are defined in gradle.properties file. This file is * updated by QtCreator and androiddeployqt tools. * Changing them manually might break the compilation! *******************************************************/ compileSdkVersion androidCompileSdkVersion.toInteger() buildToolsVersion '28.0.3' sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java'] aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl'] res.srcDirs = [qt5AndroidDir + '/res', 'res'] resources.srcDirs = ['resources'] renderscript.srcDirs = ['src'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } tasks.withType(JavaCompile) { options.incremental = true } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } lintOptions { abortOnError false } // Do not compress Qt binary resources file aaptOptions { noCompress 'rcc' } defaultConfig { resConfig "en" minSdkVersion = qtMinSdkVersion targetSdkVersion = qtTargetSdkVersion } }
I have also made an error on purpose, as you said, so I can check if my java file gets compiled. I changed it to that:
package org.qtproject.example.jnimessenger; public class JniMessenger { public static int fibonacii(int n) { if( n < 2) return n; return fibonacii(n-1) + fibonacii( // I just deleted those lines here } }
But my build is still successful, so I guess it doesn't get compiled.
Could you please explain me what is there to adjust and how to get the file compiled in order for me to use it with the JNI ?
-
@Ivelin said in Qt5 JNI error: java.lang.ClassNotFoundException: Didn't find class ..:
Also I am unaware what exactly java.srcDirs is
You can use the 'find' feature in any text editor you used to open the
build.gradle
file to find that text. It's there in the version you uploaded above...Check this post too, I expect your CMake may be faulty;
https://forum.qt.io/topic/153885/androidmanifest-xml-is-not-respected-for-android-project -
@TomZ, hello again
First of all I have changed my CMake file to this:
cmake_minimum_required(VERSION 3.21) project( Qt5 VERSION 1.0 DESCRIPTION "An example repository to showcase how to build a simple C++ android app with Qt/QML and CMake" LANGUAGES CXX) find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) if(NOT ANDROID) add_executable(Example) elseif(ANDROID) add_library(Example SHARED) set_target_properties(Example PROPERTIES QT_ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_SOURCE_DIR}/android") find_package(Qt5 REQUIRED AndroidExtras) target_link_libraries(Example PRIVATE Qt5::AndroidExtras) set_target_properties(Example PROPERTIES LIBRARY_OUTPUT_NAME Qt5) add_dependencies(apk Example) endif() target_sources(Example PRIVATE src/main.cpp) target_link_libraries(Example PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
second JniMessenger.java file is now in android/src/JniMessenger.java
and last I changed my main.cpp to
#include <QApplication> #include <QLabel> #include <QMainWindow> #include <QAndroidJniObject> int fibonacii(int n) { return QAndroidJniObject::callStaticMethod<jint>("JniMessenger", "fibonacci", " (I)I", n); } int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow mainWindow; QLabel label(QString::fromStdString(std::to_string(fibonacii(5))), &mainWindow); label.setGeometry(0, 0, 100, 100); mainWindow.show(); return app.exec(); }
I am not sure if I am getting futher from the solution. I have been playing around, but no result. After build I still do not get any errors and now with logcat I do not get any errors, but it still not working and I get 0 on my lable, also I couldn't understand what you mean by
@TomZ said in Qt5 JNI error: java.lang.ClassNotFoundException: Didn't find class ..:
It is suggested that you copy the auto-generated build.grade file into your sourcetree and adjust it there.
What is there to adjust ? Could you please explain ?
What am I doing wrong ? Why isn't it working ?
-
@Ivelin, just to point out - My Java file is still messed up on purpose, but I get successful built which should mean that it doesn't get compiled. I have tried variety of ways to compile it and have been looking through all kinds of posts without success.
I'd be grateful if someone could try to help me.
-
@TomZ, I have already tried.. I could try to rewrite everything I currently have in a post and try to explain what I have been trying if you think that would be of any help. I am so confused and have been trying to make this simple thing work for too long now..
-
@TomZ, hello, again me.
I haven't found a solution, but I have found that my android folder is totally being ignored ! I have even deleted it with the AndroidManifest.xml file and my build is still successful, so I suppose it is totally being ignored by Qt and CMake.
With the current information now does anything appear in your mind ?
-
@Ivelin you really don't have the option of using a Qt that is less than 3 years old? That's a very long time in tech land...
I never used Android on Qt5, knowing it was less than stellar supported. I can point you to a known working open source project and its code, but on Qt6. Find the links here: https://forum.qt.io/post/786794
-
@TomZ, hello, thank you again for replying.
It was mostly because I can use QtWebView wihtout qml that's why I wanted Qt5 from the lower version, but at this point I really do not care that much I just want it to work.
Right now I have this cmake file:
cmake_minimum_required(VERSION 3.21) project( example VERSION 1.0 DESCRIPTION "An example repository to showcase how to build a simple C++ android app with Qt/QML and CMake" LANGUAGES CXX) set(CMAKE_PREFIX_PATH "/home/ivelin/Qt/5.15.2/android") find_package(Qt5 REQUIRED COMPONENTS Core Gui AndroidExtras Widgets) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) if(NOT ANDROID) add_executable(example) elseif(ANDROID) add_library(example SHARED) set_target_properties(example PROPERTIES QT_ANDROID_PACKAGE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/android COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS} MOBILE") find_package(Qt5 REQUIRED AndroidExtras) target_link_libraries(example PRIVATE Qt5::AndroidExtras) set_target_properties(example PROPERTIES LIBRARY_OUTPUT_NAME example) add_dependencies(apk example) endif() target_sources(example PRIVATE src/main.cpp) target_link_libraries(example PRIVATE Qt5::Core Qt5::AndroidExtras Qt5::Widgets)
With the line you have suggested in the other post.
I have this AndroidManifest.xml file on purpose:
<?xml version="1.0"?> <manifest package="org.qtproject.example.cmakeqtexample" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto"> </manifest>
but cmake generates different one in android-build, again ignoring my android folder.. I am desperate at this point..
Also I didn't say but I am running this CMake through CMakePresets file in which I have the current key parts:
{ "name": "ci-ninja-android-debug", "displayName": "Ninja Android Debug", "inherits": [ "Qt-android", "ci-ninja", "android" ], "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } }, { "name": "Qt-android", "hidden": true, "cacheVariables": { "CMAKE_PREFIX_PATH": "/home/ivelin/Qt/5.15.2/android", "CMAKE_FIND_ROOT_PATH_MODE_PACKAGE": "BOTH" } }, { "name": "ci-ninja", "displayName": "Ninja", "description": "build using Ninja generator", "inherits": [ "ccache-env" ], "generator": "Ninja", " }, { "name": "android", "toolchainFile": "/opt/android-ndk/build/cmake/android.toolchain.cmake", "cacheVariables": { "ANDROID_ABI": "armeabi-v7a", "ANDROID_PLATFORM": "26", "ANDROID_SDK": "/opt/android-sdk", "ANDROID_BUILD_ABI_armeabi-v7a": "ON" }, "environment": { "JAVA_HOME": "/usr/lib/jvm/java-1.11.0-openjdk-amd64", "ANDROID_SDK_ROOT": "/opt/android-sdk", "ANDROID_NDK_ROOT": "/opt/android-ndk" }, "binaryDir": "${sourceDir}/build_android" },
and I build those stuff by:
cmake --preset=ci-ninja-android-debug -S ./ cmake --build build_android --target apk adb install -r build_android/android-build/CMakeQtAPKExample.apk
However, this shouldn't make any difference. Am I right ?
I'd do anything for it to work at this point. I am trying right now to move to Qt6, but I would be really greatful if someone tries to help me resolve it in Qt5..
Could you tell me what's wrong with my current setup please ?
-
@Ivelin
like this
install(FILES
android/AndroidManifest.xml
android/gradle/wrapper/gradle-wrapper.jar
android/gradlew
android/build.gradle
android/gradlew.bat
android/gradle/wrapper/gradle-wrapper.properties
android/res/values/libs.xml
android/src/com/example/ExtendsQtWithNative.java
android/src/com/example/ExtendsQtWithJava.java
DESTINATION .
)