Qt5 JNI error: java.lang.ClassNotFoundException: Didn't find class ..
-
@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 .
)