Third-party Android Libraries

This guide describes how to include a Java-based third-party library in your application package. There are many Java libraries which provide APIs that may be useful to your application.

Prerequisites

This guide assumes that the androiddeployqt tool is used for constructing the deployment package. When using Qt Creator for building and deploying, androiddeployqt is used behind the scenes, so this also applies to development with Qt Creator. Explaining how to access the Java APIs after being included in the project is not in the scope of this guide. For more information, see Customizing the Package Templates.

Including a Library to an Android Project

The very first thing you need to do is to copy the library files into your project. The contents of the library have to be copied without modifications into the packaging directory, i.e. into the path set with qmake variable ANDROID_PACKAGE_SOURCE_DIR. For more information, see Android Libraries.

If you are using Qt Creator, you can quickly set up the packaging directory structure by selecting Projects > Build > Build Android APK > Create Templates. This creates a directory for your Android package customizations. Copy the library directory into $ANDROID_PACKAGE_SOURCE_DIR/libs/.

Adding a .jar or .aar Library

By default, Qt for Android uses can use .jar or .aar libraries that are found in the path $ANDROID_PACKAGE_SOURCE_DIR/libs/. Qt has the following rule in build.gradle file that is part of the Gradle files used by Android build:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

Adding a Library Project

Having a library called CustomLibrary, similar to the previous approach, copy the library project to your packaging directory $ANDROID_PACKAGE_SOURCE_DIR/libs/, then create a file settings.gradle with the following content:

include ':libs:CustomLibrary'

Then, add the dependency to build.gradle file inside the dependencies block:

dependencies {
    implementation project(":libs:CustomLibrary")
}

For more information on adding libraries to an Android project, see Add build dependencies Android documentation.

Deployment

Once Gradle settings and the library files are all properly configured, Qt for Android should be able to build your APK or AAB package with your custom library.

© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.