Qt for Android - 从源代码构建

本页介绍配置和构建Qt for Android 的过程。要下载并安装预构建的 Android 版 Qt,请按照 "Qt 入门"页面上的说明进行操作。

前提条件

要从源代码构建 Qt for Android,请在继续之前确保满足 "开始使用 Qt for Android "中的所有要求。

Windows

在 Windows 上构建 Qt for Android 还需要以下软件:

  • Mingw-w64 13.1 工具链

注意: Qt for Android 不支持使用 Microsoft Visual C++ (MSVC) 从源代码构建。

安装先决条件,并将其二进制路径附加到系统PATH 中:

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

要验证安装,请运行

where mingw32-make.exe

该命令应首先列出<MINGW_ROOT>路径下的 mingw32-make.exe。

获取源代码

你可以从Qt 下载页面下载 Qt源代码,或按照维基指南获取源代码

配置

有了 Qt 6,你可以使用CMake 从源代码构建 Android 版 Qt。Android 版 Qt 6 需要 Qt 宿主构建,这意味着要构建 Android 版 Qt,需要为用作宿主的桌面平台(即 Linux、macOS 或 Windows)构建 Qt。

要配置并执行与 Qt for Android 联编版一起使用的简约主机联编版,请使用以下命令:

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

这将为你的主机配置开发者构建,并只构建 Android 构建所需的依赖项。有关为你的主机进行完整的、更可定制的构建的更多信息,请参阅构建 Qt 源代码

要配置 Qt for Android,请创建一个影子构建目录,以保持源代码目录的整洁:

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

Qt for Android 支持以下设备架构 (ABI):arm64-v8a,x86_64,x86, 和armeabi-v7a

然后运行配置脚本:

./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

Android SDK 通常通过Qt Creator 或 Android Studio 安装在以下位置:

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

注意: 如果有ninja 可执行文件,configure 会始终使用Ninja生成器和构建工具。Ninja 跨平台、功能丰富、性能卓越,建议在所有平台上使用。使用其他生成器可能有效,但未获官方支持。

高级配置参数

Qt for Android 包含 Java 代码,使用javac编译器将其编译成*.jar文件。要为源代码和目标代码设置javac版本,请分别使用-android-javac-source-android-javac-target

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

要将 Qt 作为开发者构建版而不是前缀构建版构建,请使用以下内容代替-prefix 参数:

-developer-build

注意: 使用此配置参数时,并不需要安装 Qt 联编,Android 版 Qt 可直接在联编目录中使用。

Qt 配置选项》(Qt Configure Options)包含有关配置选项的更多信息。

构建

要构建 Qt,请运行以下命令:

cmake --build . --parallel

安装

对于前缀构建,要安装 Qt,请运行以下命令:

cmake --install .

在 Unix 上,如果您没有提供-prefix <install_path> configure 选项,安装将被放置在/usr/local/Qt-<version> 下,在这种情况下,您需要在安装命令中使用sudo

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