Qt for Android - Building from Source

Qt for Android has some requirements that you are recommended to know before going through this guide. Accomplish the tasks in Getting Started with Qt for Android first.

You can download the Qt 5 sources from the Qt Downloads page, or follow the wiki guide for Getting the source code.

Building on Linux

Prepare the Build Environment

First, install the Android SDK in one of two ways:

Using Manual Installation

For more information, see Getting Started with Qt for Android.

Using Qt Creator

For more information, see Connecting Android Devices.

Using Android Studio

You can use Android Studio to download and installl the Android SDK packages required for developing applications for Android. For more information, see Android Studio documentation.

Building on Linux

To build Qt for Android under a Linux environment, follow the steps below:

Installing the License File

If you use Qt with a commercial license, the Qt tools look for a local license file. If you are using a binary installer or the commercial Qt Creator, your licenses are automatically fetched and stored in your local user profile ($XDG_DATA_HOME/Qt/qtlicenses.ini file).

If you do not use any binary installer or Qt Creator, you can download the respective license file from your Qt Account Web portal and save it to your user profile as $HOME/.qt-license. If you prefer a different location or file name, you need to set the QT_LICENSE_FILE environment variable to the respective file path.

Unpacking the Archive

If you have downloaded the source code archive from Qt Downloads, then unpack the archive if you have not done so already. For example, if you have the qt-everywhere-src-%VERSION%.tar.xz package, type the following commands at a command line prompt:

cd /tmp
gunzip qt-everywhere-opensource-src-%VERSION%.tar.gz        # uncompress the archive
tar xvf qt-everywhere-opensource-src-%VERSION%.tar          # unpack it

This creates the directory /tmp/qt-everywhere-src-%VERSION% containing the files from the archive. We only support the GNU version of the tar archiving utility. Note that on some systems it is called gtar.

Otherwise if you cloned the source code from Git, the source will be under qt5 folder.

Configuring and Building on Linux

Set the following environment variables, and add them to your PATH; preferably at the end of ~/.profile:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Note: As of Qt 5.15.8, JDK 11 or later is supported for Qt for Android.

Note: We use OpenJDK here, but you can also use other JDK alternatives such as AdoptOpenJDK.

To configure Qt for Android, create a shadow build directory to keep the source directory clean:

mkdir <path_to_build_dir>/build-qt
cd <path_to_build_dir>/build-qt

<path_to_qt_source>/configure -xplatform android-clang -prefix </path/to/install> -disable-rpath -nomake tests -nomake examples -android-ndk <path/to/sdk>/ndk/<ndk_version> -android-sdk <path/to/sdk> -no-warnings-are-errors

Qt Configure Options contains more information about the configure options.

You can customize your build configuration in a more advanced manner. For more information, see Advanced Build Arguments.

To build the configured Qt code, run the following command:

make -j$(nproc)

Note: nproc is optional. It represents the number of parallel jobs your system can do.

Then install the built Qt for Android:

make install

If you haven't provided the --prefix <install-dir> configure option, the installation is placed under /usr/local/Qt-<version>. In Debian/Ubuntu, you should prefix the make command with the sudo command.

Building on Windows

To build Qt for Android under a Windows environment, follow the steps below:

Preparing the Build Environment

Install the following:

Then set the respective environment variables from the Environment Variables system UI, or from the build command line prompt. For the default Command prompt:

set JDK_ROOT=<JDK_ROOT_PATH>\bin
set MINGW_ROOT=<MINGW_ROOT_PATH>\bin
set PERL_ROOT=<PERL_ROOT_PATH>\bin
set PATH=%MINGW_ROOT%;%PERL_ROOT%;%JDK_ROOT%:%PATH%

Then, in the command line prompt, verify that:

where gcc.exe

The command should list gcc.exe under the path <MINGW_ROOT> first.

where mingw32-make.exe

The command should list mingw32-make.exe under the path <MINGW_ROOT> first.

where javac.exe

The command should list javac.exe under the path <JDK_ROOT> first.

Note: Qt for Android does not support building with Microsoft Visual C++ (MSVC), we only support building with MinGW.

Installing the License File

If you have a commercially licensed Qt, install your license file. If you're using a binary installer or the commercial Qt Creator, your licenses are automatically fetched and stored in your local user profile %USERPROFILE%\AppData\Roaming\Qt\qtlicenses.ini. Alternatively, you can download the respective license file from your Qt Account web portal and save it to your user profile as %USERPROFILE%\.qt-license. If you prefer a different location or file name, you need to set the QT_LICENSE_FILE environment variable to the respective file path.

Configuring and Building on Windows

If you have downloaded the source code archive from Qt Downloads, unpack the archive. Uncompress the files into a temporary folder, for example, C:\Qt\Build\Src_%VERSION%. This path must not contain any spaces or Windows-specific file system characters.

Run the following command to configure Qt:

mkdir C:\Qt\Build\build-qt
cd C:\Qt\Build\build-qt

..\Src_%VERSION%\configure.bat -platform win32-g++ -xplatform android-clang -prefix <\path\to\install> -nomake tests -nomake examples -android-sdk <ANDROID_SDK_PATH> -android-ndk <ANDROID_SDK_PATH>\ndk\<ndk_version> -android-ndk-host windows-x86_64 -no-warnings-are-errors

Qt Configure Options contains more information about the configure options.

You can customize your build configuration in a more advanced manner. For more information, see Advanced Build Arguments.

To build the configured Qt for Android code, run the following:

mingw32-make.exe -j<N>

Note: <N> is optional. It represents the number of parallel jobs your system can do.

Now, to install Qt, run the following command:

mingw32-make.exe install

Advanced Build Arguments

You may provide the -android-abis parameter to limit the Android ABIs being built, with either of: armeabi-v7a, arm64-v8a, x86, or x86_64:

-android-abis armeabi-v7a,arm64-v8a

Note: If the parameter is not specified, Qt is built for all supported ABIs.

Qt for Android contains Java code which is compiled into *.jar files with javac. To set the javac version for source and target, use -android-javac-source and -android-javac-target respectively:

-android-javac-source 8 -android-javac-target 8

To debug Qt with a developer build instead of a prefix build, use the following instead of -prefix argument:

-developer-build

A developer build is meant to be used directly from the build directory.

Note: A developer build takes more storage than a prefix build.

Building Separate Modules

It is possible to build specific Qt modules only. There are two options:

  • Use the configured qt5 build with the following command:
    make -j$(nproc) module-qtbase

    The to install those modules, use:

    make -j$(nproc) module-qtbase-install_subtargets

    Note: On Windows, use mingw32-make.exe instead.

  • Configure and build qtbase only, then use the resulting qmake to build any other module. The only difference is that the configure script in qtbase directory has to be used:
    mkdir <path_to_build_dir>/build-qt/qtbase
    cd <path_to_build_dir>/build-qt/qtbase
    <path_to_qt_source>/qtbase/configure [...]

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