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. One example is the Google Play Services add-on provided in the Android SDK. We will use this library as an example in this guide, but the same approach may be taken to include other libraries, provided that they have been made to be included as library projects in an Android 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 they have been included in the application is not in the scope of this guide.

Including the Library in the Source

The very first thing we need to do is to copy the actual library project into the source of our project. We want the contents of the library project to be copied without modifications into the packaging directory, so we need to place it into the Android Package Source Directory. This can be any directory which is pointed to by the qmake variable ANDROID_PACKAGE_SOURCE_DIR.

If you are using Qt Creator, you can quickly set up the directory structure by clicking on the Create AndroidManifest.xml button in the Deployment settings of your project. This will create an Android Package Source Directory and place the AndroidManifest.xml file inside it. Copy the library project directory into this directory.

For example, once it has been installed via the Android SDK Maintenance application, the Google Play Services library project is located under the path $ANDROID_SDK_ROOT/extras/google/google_play_services/libproject/google-play-services_lib. If the Android Package Source Directory of our project is $PROJECT_ROOT/android, then we can copy the google-play-services_lib directory in there, creating $PROJECT_ROOT/android/google-play-services_lib.

Adding the Library to the Project Dependencies

Once the library code has been copied into the project, we need to tell the Android build system about it. This is done in the project.properties file. Create a file called project.properties inside the Android Package Source Directory and add a line to it which assigns the relative path of the library project to the property android.library.reference.1. If you want to include multiple library projects, increment the counter at the end of the property name for each.

In our example, we would create $PROJECT_ROOT/android/project.properties and add the following contents:

android.library.reference.1=google-play-services_lib/

Deployment

And that's it: Provided that the path referenced in the project.properties file is located under the Android Package Source Directory, the deployment tool will copy it into the packaging directory and perform the necessary steps to include it in your APK or AAB. You can now add Java code to your application which accesses the APIs provided by the library project.

© 2020 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.