如何应对 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的启
Configuration `:app:debugRuntimeClasspath` contains AndroidX dependencies, but the `android.useAndroidX` property is not enabled, which may cause runtime issues. Set `android.useAndroidX=true` in the `gradle.properties` file and retry. The following AndroidX dependencies are detected: :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.activity:activity:1.6.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.annotation:annotation:1.3.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 -> androidx.collection:collection:1.1.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.core:core:1.9.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.annotation:annotation-experimental:1.3.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.core:core:1.9.0 -> androidx.concurrent:concurrent-futures:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.lifecycle:lifecycle-runtime:2.5.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.lifecycle:lifecycle-runtime:2.5.1 -> androidx.arch.core:core-common:2.1.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.lifecycle:lifecycle-runtime:2.5.1 -> androidx.arch.core:core-runtime:2.1.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.lifecycle:lifecycle-runtime:2.5.1 -> androidx.lifecycle:lifecycle-common:2.5.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.core:core:1.9.0 -> androidx.versionedparcelable:versionedparcelable:1.1.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.core:core-ktx:1.9.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.lifecycle:lifecycle-viewmodel:2.5.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.fragment:fragment:1.3.6 -> androidx.lifecycle:lifecycle-viewmodel-savedstate:2.5.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.fragment:fragment:1.3.6 -> androidx.lifecycle:lifecycle-livedata-core:2.5.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.savedstate:savedstate:1.2.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.activity:activity:1.6.0 -> androidx.tracing:tracing:1.0.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.vectordrawable:vectordrawable:1.1.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 -> androidx.vectordrawable:vectordrawable-animated:1.1.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 -> androidx.vectordrawable:vectordrawable-animated:1.1.0 -> androidx.interpolator:interpolator:1.0.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.cursoradapter:cursoradapter:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.drawerlayout:drawerlayout:1.1.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.drawerlayout:drawerlayout:1.1.1 -> androidx.customview:customview:1.1.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2:1.2.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2:1.2.0 -> androidx.lifecycle:lifecycle-process:2.4.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2:1.2.0 -> androidx.startup:startup-runtime:1.1.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2-views-helper:1.2.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.fragment:fragment:1.3.6 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.fragment:fragment:1.3.6 -> androidx.viewpager:viewpager:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.fragment:fragment:1.3.6 -> androidx.loader:loader:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.fragment:fragment:1.3.6 -> androidx.loader:loader:1.0.0 -> androidx.lifecycle:lifecycle-livedata:2.0.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.resourceinspection:resourceinspection-annotation:1.0.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.cardview:cardview:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.coordinatorlayout:coordinatorlayout:1.1.0 :app:debugRuntimeClasspath -> androidx.constraintlayout:constraintlayout:2.1.4 :app:debugRuntimeClasspath -> androidx.constraintlayout:constraintlayout:2.1.4 -> androidx.constraintlayout:constraintlayout-core:1.0.4 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.documentfile:documentfile:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.localbroadcastmanager:localbroadcastmanager:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.print:print:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.recyclerview:recyclerview:1.1.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.transition:transition:1.2.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.9.0 -> androidx.viewpager2:viewpager2:1.0.0
08-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值