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. Using Java in my Qt/Android project
QtWS25 Last Chance

Using Java in my Qt/Android project

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 4 Posters 1.3k 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.
  • T Offline
    T Offline
    TomZ
    wrote on 13 May 2023, 17:06 last edited by
    #1

    I have a normal QML app that I ported to Qt/Android. Works great.

    Now I want to do a bit more integration into the Android system and it looks like I need to create some Java classes to do so. I can program Java, I'll live.

    Whats got me stumped is how I can compile those Java classes from my cmake based C++ project and include them into the APK which is made with the cmake target;
    qt6_android_add_apk_target()

    Any indication on how Qt integration there works is appreciated!

    A 1 Reply Last reply 30 May 2023, 08:33
    0
    • T Offline
      T Offline
      TomZ
      wrote on 27 Jun 2023, 15:24 last edited by TomZ
      #3

      By accident I actually found the answer to this.

      cmake creates a gradle default build file in your build dir which automatically compiles any and all Java files provided they are placed in the right subdirectory.

      This completely undocumented directory is a subdirectory under your android dir (the dir where your AndroidManifest.xml goes).
      So under that dir you create a 'src' dir and under that you place all your Java files.

      Which magically get found and compiled.

      Took me 4 months to figure that out :-)

      ps. it is not needed to copy any of the gradle files to your git repository, they get recreated anyway.

      J 1 Reply Last reply 27 Jun 2023, 15:42
      1
      • T TomZ
        13 May 2023, 17:06

        I have a normal QML app that I ported to Qt/Android. Works great.

        Now I want to do a bit more integration into the Android system and it looks like I need to create some Java classes to do so. I can program Java, I'll live.

        Whats got me stumped is how I can compile those Java classes from my cmake based C++ project and include them into the APK which is made with the cmake target;
        qt6_android_add_apk_target()

        Any indication on how Qt integration there works is appreciated!

        A Offline
        A Offline
        Aninoss
        wrote on 30 May 2023, 08:33 last edited by Aninoss
        #2

        @TomZ said in Using Java in my Qt/Android project:

        Any indication on how Qt integration there works is appreciated!

        In the Project Section in Qt Creator, in the Build APK > Create Templates when you create a template it will make an Android folder you can add all your Java classes to expand your Application functionality and you can access all your C++ classes as objects because Qt takes the code we write in C++ and makes it accessible by making it into an object and then access it with Java from the Android app so we can also access it with ease and expand its functionality more.
        Ps: this is the best way to add an Android file to your Cmake file

        if(ANDROID)
            set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
            set_property(TARGET QGuiApp PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR ${ANDROID_PACKAGE_SOURCE_DIR})
            add_custom_target(cmake_android_package
                SOURCES
                ${ANDROID_PACKAGE_SOURCE_DIR}/AndroidManifest.xml
                ${ANDROID_PACKAGE_SOURCE_DIR}/build.gradle
                ${ANDROID_PACKAGE_SOURCE_DIR}/grable.properties
                ${ANDROID_PACKAGE_SOURCE_DIR}/res/values/libs.xml
            )
        
            include(D:/Dev/Dev/android_openssl/CMakeLists.txt)
        endif()
        
        G 1 Reply Last reply 20 Mar 2025, 11:50
        1
        • T Offline
          T Offline
          TomZ
          wrote on 27 Jun 2023, 15:24 last edited by TomZ
          #3

          By accident I actually found the answer to this.

          cmake creates a gradle default build file in your build dir which automatically compiles any and all Java files provided they are placed in the right subdirectory.

          This completely undocumented directory is a subdirectory under your android dir (the dir where your AndroidManifest.xml goes).
          So under that dir you create a 'src' dir and under that you place all your Java files.

          Which magically get found and compiled.

          Took me 4 months to figure that out :-)

          ps. it is not needed to copy any of the gradle files to your git repository, they get recreated anyway.

          J 1 Reply Last reply 27 Jun 2023, 15:42
          1
          • T TomZ has marked this topic as solved on 27 Jun 2023, 15:25
          • T TomZ
            27 Jun 2023, 15:24

            By accident I actually found the answer to this.

            cmake creates a gradle default build file in your build dir which automatically compiles any and all Java files provided they are placed in the right subdirectory.

            This completely undocumented directory is a subdirectory under your android dir (the dir where your AndroidManifest.xml goes).
            So under that dir you create a 'src' dir and under that you place all your Java files.

            Which magically get found and compiled.

            Took me 4 months to figure that out :-)

            ps. it is not needed to copy any of the gradle files to your git repository, they get recreated anyway.

            J Offline
            J Offline
            JoeCFD
            wrote on 27 Jun 2023, 15:42 last edited by
            #4

            @TomZ Glad you made it. 4 months are not a short time. You can try to push your post up if you can not get the right answer. Somehow I missed your post.

            1 Reply Last reply
            0
            • A Aninoss
              30 May 2023, 08:33

              @TomZ said in Using Java in my Qt/Android project:

              Any indication on how Qt integration there works is appreciated!

              In the Project Section in Qt Creator, in the Build APK > Create Templates when you create a template it will make an Android folder you can add all your Java classes to expand your Application functionality and you can access all your C++ classes as objects because Qt takes the code we write in C++ and makes it accessible by making it into an object and then access it with Java from the Android app so we can also access it with ease and expand its functionality more.
              Ps: this is the best way to add an Android file to your Cmake file

              if(ANDROID)
                  set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
                  set_property(TARGET QGuiApp PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR ${ANDROID_PACKAGE_SOURCE_DIR})
                  add_custom_target(cmake_android_package
                      SOURCES
                      ${ANDROID_PACKAGE_SOURCE_DIR}/AndroidManifest.xml
                      ${ANDROID_PACKAGE_SOURCE_DIR}/build.gradle
                      ${ANDROID_PACKAGE_SOURCE_DIR}/grable.properties
                      ${ANDROID_PACKAGE_SOURCE_DIR}/res/values/libs.xml
                  )
              
                  include(D:/Dev/Dev/android_openssl/CMakeLists.txt)
              endif()
              
              G Offline
              G Offline
              Golubev
              wrote on 20 Mar 2025, 11:50 last edited by
              #5

              @Aninoss Hi! faced with the same problem, could you show me your code. I figured out what's in the AndroidManifest directory, but I can't find where exactly.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Golubev
                wrote on 21 Mar 2025, 06:07 last edited by
                #6

                I found a working way to solve the problem. https://scythe-studio.com/en/blog/how-to-interface-qt-with-android-java-code-2023 And also check. https://doc.qt.io/qt-6/qtcore-platform-androidnotifier-example.html

                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