如何应对 Android 面试官 -> 启动如何进行优化(上)?玩转 Android StartUp

前言


在这里插入图片描述

本章主要围绕 App 的启动流程如何优化进行讲解;

将启动优化,首先要了解的就是 app 的启动流程,只有清晰并完善的了解了 启动流程 才能更好的进行优化;

App 启动流程

在将 AMS 的时候,其实已经讲解了 App 的启动流程,感兴趣的可以翻看下我之前的文章;

在这里插入图片描述

这里我们贴一张启动流程图;

整体流程就是当我们点击桌面图标启动某一个应用层的时候,首先会在 Launcher 进程中通过 ActivityManagerProxy 跨进程通信发送 startActivtity 并代理到 system_server(AMS) 进程, AMS 发现这个 Activity 所在的进程已经存在,则直接启动这个 Activity,(这就是所谓的热启动),如果不存在,则通知 Zygote 进程 fork 出一个进程给目标 App 使用,并通知 AMS,由 AMS 来启动目标 Activity;而启动目标 Activity 之前,先启动 Application,在 Application 启动之后才会启动 Activity;

整体可以分为三个大的阶段

  1. 点击桌面 Launcher 的应用图标,通过与 AMS 通信,启动应用的过程;
  2. 应用 Application 执行过程;
  3. 启动 MainActivity 执行过程;

所以这里的启动其实是有三种状态的:冷启动、温启动、热启动

启动方式

冷启动

冷启动是指应用从头开始启动:系统进程在冷启动后才创建应用进程。发生冷启动的情况包括应用自设备启动后或系统终止应用后首次启动

热启动

在热启动中,系统的所有工作就是将 Activity 带到前台。只要应用的所有 Activity 仍驻留在内存
中,应用就不必重复执行对象初始化、布局加载和绘制

温启动

包含了在冷启动期间发生的部分操作;同时,它的开销要比热启动高。有许多潜在状态可视为温启动。例如:

用户在退出应用后又重新启动应用。进程可能未被销毁,继续运行,但应用需要执行
onCreate() 从头开始重新创建 Activity;

系统将应用从内存中释放,然后用户又重新启动它。进程和 Activity 需要重启,但传递到 onCreate() 的已保存的实例 state bundle 对于完成此任务有一定助益;

启动时长统计

Displayed

app启动完成之后,ActivityManager 会打印一个 Displayed 展示启动时间;

adb 命令

adb shell am start -s -w 「packageName/ .activityName」

  • waitTime 总的耗时,包括前一个应用Activity pause的时间和新应用启动的时间
  • thisTime 表示一连串启动Activity的最后一个Activity的启
:-1: error: Set `android.useAndroidX=true` in the `gradle.properties` file and retry. The following AndroidX dependencies are detected: :debugRuntimeClasspath -> androidx.core:core:1.16.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.annotation:annotation:1.8.1 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.annotation:annotation:1.8.1 -> androidx.annotation:annotation-jvm:1.8.1 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.annotation:annotation-experimental:1.4.1 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.collection:collection:1.4.2 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.collection:collection:1.4.2 -> androidx.collection:collection-jvm:1.4.2 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.concurrent:concurrent-futures:1.1.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.core:core-viewtree:1.0.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.interpolator:interpolator:1.0.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.lifecycle:lifecycle-runtime:2.6.2 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.lifecycle:lifecycle-runtime:2.6.2 -> androidx.arch.core:core-common:2.2.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.lifecycle:lifecycle-runtime:2.6.2 -> androidx.arch.core:core-runtime:2.2.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.lifecycle:lifecycle-runtime:2.6.2 -> androidx.lifecycle:lifecycle-common:2.6.2 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.lifecycle:lifecycle-runtime:2.6.2 -> androidx.profileinstaller:profileinstaller:1.3.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.lifecycle:lifecycle-runtime:2.6.2 -> androidx.profileinstaller:profileinstaller:1.3.0 -> androidx.startup:startup-runtime:1.1.1 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.tracing:tracing:1.2.0 :debugRuntimeClasspath -> androidx.core:core:1.16.0 -> androidx.versionedparcelable:versionedparcelable:1.1.1 * 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 6s
最新发布
12-17
<think>我们遇到了在构建Qt 6.10.1的Android应用时,提示需要在gradle.properties中设置`android.useAndroidX=true`的问题。这个错误通常是因为项目依赖的库(包括Qt自身的Android支持库)使用了AndroidX,但是项目配置中没有启用AndroidX支持。 ### 解决方案步骤 #### 1. 在项目中添加`gradle.properties`文件 如果项目中没有`gradle.properties`文件,我们需要在Android构建目录中创建它。通常,Qt构建Android APK时,会在`android-build`目录(或类似目录)中生成Gradle项目。我们需要在该目录下创建或修改`gradle.properties`文件。 路径示例: ``` D:/Demo/record251215/android-build/gradle.properties ``` 在该文件中添加以下内容: ```properties # 启用AndroidX支持 android.useAndroidX=true # 启用Jetifier(如果需要转换旧的支持库) android.enableJetifier=true ``` #### 2. 修改Qt项目配置(可选) 如果上述方法不起作用,可能是因为Qt生成的构建文件覆盖了我们的配置。我们可以尝试在Qt项目文件中(`.pro`或`.qmake.conf`)添加配置,让Qt在生成Gradle项目时自动包含这些属性。 在项目文件(`.pro`)中添加: ```qmake # 告诉Qt在生成Android项目时添加这些属性到gradle.properties android { ANDROID_GRADLE_EXTRA_PROPERTIES += android.useAndroidX=true \ android.enableJetifier=true } ``` 或者,在`android`构建目录下的`gradle.properties`模板文件中添加。但更直接的方式是覆盖Qt生成的`gradle.properties`文件。 #### 3. 清理并重新构建 在修改了配置后,需要清理项目并重新构建: - 删除之前构建生成的`android-build`目录(或整个构建目录) - 在Qt Creator中执行`清理项目`,然后重新构建 #### 4. 验证Gradle配置 如果问题仍然存在,检查项目中的其他Gradle配置: - 确保`build.gradle`文件中使用的依赖库版本兼容AndroidX。例如,如果使用了旧的支持库(如`com.android.support`),需要替换为对应的AndroidX库。 - 在`android/build.gradle`文件中,检查依赖项是否使用AndroidX版本。例如: ```gradle dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.core:core-ktx:1.10.1' // 其他依赖 } ``` #### 5. 使用Qt 6.10.1的已知解决方案 根据Qt官方文档,从Qt 5.15开始,Qt for Android就要求使用AndroidX。因此,在Qt 6.10.1中必须设置`android.useAndroidX=true`。如果Qt没有自动设置,我们需要手动添加。 另外,确保使用的Android SDK版本支持AndroidX。建议使用较新的Android SDK(如API 33或34)和最新的构建工具。 ### 错误排查 如果以上步骤仍不能解决问题,可以检查构建日志(添加`--verbose`参数)以获取更多信息。例如,运行: ```bash androiddeployqt --input ... --output ... --gradle --verbose ``` 在日志中搜索与AndroidX相关的错误,例如: ``` > Failed to resolve: androidx.appcompat:appcompat:1.6.1 ``` 这表示需要检查网络连接或代理设置,或者可能需要更新仓库。 ### 注意事项 - **路径问题**:确保`gradle.properties`文件放置在正确的目录(即Gradle项目的根目录,通常是`android-build`)。 - **文件编码**:使用UTF-8编码保存`gradle.properties`文件,避免特殊字符问题。 - **Qt版本兼容性**:确认使用的Qt版本完全支持AndroidX。Qt 6.10.1应该已经内置支持,但可能需要手动设置属性。 ### 流程图 ```mermaid graph TD A[构建失败:需要设置useAndroidX] --> B[在android-build/gradle.properties中添加配置] B --> C{问题解决?} C -->|是| D[成功构建] C -->|否| E[在.pro文件中添加ANDROID_GRADLE_EXTRA_PROPERTIES] E --> F[清理并重新构建] F --> G{问题解决?} G -->|是| D G -->|否| H[检查依赖库是否兼容AndroidX] H --> I[更新依赖库到AndroidX版本] I --> F ``` ### 相关问题 1. 如何在Qt for Android项目中配置AndroidX? 2. Qt 6.10.1构建Android应用时出现依赖冲突怎么办? 3. 如何解决Qt Android构建中的`Program type already present`错误? 4. 为什么Qt Android应用需要设置`android.useAndroidX=true`? 通过以上步骤,应该能够解决构建APK时提示设置`android.useAndroidX=true`的问题。如果问题依旧,请检查构建环境(如Android SDK、Qt版本)是否配置正确[^2][^3]。 </think> ### 解决Qt 6.10.1构建Android APK时`android.useAndroidX=true`错误的完整指南 当Qt 6.10.1为Android armeabi-v7a构建APK时提示"Set `android.useAndroidX=true` in the `gradle.properties`"错误,这是因为项目需要迁移到AndroidX库而配置未更新。以下是逐步解决方案: #### 一、核心修复步骤 1. **在项目中创建/修改gradle.properties文件** - 在项目根目录的`android`文件夹内创建`gradle.properties`文件: ``` D:/Demo/record251215/android/gradle.properties ``` - 文件内容必须包含: ```properties android.useAndroidX=true android.enableJetifier=true ``` 2. **更新build.gradle依赖**(关键步骤) 在`android/build.gradle`中添加AndroidX依赖: ```gradle dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.core:core-ktx:1.10.1' } ``` 3. **配置AndroidManifest.xml** 在`<application>`标签内添加AndroidX配置: ```xml <application ... android:usesCleartextTraffic="true" tools:replace="android:usesCleartextTraffic"> <!-- 添加AndroidX兼容配置 --> <meta-data android:name="androidx.lifecycle.Version" android:value="2.6.1" /> </application> ``` #### 二、Qt项目配置优化 1. **在.pro文件中声明AndroidX支持** ```qmake android { # 强制启用AndroidX ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android ANDROID_EXTRA_LIBS += \ $$[QT_INSTALL_LIBS]/libQt6AndroidExtras_armeabi-v7a.so } ``` 2. **清理并重建项目** ```bash # 删除旧构建 rm -rf android-build # 重新生成qmake qmake make clean ``` #### 三、Gradle环境验证 1. **检查Gradle版本兼容性** - 在`android/gradle/wrapper/gradle-wrapper.properties`中确保版本≥7.0: ```properties distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip ``` 2. **验证本地gradle.properties位置** ```mermaid graph LR A[项目根目录] --> B[android文件夹] B --> C[gradle.properties] C --> D[android.useAndroidX=true] ``` #### 四、高级故障排除 若仍报错,尝试: 1. **强制刷新Gradle依赖** ```bash cd android-build ./gradlew cleanBuildCache --refresh-dependencies ``` 2. **在Qt Creator中添加全局属性** - 打开 *项目设置 > 构建环境* - 添加变量: ``` ANDROID_GRADLE_EXTRA_PROPERTIES = android.useAndroidX=true ``` 3. **处理多模块冲突** 在`android/build.gradle`中添加: ```gradle configurations.all { resolutionStrategy { force 'androidx.core:core:1.10.1' force 'androidx.appcompat:appcompat:1.6.1' } } ``` #### 五、错误原因分析 该错误源于: 1. Qt 6默认使用AndroidX,但旧项目未迁移 2. 第三方库(如Firebase)要求AndroidX 3. 兼容层未启用(需`android.enableJetifier=true`) > **关键提示**:Qt 6.10.1要求AndroidX,不支持旧支持库。确保所有依赖库(如`com.android.support`)已替换为AndroidX等价物[^2]。 ### 相关问题 1. 如何将Qt Android项目从旧支持库迁移到AndroidX? 2. Qt 6.10.1构建Android应用时如何解决`Program type already present`错误? 3. 为什么AndroidX在Qt Android开发中是必需的? 4. 如何解决Qt Android构建中的`INSTALL_PARSE_FAILED_MANIFEST_MALFORMED`错误?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值