Skip to content

Migrate performance snippets - #372

Open
pengzhi-cb wants to merge 4 commits into
android:mainfrom
StellarElements:pquah/performance-snippets
Open

pengzhi-cb wants to merge 4 commits into
android:mainfrom
StellarElements:pquah/performance-snippets

Conversation

@pengzhi-cb

Copy link
Copy Markdown

Code snippets are for:

  1. (3 snippets) https://developer.android.com/topic/performance/appstartup/analysis-optimization
  2. (2 snippets) https://developer.android.com/topic/performance/appstartup/best-practices
  3. (1 snippet) https://developer.android.com/topic/performance/issues/launch-time
  4. (1 snippet) https://developer.android.com/topic/performance/baselineprofiles/measure-baselineprofile
  5. (1 snippet) https://developer.android.com/topic/performance/baselineprofiles/overview
  6. (1 snippet) https://developer.android.com/topic/performance/startupprofiles/dex-layout-optimizations

Total: 9 code snippets (6 unique Kotlin snippets and 0 XML snippets) across 6 pages.

List of modifications
  1. Analysis-optimization page: Replaced standalone ... placeholders with // ... comments and added braces { ... } around the if (BuildConfig.DEBUG) body in MyApplication.onCreate() (android_performance_appstartup_strict_mode in AnalysisOptimization.kt).
  2. Analysis-optimization page: Normalized indentation to 4 spaces and added spaces inside { mutableStateOf(false) } in android_compose_performance_conditional_composition and android_compose_performance_conditional_composition_effect (AnalysisOptimization.kt).
  3. Best-practices page: Consolidated duplicate conditional composition blocks with the Analysis-optimization page by sharing android_compose_performance_conditional_composition and android_compose_performance_conditional_composition_effect in AnalysisOptimization.kt, and normalized indentation from 3 spaces to 4 spaces.
  4. Launch-time page: Formatted when(activityState) to when (activityState) in MainActivity.onCreate() (android_performance_launch_time_fully_drawn_reporter in LaunchTime.kt).
  5. Measure-baselineprofile page: Replaced internal runner @RunWith(AndroidJUnit4ClassRunner::class) with @RunWith(AndroidJUnit4::class), fixed parameter name warmupIteration = 3 to warmupIterations = 3 in CompilationMode.Partial(...), and normalized spacing before the closing brace on line 76 (android_performance_baselineprofiles_measure_compilation_modes in MeasureBaselineProfile.kt).
  6. Dex-layout-optimizations page: Added the missing closing brace } for the uiAutomator block so the snippet compiles and formatted startIntent(...) across multiple lines (android_performance_startupprofiles_generator in DexLayoutOptimizations.kt).
Snippets not migrated
  1. Analysis-optimization page:
    1. None (all 3 code snippets on the guide page are migrated).
  2. Best-practices page:
    1. None (all 2 code snippets on the guide page are migrated).
  3. Launch-time page:
    1. None (all Kotlin code snippets on the guide page are migrated; lines 239–241, 266–268, 281–286, 291–298, and 339–341 are Logcat output and adb shell command examples).
  4. Measure-baselineprofile page:
    1. None (all code snippets on the guide page are migrated).
  5. Overview page:
    1. None (all code snippets on the guide page are migrated).
  6. Dex-layout-optimizations page:
    1. None (all code snippets on the guide page are migrated).

@pengzhi-cb
pengzhi-cb requested a review from a team as a code owner October 29, 2026 16:50

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new snippets module declares applicationId = "com.example.android.performance", while its three instrumented benchmark/profile tests launch com.example.snippets, com.example.macrobenchmark.target, and com.example.app. Since those packages are not provided here, connectedAndroidTest will discover tests that cannot run. Could the snippet-only tests be kept out of the runnable test suite, or be wired to a target app?

- UI Automator 2.4.0-alpha06 -> 2.4.1, Benchmark 1.4.1 -> 1.5.0,
  Activity 1.11.0 -> 1.13.0, Compose BOM 2025.11.00 -> 2026.06.01,
  Lifecycle 2.9.4 -> 2.10.0, Kotlin 2.2.21 -> 2.4.21, AGP 8.13.1 -> 8.13.2,
  Gradle 8.14.3 -> 8.14.7 (with the distribution checksum pinned).
  Newer Compose and Lifecycle releases need compileSdk 37 and AGP 9.
- Depend on lifecycle-runtime: lifecycle-runtime-ktx is empty since 2.8.0.
- UI Automator 2.4.1 has no startIntent(); the startup profile snippet now
  calls startActivityIntent(), which its comment already names.
- Sort the dependency and version entries alphabetically.
- End two snippet comments with a full stop.
@hamen

hamen commented Nov 18, 2026

Copy link
Copy Markdown

Thanks. These three classes are there so the snippets on the Baseline Profiles and startup profile pages compile. The package names come from those pages, and no target app for them exists in this repository, so they are not meant to run. The CI workflow I'm adding for snippets/ runs assembleAndroidTest to compile them and doesn't run connectedAndroidTest.

If you'd rather keep them out of the instrumented test APK, I can move them to a separate source set that only compiles. Which do you prefer?

The Baseline Profile and Macrobenchmark snippet classes sit in androidTest
so they compile. A comment outside each region says that in an app they
live in a separate com.android.test module.
@hamen

hamen commented Nov 18, 2026

Copy link
Copy Markdown

I pushed two commits on top of this PR. I can't edit the PR description from my account, so here are the changes it doesn't cover yet.

Visible changes to published code:

  • Dex-layout-optimizations page: startIntent(...) is now startActivityIntent(...), because UI Automator 2.4.1 has no startIntent(). The snippet's comment already names startActivityIntent (android_performance_startupprofiles_generator in DexLayoutOptimizations.kt).
  • Dex-layout-optimizations and Baseline Profiles overview pages: the comments "Launch directly into the NEWS_FEED using startActivityIntent" and "Find and click elements using the new DSL" now end with a full stop.

Other changes:

  • Dependencies: the project uses the latest stable versions that build with AGP 8.13 and compileSdk 36: UI Automator 2.4.1, Benchmark 1.5.0, Activity 1.13.0, Compose BOM 2026.06.01, Lifecycle 2.10.0, Kotlin 2.4.21, AGP 8.13.2 and Gradle 8.14.7. Compose BOM 2026.09.00 and Lifecycle 2.11.0 need compileSdk 37 and AGP 9. Gradle stays on 8.14.7 because Gradle 9.6 and later don't support AGP 8, and 9.5.1 doesn't have the fixes for the Gradle security advisories published in October and November (for example GHSA-3cfg-jpcr-gfp3), which 8.14.7 has.
  • lifecycle-runtime replaces lifecycle-runtime-ktx, which has been empty since 2.8.0.
  • The three classes in src/androidTest now have a comment, outside the snippet regions, saying that in an app they live in a separate com.android.test module.

@agammann

Copy link
Copy Markdown

Thanks for clarifying. I’d prefer the separate compile-only source set. Please keep all three snippets in the CI compile check and out of the instrumented test APK. That keeps the documentation examples checked without having connectedAndroidTest discover tests whose target apps aren’t in this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants