Android App启动时间

本文深入解析了Android应用的三种启动方式:冷启动、热启动和温启动,并详细介绍了冷启动的具体流程,包括system tasks和app process tasks。文章还提供了推荐的启动时间指标,指导开发者如何通过日志和adb shell命令来获取启动时间,最后探讨了启动时间瓶颈的分析方法,如使用Android Studio的CPU Profiler和System Tracing。

1 启动方式

cold startup
warm startup
hot startup

2 cold startup过程

system tasks:
在后台加载和launch app
当后台launch app结束后立刻在前台屏幕上显示出一个空白window
后台创建app进程

app process tasks:
创建app对象
launch主线程(UI线程)
创建MainActivity
inflate views
在屏幕上布局
在屏幕上绘制

绘制完成后,后台使用这个MainActivity替换屏幕上的window,此时用户可以和app交互。

windowDisablePreview theme可以去掉preview window

A visual representation of the important parts of a cold application launch.

3 启动时间指标

推荐时间(From Android vitals)
Cold startup takes 5 seconds or longer.
Warm startup takes 2 seconds or longer.
Hot startup takes 1.5 seconds or longer.

4 获取启动时间

通过log搜索启动时间

09-14 18:26:19.883  1393  1425 I ActivityManager: Displayed com.ezia.coreservices.testcarservice/.MainActivity: +1s18ms

若MainActivity与总启动时间之间存在差异时,还会显示总时间度量

09-14 18:25:32.708  1393  1425 I ActivityManager: Displayed com.maezia.sevco.syshmicommgr.demo/.MainActivity: +1s801ms (total +2m24s378ms)

通过adb shell命令打印启动时间

adb shell am start -S -W
com.example.app/.MainActivity
-c android.intent.category.LAUNCHER
-a android.intent.action.MAIN

5 分析启动时间瓶颈
Android Studio CPU Profiler
System tracing

using a dependency injection framework like Hilt that creates objects and dependencies when they are injected for the first time
If your app uses content providers to initialize app components at startup, consider using the App Startup library instead

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值