跳到正文
Skillpedia
移动应用10k · #11912

litert-runtime

它能做什么

这个技能指导使用 LiteRT CompiledModel API,借助 .tflite 模型在 Android 应用中加入设备端机器学习。适合在手机上处理图像、音频或嵌入任务的 Android 开发者。

适用场景

  • ✓当你希望在 Android 应用中运行图像分类或其他视觉模型时。
  • ✓当你需要在 Gradle 中设置 LiteRT,并在 Play 服务运行时与捆绑运行时之间做选择时。
  • ✓当你希望在 GPU 上运行 .tflite 模型,并在需要时回退到 CPU 的时候。

技能链接

https://github.com/android/skills/blob/main/device-ai/litert/litert-runtime/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/android/skills/blob/main/device-ai/litert/litert-runtime/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请使用 LiteRT 在 GPU 上为我的 Android 应用添加基于 .tflite 模型的图像分类功能。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能会帮你选择运行时、添加 Gradle 依赖,并为模型准备图像。然后它会展示如何在 GPU 上运行模型,并在需要时回退到 CPU。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

This skill provides step-by-step guidance for building an Android app that runs a .tflite model with the LiteRT CompiledModel API (com.google.ai.edge.litert:litert 2.x; source repository) on the CPU or the GPU, with the runtime bundled in the APK or from Google Play services. Don't use the deprecated org.tensorflow.lite.Interpreter API. For open text language models using .litertlm files, suggest the litert-lm skill.

Prerequisites

  • A Kotlin Android project (minSdk must be 24 or higher, as declared by the LiteRT 2.3.0 AARs).
  • The runtime that Step 1 picks: the Google Play services runtime (com.google.android.gms:play-services-litert, the default; see LiteRT in Google Play services), or the bundled runtime (implementation("com.google.ai.edge.litert:litert:2.3.0") and implementation("com.google.ai.edge.litert:litert-gpu:2.3.0") from Google Maven). For the bundled runtime, from 2.3.0 the GPU accelerator is the second artifact: with litert alone the app builds, and CompiledModel.create with CompiledModel.Options(Accelerator.GPU) throws LiteRtException because the APK has no GPU accelerator library. With CompiledModel.Options(Accelerator.GPU, Accelerator.CPU) it doesn't throw: the model runs on the CPU while the app reports the GPU, and only Logcat shows it (GPU accelerator could not be loaded and registered). No manifest entry is needed: litert-api, which litert depends on, declares the OpenCL driver libraries (uses-native-library) in its own manifest.
  • A verified .tflite model and its input contract (input dimensions, mean, standard deviation, channel order, and tensor layout). For models with a verified recipe, inspect the LiteRT model recipes.

Critical mandates

  • Single-thread confinement : One single-thread executor or confined coroutine dispatcher must own the CompiledModel instance. Creation, inference, and closing must execute on that same background thread, never on the main thread.
  • Buffer lifecycle : Allocate TensorBuffer instances once using createInputBuffers and createOutputBuffers, reuse them across inferences, and close all input and output buffers before closing the CompiledModel.
  • Synchronous timing : model.run enqueues work on the GPU and returns before GPU execution completes. Always measure model.run and output buffer readback (outputs[0].readFloat) together.
  • Warm-up and exception safety : Execute one warm-up inference inside the helper constructor's init block so first-run GPU shader compilation doesn't block the first user interaction, and call close in catch before rethrowing so neither CompiledModel nor TensorBuffer handles leak if buffer allocation or warm-up throws.
  • Runtime choice : Before Step 1, you must read LiteRT in Google Play services and choose the runtime. Use the Google Play services runtime unless the app needs the NPU, custom operators, or a Support library class that this skill can't replace (such as MetadataExtractor), or also targets devices without Google Play services. Never put both runtimes in the same build variant.
  • Migration : When the app already uses org.tensorflow.lite code, you must read the "Migrating from legacy TFLite Interpreter" section in full before you edit any file. Migrated code must not import any org.tensorflow.lite class (including DataType), every legacy GPU choice (GpuDelegate, or the Support library's Model.Device.GPU) becomes CompiledModel.Options(Accelerator.GPU, Accelerator.CPU), and an NNAPI choice maps to that same GPU option.

Detailed steps

Step 1: Configure Gradle and place the model file

Search the project for libs.versions.toml, gradle.properties, app/build.gradle.kts, and any existing org.tensorflow.lite imports before editing build files. Then pick the runtime:

  • Google Play services runtime (default) : Use it unless one of the bundled runtime cases applies, also when the request doesn't name a runtime. You must read LiteRT in Google Play services now: its dependencies replace the ones in this step, and its Environment replaces the environment property of Step 2. Use version 17.0.0 exactly, and run `./gradlew assembleD
常见问题

我应该使用哪个运行时?

该技能会请你在 Google Play 服务运行时(默认选项)和捆绑运行时之间做选择。

需要的最低 Android 版本是多少?

项目的 minSdk 必须为 24 或更高,因为 LiteRT 库有此要求。

它会使用旧的 TensorFlow Lite Interpreter 吗?

不会。该技能说明不要使用那个已弃用的 API,而是改用 CompiledModel。

详情

作者的原始描述

Configures and implements LiteRT (.tflite) on-device inference on Android in Kotlin using the CompiledModel API on CPU and GPU. Use this skill for any Android LiteRT or .tflite task, including Gradle setup, image classification or vision/audio/embedding helpers, TensorBuffer reuse, CPU memory optimization, Bitmap preprocessing, AndroidViewModel GPU-to-CPU fallback with warm-up on a single-thread dispatcher, LiteRT in Google Play services (play-services-litert), migrating from legacy org.tensorflow.lite.Interpreter, GpuDelegate, or play-services-tflite, and latency benchmarking. Don't use for r

repo: android/skills

热度

GitHub 星标
10k
排名
#11912
许可证
Apache-2.0
更新时间
2026/10/29

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能