Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt5 JNI error: java.lang.ClassNotFoundException: Didn't find class ..
Forum Updated to NodeBB v4.3 + New Features

Qt5 JNI error: java.lang.ClassNotFoundException: Didn't find class ..

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
15 Posts 4 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Ivelin
    17 Jan 2024, 16:34

    @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 ?

    T Offline
    T Offline
    TomZ
    wrote on 18 Jan 2024, 14:05 last edited by
    #6

    @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

    I 1 Reply Last reply 18 Jan 2024, 21:21
    0
    • T TomZ
      18 Jan 2024, 14:05

      @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

      I Offline
      I Offline
      Ivelin
      wrote on 18 Jan 2024, 21:21 last edited by
      #7

      @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 ?

      I 1 Reply Last reply 18 Jan 2024, 23:10
      0
      • I Ivelin
        18 Jan 2024, 21:21

        @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 ?

        I Offline
        I Offline
        Ivelin
        wrote on 18 Jan 2024, 23:10 last edited by
        #8

        @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.

        T 1 Reply Last reply 18 Jan 2024, 23:13
        0
        • I Ivelin
          18 Jan 2024, 23:10

          @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.

          T Offline
          T Offline
          TomZ
          wrote on 18 Jan 2024, 23:13 last edited by
          #9

          @Ivelin quick tip,
          sometimes the java stuff doesn't find that there are java files without nuking your build dir and re-running cmake.

          I 1 Reply Last reply 18 Jan 2024, 23:21
          0
          • T TomZ
            18 Jan 2024, 23:13

            @Ivelin quick tip,
            sometimes the java stuff doesn't find that there are java files without nuking your build dir and re-running cmake.

            I Offline
            I Offline
            Ivelin
            wrote on 18 Jan 2024, 23:21 last edited by
            #10

            @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..

            T 1 Reply Last reply 19 Jan 2024, 11:37
            0
            • I Ivelin
              18 Jan 2024, 23:21

              @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..

              T Offline
              T Offline
              TomZ
              wrote on 19 Jan 2024, 11:37 last edited by
              #11

              @Ivelin not sure then.

              My advice, start with something that is known to work. For instance: https://github.com/ekke/ekkesSHAREexample

              I 1 Reply Last reply 21 Jan 2024, 11:39
              0
              • T TomZ
                19 Jan 2024, 11:37

                @Ivelin not sure then.

                My advice, start with something that is known to work. For instance: https://github.com/ekke/ekkesSHAREexample

                I Offline
                I Offline
                Ivelin
                wrote on 21 Jan 2024, 11:39 last edited by
                #12

                @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 ?

                T 1 Reply Last reply 22 Jan 2024, 13:15
                0
                • I Ivelin
                  21 Jan 2024, 11:39

                  @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 ?

                  T Offline
                  T Offline
                  TomZ
                  wrote on 22 Jan 2024, 13:15 last edited by
                  #13

                  @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

                  I 1 Reply Last reply 22 Jan 2024, 15:36
                  0
                  • T TomZ
                    22 Jan 2024, 13:15

                    @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

                    I Offline
                    I Offline
                    Ivelin
                    wrote on 22 Jan 2024, 15:36 last edited by
                    #14

                    @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 ?

                    L 1 Reply Last reply 11 Sept 2024, 10:08
                    0
                    • I Ivelin
                      22 Jan 2024, 15:36

                      @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 ?

                      L Offline
                      L Offline
                      luke6
                      wrote on 11 Sept 2024, 10:08 last edited by
                      #15

                      @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 .
                      )

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved