# Qt for Android - 从源代码构建

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.

## Building on Linux

### Prepare the Build Environment

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

Then, you need the following tools:

## 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 构建 Qt 源 .

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

## 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 文件夹。

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


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


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

cmake --build . --parallel


Also, the following is possible instead:

ninja -j$(nproc)  注意： 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  命令。 ## 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: Then set the respective environment variables from the 环境变量 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\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 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. 注意： JDK 11 or earlier must be used to properly build Qt for Android. 注意： 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> -android-ndk-host windows-x86_64 -qt-host-path <path_to_host_qt_installation> -android-abis arm64-v8a  Qt 配置选项 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 . 注意： For more information about building Qt 6 with CMake, see 构建采用 CMake . To build the configured Qt for Android code, run the following: cmake --build . --parallel  Also, the following is possible instead: ninja -j<N>  注意： <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. 注意： 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.