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

**URL:** <https://discourse.cmake.org/t/how-to-add-a-jar-file-to-a-c-qt-widgets-for-android-project/597>\
**Category:** Code\
**Created:** [February 7, 2020, 10:50am UTC](https://discourse.cmake.org/t/how-to-add-a-jar-file-to-a-c-qt-widgets-for-android-project/597 "2020-02-07T10:50:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ludo](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@ludo](https://discourse.cmake.org/u/ludo)\
**Post date:** [February 7, 2020, 10:50am UTC](https://discourse.cmake.org/t/how-to-add-a-jar-file-to-a-c-qt-widgets-for-android-project/597/1 "2020-02-07T10:50:54Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Ben\_Cottrell](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ben_cottrell/32/1180_2.png) [@Ben\_Cottrell](https://discourse.cmake.org/u/Ben_Cottrell)\
**Post date:** [February 5, 2021, 9:04pm UTC](https://discourse.cmake.org/t/how-to-add-a-jar-file-to-a-c-qt-widgets-for-android-project/597/2 "2021-02-05T21:04:09Z")

</div>

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:

```auto
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:

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

```

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