Qt for Android - 从源代码构建

此页面描述了配置和构建的过程,针对 Qt for Android . To download and install a pre-built Qt for Android, follow the instructions on the Qt 快速入门 页面。

先决条件

To build Qt for Android from source please ensure all requirements from Qt for Android 快速入门 are met before continuing.

Windows

Building Qt for Android on Windows also requires the following software:

  • Mingw-w64 13.1 toolchain

注意: Qt for Android does not support building from source with Microsoft Visual C++ (MSVC).

Install the prerequisites and append their binary paths to the system PATH :

set MINGW_ROOT=<MINGW_ROOT_PATH>\bin
set PATH=%MINGW_ROOT%;%PATH%
					

To verify the installation, run:

where mingw32-make.exe
					

The command should list mingw32-make.exe under the path <MINGW_ROOT> 首先。

获取源

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

配置

With Qt 6, you can build Qt for Android from source code using CMake . Qt 6 for Android requires a host Qt build, which means to build Qt for Android, you need to build Qt for the desktop platform used as a host (that is, Linux, macOS, or Windows).

To configure and do a minimalistic host build to be used with Qt for Android build, use the following command:

mkdir ~/dev/build-host
cd ~/dev/build-host
./configure -developer-build -nomake tests -nomake examples
cmake --build . --target host_tools
					

That will configure a developer build for your host and only build the necessary dependencies used for the Android build. For more information on doing a full and more customizable build for your host, see 构建 Qt 源 .

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

mkdir -p ~/dev/build-qt-android
cd ~/dev/build-qt-android
					

Qt for Android supports the following device architectures (ABIs): arm64-v8a , x86_64 , x86 ,和 armeabi-v7a

And then run the configure script from within the build directory you just created:

<qt5_super_repo_path>/configure -prefix <install_path> -qt-host-path <qt_host_path> \
    -android-abis <abi> -android-sdk ~/Android/Sdk -android-ndk ~/Android/Sdk/ndk/26.1.10909125
					

The Android SDK is commonly installed by Qt Creator or Android Studio in the following locations:

  • Linux: ~/Android/Sdk/
  • macOS: ~/Library/Android/sdk/
  • Windows: C:\Users\<USER>\AppData\Local\Android\Sdk\

注意: configure always uses the Ninja generator and build tool if a ninja executable is available. Ninja is cross-platform, feature-rich, performant, and recommended on all platforms. The use of other generators might work but is not officially supported.

高级配置自变量

Before exploring this section, see Qt 配置选项 , which contains more information about the configure options.

Qt for Android contains Java code compiled into *.jar files with the javac compiler. 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 build Qt as a developer build instead of a prefix build, use the following instead of the -prefix 自变量:

-developer-build
					

The -qt-host-path should point to the qtbase directory in the host build directory in a host developer build. For example: ~/dev/build-qt-host/qtbase .

构建

To build Qt, run the following command:

cmake --build . --parallel
					

安装

For prefix builds, to install Qt, run the following command:

cmake --install .
					

On Unix, if you haven't provided the -prefix <install_path> configure option, the installation is placed under /usr/local/Qt-<version> , in that case, you would need to use sudo with the install command.