You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算androidx.startup库的初始化耗时?

如何获取androidx.startup的初始化耗时

方法一:手动添加日志统计

如果是你自己实现的Initializer,可以直接在初始化逻辑前后插入时间戳计算耗时:

class CustomInitializer : Initializer<YourComponent> {
    override fun create(context: Context): YourComponent {
        val start = System.currentTimeMillis()
        // 执行你的初始化逻辑
        val component = YourComponent()
        val cost = System.currentTimeMillis() - start
        Log.d("StartupCost", "CustomInitializer 初始化耗时: $cost ms")
        return component
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        return emptyList()
    }
}

如果是第三方库的Initializer无法修改源码,可开启App Startup的verbose日志:在设备开发者选项中开启“Enable verbose logging”,然后过滤日志标签Startup,日志会包含每个Initializer的启动、完成时间节点,自行计算时间差即可。

方法二:使用Android Studio内置工具

Android Studio Arctic Fox及以上版本提供App Startup Inspector工具:

  • 打开Android Studio的Profiler面板(点击工具栏Profile按钮)
  • 连接设备并启动应用
  • 在Profiler导航栏选择App Startup标签
  • 面板会自动展示所有通过App Startup初始化的组件,包括每个组件的耗时、依赖关系和启动顺序,无需额外编码。

方法三:用基准测试框架批量统计

若需要更精准的性能数据或批量测试,可使用Jetpack Macrobenchmark:

  • 创建基准测试模块,编写启动测试用例
  • 在测试中监听应用启动阶段的Startup相关事件,或直接测量App Startup在启动总耗时中的占比
  • 运行测试后会生成包含详细耗时数据的报告。

内容的提问来源于stack exchange,提问作者PangoSea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 11:50:24