How to add a JAR file to a C++ Qt Widgets for Android project?

Ubuntu 18.04
cmake 3.15.3
Qt Creator 4.11.0
Qt 5.14.1 for Android
Android NDK r21
Android ABI arm64-v8a
Using the clang++ compiler from the NDK r21 toolchain:
/home/cosmo/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --version --target=aarch64-none-linux-android21
Android (5900059 based on r365631c) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 207d7abc1a2abf3ef8d4301736d6a7ebc224a290) (based on LLVM 9.0.8svn)
Target: aarch64-none-linux-android21
Thread model: posix
InstalledDir: /home/cosmo/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin

I have a project in C++ using Qt. It has to be compiled for Android arm64-v8a
I’m using QtCreator with CMake as build system.
Compilation is OK, generation of APK by androiddeployqt is OK, installation on Android device is OK.
The application starts on the Android device, however it crashes because the C++ code, through the JVM, needs to load some Java classes from a JAR file of a separate library. But the JAR file does not belong to the APK.

How can I tell CMake to add the JAR file to my target executable?

I tried to set the ANDROID_JAR_DEPENDENCIES property of my target executable as follows:
set_target_properties( ${_exe_name} PROPERTIES ANDROID_JAR_DEPENDENCIES /path/to/audio_device_java.jar )

I tried to set CMAKE_JAVA_INCLUDE_PATH:
set( CMAKE_JAVA_INCLUDE_PATH /path/to/audio_device_java.jar )

I tried to use find_jar()
find_jar( audio_device_java
NAMES audio_device_java
PATHS /path/to )

The JAR file is never added to the generated APK.

1 Like

Qt Creator uses the android deployment tool which copies certain files to the Android package. You can specify the package directory in your CMakeLists.txt file with:

if (ANDROID)
     set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
endif()

You can create this directory along with template files in android by clicking “Create Templates” under Projects Mode (Ctrl+Five), “Build Steps” in “Build Android APK”.

In the “android” directory you can specify which JAR files to bundle in “build.gradle”, similar to the following:

dependencies {
    compile files('path/local_dependency.jar')
}

That was taken from https://riptutorial.com/gradle/example/8349/add-a-local-jar-file-dependency