Visitar URL original
Add LiteRT CompiledModel snippets for the litert-runtime skill by smilingday · Pull Request #1186 · android/snippets · GitHub
Skip to content

Add LiteRT CompiledModel snippets for the litert-runtime skill - #1186

Open
smilingday wants to merge 2 commits into
android:mainfrom
smilingday:litert-runtime-skill-snippets
Open

smilingday wants to merge 2 commits into
android:mainfrom
smilingday:litert-runtime-skill-snippets

Conversation

@smilingday

@smilingday smilingday commented Oct 16, 2026 •

Copy link
Copy Markdown

Add LiteRT CompiledModel snippets for the litert-runtime skill

Summary

Adds compilable Kotlin snippets for the LiteRT CompiledModel API
(com.google.ai.edge.litert:litert / litert-gpu 2.3.0) used by the
developer.android.com Android agent skill litert-runtime
("Build an Android app with the LiteRT CompiledModel API",
/agents/skills/device-ai/litert/litert-runtime). The skill currently inlines
this code; once this PR is merged the DAC page can include these regions instead
of hardcoded blocks.

  • New file: ai/src/main/java/com/example/snippets/ai/LiteRtRuntimeSkillSnippets.kt
    (package com.example.snippets.ai).
  • gradle/libs.versions.toml: new litert = "2.3.0" version and litert,
    litert-gpu library entries.
  • ai/build.gradle.kts: implementation(libs.litert) and
    implementation(libs.litert.gpu) (wrapped in a region), plus the existing
    catalog entries androidx-lifecycle-runtime-compose and
    androidx-lifecycle-viewModelCompose for collectAsStateWithLifecycle and
    viewModel().

Region tags

Region tag Skill section it backs
android_litert_runtime_skill_imports Imports for all regions below, so the skill can show them next to the code
android_litert_runtime_skill_dependencies (ai/build.gradle.kts) Prerequisites / Step 1: Configure Gradle (dependency lines)
android_litert_runtime_skill_file_path Step 1: load an AI pack or downloaded model with CompiledModel.create(filePath, CompiledModel.Options(accelerator), environment)
android_litert_runtime_skill_classifier Step 2: Implement the inference helper class (Classifier: CompiledModel, TensorBuffer reuse, warm-up in init, close order)
android_litert_runtime_skill_viewmodel Step 3: Wire the ViewModel (UiState, MainViewModel, single-thread executor, GPU to CPU fallback on LiteRtException)
android_litert_runtime_skill_screen Step 3: Compose screen (MainScreen)
android_litert_runtime_skill_preprocess Step 4: Preprocess inputs (preprocess)

Notes

  • Code logic and identifiers match the skill verbatim. The only addition is a
    small createCompiledModelFromFile(filePath, accelerator, environment)
    function so the Step 1 on-disk CompiledModel.create(filePath, ...) variant
    has a compilable, self-contained home; callers pass the process-wide
    Environment.
  • The skill's minSdk = 24 Gradle block is intentionally not ported: this
    repo's minSdk is 36, so that block stays inline in the skill.
  • No model asset is bundled; the snippets are compile-only (the ai app does
    not reference these classes at runtime).
  • Dependency impact: no existing ai dependency changes version, and none is
    removed (debugRuntimeClasspath diffed against main). The new entries come
    only from LiteRT: litert, litert-gpu, litert-api, plus the transitive
    dependencies of litert-api 2.3.0 for loading models from Play AI packs
    (com.google.android.play:ai-delivery 0.1.1-alpha01 and its
    asset-delivery, core-common, work-runtime, room-*, and sqlite*).
    This PR doesn't add those directly.

Verification

  • ./gradlew :ai:assembleDebug — BUILD SUCCESSFUL (JDK 21, AGP from the repo,
    compileSdk 37).
  • ./gradlew :ai:spotlessApply :ai:spotlessKotlinCheck :ai:assembleDebug —
    BUILD SUCCESSFUL.
  • ./gradlew :ai:clean :ai:assembleDebug :ai:testDebugUnitTest --rerun-tasks —
    BUILD SUCCESSFUL; recompiles every existing ai snippet, and
    checkDebugDuplicateClasses / mergeDebugNativeLibs pass (no duplicate
    classes or native libraries).

@smilingday
smilingday requested a review from a team as a code owner October 16, 2026 03:39
@snippet-bot

snippet-bot Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 7 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Adds LiteRtRuntimeSkillSnippets.kt to the ai module with region-tagged snippets backing the developer.android.com litert-runtime Android agent skill: imports, the Classifier helper (CompiledModel, TensorBuffer reuse, warm-up, close order), on-disk CompiledModel.create(filePath, ...), UiState/MainViewModel with GPU-to-CPU fallback, MainScreen, and preprocess. Adds the LiteRT 2.3.0 litert and litert-gpu dependencies.
@smilingday

smilingday commented Oct 16, 2026 •

Copy link
Copy Markdown
Author

Hi @kkuan2011 @ksemenova @JolandaVerhoef, could one of you take a look when you have a chance? GitHub didn't auto-request reviewers on this PR (it comes from a fork).

This adds the code snippets for the LiteRT litert-runtime Android agent skill on developer.android.com (/agents/skills/device-ai/litert/litert-runtime). Once it merges, the skill switches from inline code blocks to github.code_snippet macros (pending on internal changes), as requested by the DAC skills reviewers.

  • One new file in the ai module: LiteRtRuntimeSkillSnippets.kt (same flat package as the other ai snippets), with 7 region tags (android_litert_runtime_skill_*).
  • Adds com.google.ai.edge.litert:litert and litert-gpu 2.3.0. No existing ai dependency changes version (diffed debugRuntimeClasspath against main).
  • ./gradlew :ai:clean :ai:assembleDebug --rerun-tasks and :ai:spotlessKotlinCheck pass locally; CI checks are green.

Thanks!

@john-rocky

Copy link
Copy Markdown

The snippets are compile-only here, so I ran them on a phone. I built this head (3d8f5fc) and called its five code regions (all but the imports and the dependency lines) from a local instrumented test on a Pixel 8a (Android 16) with LiteRT 2.3.0. The test serves the model through a test-only asset loader and leaves the snippet file unchanged. All 23 tests passed:

  • Classifier and preprocess with EfficientNet-Lite0 (float32), on one synthetic 224×224 input: the CPU output is within 2.4e-7 of the same model run on a Mac CPU with the ai-edge-litert 2.3.0 Python package, and the GPU output within 5.3e-3, with the same top class. createCompiledModelFromFile gives the same output as the asset load on both accelerators.
  • MainScreen with MainViewModel loads the model on the GPU and shows the top class after a tap on "Run (GPU)".
  • 20 more MainViewModel tests, including the failure paths: a warm-up or buffer allocation that throws leaks nothing (50 repeated failing loads keep no native heap and no extra threads), a model with an op the GPU doesn't support loads on the CPU, and onCleared closes the model, also during a load.
  • ./gradlew :ai:build and :ai:spotlessCheck pass. Lint reports no errors and one warning on the new file: UseKtx at line 200 (Bitmap.createScaledBitmap).

@kkuan2011 kkuan2011 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good from a snippets repo perspective, but I'll defer to the Intelligent Experiences team to review the code as they'll be more familiar with this subject matter area. Thanks Shuangfeng for adding these snippets!


package com.example.snippets.ai

// [START android_litert_runtime_skill_imports]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you actually don't need to add region tags around import statements (because ordering can change as you add more code to the file), so feel free to leave those hardcoded in the markdown file

Comment thread ai/build.gradle.kts
implementation(libs.firebase.ondevice)
implementation(libs.guava.android)
implementation(libs.mlkit.genai.prompt)
// [START android_litert_runtime_skill_dependencies]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same here, you don't need to add dependency block code snippets to the snippets repo (since the list of dependencies could change as the ai module grows), feel free to leave these dependencies hardcoded in the markdown file and remove the region tags here

This branch has not been deployed

No deployments
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