一、Qt for Android的配置方法:
- 安装正确版本的Java JDK(经测试,qt 6.7.3版本对应于Java JDK 17),并在环境变量中进行添加,Windows控制台使用命令java --version可验证环境变量是否添加成功并查看安装的Java版本。
- 在Qt creator中进行配置:
2.1 配置JDK位置后,点击设置SDK可以自动下载对应的Android-SDK套件,按照系统引导一直点确定即可。
下载好以后点击SDK管理器,搜索Android emulator勾选应用以安装对应的安卓模拟器。
2.2 点击下载openSSL跳转GitHub仓库,download对应的zip文件,解压到自定义位置,然后回到Qt creator进行位置添加即可。
2.3 在设备中添加安卓设备,架构取决于安装的SDK套件,如果没有可以回到SDK管理器进行下载,我这里选择x86_64,后续编译也是使用x86_64,设备名称不限,其余默认即可。(目标api的选择影响后续的配置)
2.4 添加了安卓模拟器后可以点击启动AVD进行查看,如果无法打开则:
控制面板-卸载程序-启动或关闭Windows功能-打开Hyper-V-重启电脑
打开后模拟器后应该如下图所示:
如下图,在构建套件界面可以看到安卓构建套件已安装完毕。
新建安卓项目,运行即可。(有的人在新建项目进行运行后会提醒需要再下载一个套件,详情可参考网上经验,笔者由于安装过程中没遇到该问题,因此不在此赘述)
二、 遇到的问题及其解决方法:
1. 问题一:
Generating Android Package Input file: E:/code/Qt/AndroidPhone/build/Qt_6_5_3_Clang_x86_64-Debug/android-AndroidPhone-deployment-settings.json Output directory: E:/code/Qt/AndroidPhone/build/Qt_6_5_3_Clang_x86_64-Debug/android-build/ Application binary: AndroidPhone Android build platform: android-29 Install to device: No FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project ‘android-build’. > Could not open cp_proj generic class cache for build file ‘E:\code\Qt\AndroidPhone\build\Qt_6_5_3_Clang_x86_64-Debug\android-build\build.gradle’ (C:\Users.gradle\caches\8.0\scripts\et9x1nmdyrg8ikokvipr8cqkb). > BUG! exception in phase ‘semantic analysis’ in source unit ‘BuildScript’ Unsupported class file major version 67 * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 639ms Building the android package failed! – For more information, run this command with --verbose. Skipping createRCC 16:44:27: 进程"D:\Qt\6.5.3\mingw_64\bin\androiddeployqt.exe"退出,退出代码 14 .
Error while building/deploying project AndroidPhone (kit: 安卓 Qt 6.5.3 Clang x86_64) When executing step “构建安卓 APK”
问题分析:Java JDK版本不匹配
解决方法:安装了高版本的Java JDK,将Java JDK 23修改为Java JDK 17
2. 问题二:
Generating Android Package Input file: E:/code/Qt/AndroidTest/build/Qt_6_5_3_Clang_x86_64-Debug/android-AndroidTest-deployment-settings.json Output directory: E:/code/Qt/AndroidTest/build/Qt_6_5_3_Clang_x86_64-Debug/android-build/ Application binary: AndroidTest Android build platform: android-29 Install to device: No Skipping createRCC Starting a Gradle Daemon, 2 incompatible and 3 stopped Daemons could not be reused, use --status for details > Task :preBuild UP-TO-DATE > Task :preDebugBuild UP-TO-DATE > Task :mergeDebugNativeDebugMetadata NO-SOURCE > Task :compileDebugAidl NO-SOURCE > Task :compileDebugRenderscript NO-SOURCE > Task :generateDebugBuildConfig > Task :javaPreCompileDebug > Task :checkDebugAarMetadata > Task :generateDebugResValues > Task :mapDebugSourceSetPaths > Task :generateDebugResources > Task :createDebugCompatibleScreenManifests > Task :extractDeepLinksDebug > Task :processDebugMainManifest package=“org.qtproject.example.AndroidTest” found in source AndroidManifest.xml: E:\code\Qt\AndroidTest\build\Qt_6_5_3_Clang_x86_64-Debug\android-build\AndroidManifest.xml. Setting the namespace via a source AndroidManifest.xml’s package attribute is deprecated. Please instead set the namespace (or testNamespace) in the module’s build.gradle file, as described here: https://developer.android.com/studio/build/configure-app-module#set-namespace This migration can be done automatically using the AGP Upgrade Assistant, please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information. > Task :mergeDebugResources > Task :processDebugManifest > Task :mergeDebugShaders > Task :compileDebugShaders NO-SOURCE > Task :generateDebugAssets UP-TO-DATE > Task :mergeDebugAssets > Task :compressDebugAssets > Task :processDebugJavaRes NO-SOURCE > Task :checkDebugDuplicateClasses > Task :mergeDebugJavaResource > Task :mergeLibDexDebug > Task :mergeDebugJniLibFolders > Task :desugarDebugFileDependencies > Task :mergeDebugNativeLibs > Task :validateSigningDebug > Task :writeDebugAppMetadata > Task :writeDebugSigningConfigVersions > Task :processDebugManifestForPackage > Task :mergeExtDexDebug > Task :stripDebugDebugSymbols > Task :processDebugResources FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:processDebugResources’. > A failure occurred while executing com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask$TaskAction > Android resource linking failed E:\code\Qt\AndroidTest\build\Qt_6_5_3_Clang_x86_64-Debug\android-build\build\intermediates\packaged_manifests\debug\AndroidManifest.xml:22: error: attribute android:allowNativeHeapPointerTagging not found. error: failed processing manifest. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 1m 5s 26 actionable tasks: 26 executed Building the android package failed! – For more information, run this command with --verbose. 17:33:01: 进程"D:\Qt\6.5.3\mingw_64\bin\androiddeployqt.exe"退出,退出代码 14 。
Error while building/deploying project AndroidTest (kit: 安卓 Qt 6.5.3 Clang x86_64) When executing step “构建安卓 APK”
问题分析:属性 android:allowNativeHeapPointerTagging 未找到
解决方法:打开 D:\Qt\6.5.3\android_x86_64\src\android\templates\build.gradle 文件,检查并修改以下字段:
android {
compileSdkVersion 31 // 确保这里是至少 API 31
defaultConfig {
minSdkVersion 21
targetSdkVersion 31 // 确保目标 SDK 是 31 或更高
}
}