Qt for Android - Building from Source | Qt 6.2

- Qt 6.2
- Qt for Android - Building from Source
- Qt 6.2.4 Reference Documentation

Getting Started with Qt for Android ◦ Porting to Android

# 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 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
- Using Qt Creator
- Using Android Studio

Then, you need the following tools:

- A JDK package such as AdoptOpenJDK, JDK, or OpenJDK.
- `CMake`
- `Ninja`

#### 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 Qt for Android

Qt 6 allows building Qt for Android from source code using `CMake`. Qt 6 keeps the same configure script from Qt 5. The main difference is that Qt for Android is dependent on a host Qt build, which means to build Qt for Android, you need to build Qt for the desktop platform used as a host (for example Linux, macOS, or Windows).

With the configure script, the same arguments can be used as before. For more information, see Building Qt Sources.

Qt for Android supports the following ABIs armeabi-v7a, arm64-v8a, x86, and x86_64.

Note: Qt 6.0 does not support multi-abi feature, for that reason setting the -android-abis is mandatory.

## Building on Linux

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

### 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-8-openjdk-amd64
     export CMAKE_ROOT=~/Qt/Tools/CMake/bin
     export NINJA_ROOT=~/Qt/Tools/Ninja
     export PATH=$JAVA_HOME/bin:$CMAKE_ROOT:$NINJA_ROOT:$PATH

Note: JDK 11 or earlier must be used to properly build 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 -platform android-clang -prefix </path/to/install> -android-ndk <path/to/sdk>/ndk/<ndk_version> -android-sdk <path/to/sdk> -qt-host-path <path_to_host_qt_installation> -android-abis arm64-v8a

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.

Note: For more information about building Qt 6 with CMake, see Building with CMake.

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

     cmake --build . --parallel

Also, the following is possible instead:

     ninja -j$(nproc)

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

Now, to install Qt, run the following command:

     cmake --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

In addition to the requirements from `Prepare the Build Environment`, you need the following on Windows:

- MinGW 7.3 toolchain
- Perl

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 CMAKE_ROOT=C:\Qt\Tools\CMake_64\bin
     set NINJA_ROOT=C:\Qt\Tools\Ninja
     set PATH=%MINGW_ROOT%;%CMAKE_ROOT%;%NINJA_ROOT%;%PERL_ROOT%;%JDK_ROOT%;%PATH%

Then, in the command line prompt, verify that:

     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: JDK 11 or earlier must be used to properly build Qt for Android.

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

### 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 android-clang -prefix <\path\to\install> -android-sdk <ANDROID_SDK_PATH> -android-ndk <ANDROID_SDK_PATH>\ndk\<ndk_version> -qt-host-path <path_to_host_qt_installation> -android-abis arm64-v8a

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.

Note: For more information about building Qt 6 with CMake, see Building with CMake.

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

     cmake --build . --parallel

Also, the following is possible instead:

     ninja -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:

     cmake --install .

## Advanced Build Arguments

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:

       ninja -j$(nproc) module-qtbase

  The to install those modules, use:

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

- Configure and build qtbase only, then use the resulting qt-configure-module to configure 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 [...]

  Then to configure `qtdeclarative` for example:

       mkdir <build_directory>/build-qtdeclarative
       cd <build_directory>/build-qtdeclarative
       <qt_for_android_install_path>/bin/qt-configure-module <qt_source_path>/qtdeclarative
       cmake --build . --parallel
       cmake --install .

## Building User Projects

To build CMake projects with the resulting Qt installation, use `qt-cmake` wrapper. For Android the following has to be be provided:

     <qt_for_android_install_path>/bin/qt-cmake -DANDROID_SDK_ROOT=<android_sdk_path> -DANDROID_NDK_ROOT=<android_ndk_path> -S <source_dir> -B <build_dir>

Additionally, you can still use `qmake` to build user projects, as before in Qt 5.

Getting Started with Qt for Android ◦ Porting to Android