在 Android 部署应用程序

本文描述把 Qt 应用程序部署到 Android 设备所需的技术步骤。

Android 应用程序绑定

Applications on Android can be packaged in two ways; either as an Application Package (APK) or Android App Bundle (AAB). Both are a type of ZIP file that follows a predefined directory structure. The difference between the two is that APK files can be deployed and executed on a device, whereas AAB is intended to be interpreted by the Google Play store and is used to generate APK files for different device architectures and form factors.

For testing the application locally, the APK format is appropriate, as this can be installed and run directly on the device. For distribution to the Google Play store, it is recommended to use AAB instead, which has a similar layout. The added convenience of AAB is that you can include all target ABIs in the same bundle without increasing the size of the actual package downloaded by your users. When using AAB, the Google Play store generates optimized APK packages for the devices issuing the download request and automatically signs them with your publisher key. For more information, see 发布到 Google Play .

For more information on the AAB format, see the Android App Bundles .

在任何情况下,应用程序捆绑都是从特定目录结构生成的,包含 shared libraries of your project and Qt's dependencies needed by your application. In addition, any assets, resources, jar files or project Java code is compiled and included.

生成应用程序捆绑

It is recommended to use Qt Creator to deploy Qt for Android apps . Otherwise, the same can be done through the command line with the help of CMake or qmake. For more information, see 从命令行构建 Qt for Android 工程 .

The packaging and deployment process is handled by CMake or qmake which, under the hood, use the androiddeployqt tool to manage the specifics of building and deploying an Android app. Qt Creator also uses the same tool.

Extending Qt with Android Facilities

By default, Qt for Android does most of the heavy lifting to get an Android app up and running, having the most useful APIs available directly from Qt, or using QJniObject to invoke not readily available APIs. The same is valid for CMake, which handles the various build and deployment cases. However, in some other cases, it might be required to have the full power of native Android facilities, such as writing Java/kotlin code or using Android resource management. Qt allows that by allowing the user to extend the set of templates.

The default templates used by Qt are found under the Qt for Android install path, for example, under ~/Qt/<version>/android_<abi>/src/android/templates for Unix. To use those templates, it's possible to have Qt Creator copy them to your project, see Qt Creator:编辑清单文件 . Or you can manually copy them over to your project source under a android sub-directory. Then make sure to define the following property in your CMakeLists.txt :

set_property(TARGET target_name PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
          "${CMAKE_CURRENT_SOURCE_DIR}/android")
					

Or for qmake in your pro 文件:

android: ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
					

注意: When using Qt Creator, files under this path are by default visible under the project files if CMake is used. To achieve the same behavior with qmake, add those file paths manually to your project using DISTFILES .

The build process copies the templates to the build directory <build_path>/android-build from your project or from the default templates if the project didn't set QT_ANDROID_PACKAGE_SOURCE_DIR . After that, the directory <build_path>/android-build acts as the packaging directory. The application bundle is created from there using Gradle.

Now, let's go through the different parts that the user can work with after extending the default templates.

AndroidManifest.xml

The AndroidManifest.xml file gives detailed meta-information about your application. This information is used to customize your application bundle, and it's used by the device to decide which features to enable, such as the default orientation of the application. In addition, it's used by the Google Play Store for information on the version code, device support, package name, and lots more. The Android Manifest is also used to define Android 服务 and custom Android Activities .

更多信息有关 AndroidManifest.xml ,见 Android Manifest file documentation , and Qt Creator's page for Qt Creator:编辑清单文件 .

Gradle Files

Gradle is used to build Android packages. Qt includes two sets of Gradle related files:

  • Gradle wrapper, which is used to download a specific version of Gradle itself, and the build scripts that are used to invoke the Gralde build. These files come with Qt under for example ~/Qt/<version>/android_<abi>/src/3rdparty/gradle .

    注意: Usually, using the same Gradle version that Qt comes with is recommended. However, if you wish to use a different Gradle version, you can modify the Gradle wrapper gradle-wrapper.properties and set it to the Gradle version you want to use.

  • The Gradle configuration file build.gradle , which is under the Android Templates . This file is required by Gradle and can be used to customize the build. It can be used to set the build target or minimum API or add library dependencies. It can also be used to set the Android Gradle plugin , which is a required Gradle dependency for building Android apps. An example of this is:
    buildscript {
    ...
    dependencies {
    classpath 'com.android.tools.build:gradle:7.4.1'
    }
    }
    						

    更多信息,见 Android: Build Configuration Files .

Java/Kotlin 代码

To include any Java/Kotlin code to target some APIs that Qt doesn't cover or for some other reason, place any code under the path <QT_ANDROID_PACKAGE_SOURCE_DIR>/src/ . For example, you can call Java methods from within Qt code. For an example, see Qt Android Notifier Example .

资源

Android allows the addition of resource files such as icons, images, strings, colors, and so on. Those resources can be referenced directly from Java/Kotlin code or the manifest file. Any such files can be added to your project under <QT_ANDROID_PACKAGE_SOURCE_DIR>/res/ . For example, app icons can be added under res/drawable/icon.png .

更多信息,见 Android:App 资源概述 .

Qt 内部资源

By default, Qt packages a few resources that are needed for the apps to run properly. For example, on Unix, these resources are found under ~/Qt/<version>/android_<abi>/src/android/templates/res .

strings.xml

This file can be found at res/values/strings.xml . This file contains strings the Qt library uses to reference message translations for various languages.

libs.xml

This file can be found at res/values/libs.xml . It is used purely to manage deployment information of the Qt libraries, and it's not recommended to be manually modified.

Android 资产

For more information on managing Android assets, see 添加资源 .

Android 库

For more information on using third-party libraries with your Qt project, see Including a Library to an Android Project .

移植到 Android 为 Android 添加 OpenSSL 支持