其他
Android 启动优化方案调研!
关注「技术最TOP」
,早上8:40不见不散!
作者:汐颜染瞳べ
https://juejin.cn/user/2365804752418232/posts
TotalTime:应用的启动时间,包括创建进程+Application初始化+Activity初始化到界面显示。 WaitTime:一般比TotalTime大点,是AMS启动Activity的总耗时。 Android 5.0以下没有WaitTime,所以我们只需要关注TotalTime即可。
Warning: Activity not started, intent has been delivered to currently running top-most instance.
Status: ok
LaunchState: UNKNOWN (0)
Activity: ***/***.MainActivity
TotalTime: 785
WaitTime: 787
Complete
TraceCompat.beginSection("initAnalyzeAync");
PbnAnalyze.setEnableLog(BuildConfig.DEBUG);
PbnAnalyze.setEnableAnalyzeEvent(true);
initAnalyze();
TraceCompat.endSection();
}
先执行入度为0的任务 让依赖于它的任务入度-1(countDownLatch.countDown()),直到入度为0,执行该任务 重复以上两个步骤
implementation("androidx.profileinstaller:profileinstaller:1.2.0-beta01")
}
@RunWith(AndroidJUnit4::class)
class BaselineProfileGenerator {
@get:Rule val baselineProfileRule = BaselineProfileRule()
@Test
fun startup() =
baselineProfileRule.collectBaselineProfile(packageName = "com.example.app") {
pressHome()
// This block defines the app's critical user journey. Here we are interested in
// optimizing for app startup. But you can also navigate and scroll
// through your most important UI.
startActivityAndWait()
}
}
从终端运行adb root命令以确保 adb 守护程序以 root 权限运行。 运行测试并等待其完成。 在 logcat 中找到生成的配置文件位置。搜索日志标签 Benchmark。
ls /storage/emulated/0/Android/media/com.example.app
SampleStartupBenchmark_startup-baseline-prof.txt
HSPLandroidx/compose/runtime/ComposerImpl;->updatedNodeCount(I)I
HLandroidx/compose/runtime/ComposerImpl;->validateNodeExpected()V
PLandroidx/compose/runtime/CompositionImpl;->applyChanges()V
HLandroidx/compose/runtime/ComposerKt;->findLocation(Ljava/util/List;I)I
Landroidx/compose/runtime/ComposerImpl;
规则语法
这些规则采用以下两种形式之一来定位方法或类:
class BaselineProfileBenchmark {
@get:Rule
val benchmarkRule = MacrobenchmarkRule()
@Test
fun startupNoCompilation() {
startup(CompilationMode.None())
}
@Test
fun startupBaselineProfile() {
startup(CompilationMode.Partial(
baselineProfileMode = BaselineProfileMode.Require
))
}
private fun startup(compilationMode: CompilationMode) {
benchmarkRule.measureRepeated(
packageName = "com.example.app",
metrics = listOf(StartupTimingMetric()),
iterations = 10,
startupMode = StartupMode.COLD,
compilationMode = compilationMode
) { // this = MacrobenchmarkScope
pressHome()
startActivityAndWait()
}
}
}
# Force Stop App
adb shell am force-stop $PACKAGE_NAME
# Reset compiled state
adb shell cmd package compile --reset $PACKAGE_NAME
# Measure App startup
# This corresponds to `Time to initial display` metric
# For additional info https://developer.android.com/topic/performance/vitals/launch-time#time-initial
adb shell am start-activity -W -n $PACKAGE_NAME/.ExampleActivity \
| grep "TotalTime"
unzip release.apk
# Create a ZIP archive
# Note: The name should match the name of the APK
# Note: Copy baseline.prof{m} and rename it to primary.prof{m}
cp assets/dexopt/baseline.prof primary.prof
cp assets/dexopt/baseline.profm primary.profm
# Create an archive
zip -r release.dm primary.prof primary.profm
# Confirm that release.dm only contains the two profile files:
unzip -l release.dm
# Archive: release.dm
# Length Date Time Name
# --------- ---------- ----- ----
# 3885 1980-12-31 17:01 primary.prof
# 1024 1980-12-31 17:01 primary.profm
# --------- -------
# 2 files
# Install APK + Profile together
adb install-multiple release.apk release.dm
adb shell dumpsys package dexopt | grep -A 1 $PACKAGE_NAME
path: /data/app/~~YvNxUxuP2e5xA6EGtM5i9A==/com.example.app-zQ0tkJN8tDrEZXTlrDUSBg==/base.apk
arm64: [status=speed-profile] [reason=install-dm]
adb shell am force-stop $PACKAGE_NAME
# Measure App startup
adb shell am start-activity -W -n $PACKAGE_NAME/.ExampleActivity \
| grep "TotalTime"
Android 5 到 Android 6(API 级别 21 和 23)已经在安装时 AOT 编译 APK。 可调试的应用程序 永远不会被 AOT 编译以帮助进行故障排除。 规则文件必须命名baseline-prof.txt并放置在主源集的根目录中(它应该是文件的同级 AndroidManifest.xml文件)。 7.1.0-alpha05仅当您使用 Android Gradle 插件或更高版本 (Android Studio Bumblebee Canary 5)时才会使用这些文件 。 Bazel 目前不支持读取基线配置文件并将其合并到 APK 中。 基线配置文件压缩后的大小不能超过 1.5 MB。因此,库和应用程序应该努力定义一小组能够最大化影响的配置文件规则。 编译过多应用程序的广泛规则可能会由于磁盘访问增加而减慢启动速度。您应该测试基线配置文件的性能。
---END---