litert-runtime
Ce qu’il fait
Ce skill guide l'ajout d'apprentissage automatique sur l'appareil dans une application Android avec un modèle .tflite, en utilisant l'API LiteRT CompiledModel. Il s'adresse aux développeurs Android qui traitent des images, de l'audio ou des embeddings sur le téléphone.
Quand l’utiliser
- ✓Quand vous voulez qu'un modèle de classification d'images ou un autre modèle de vision tourne dans une application Android.
- ✓Quand vous devez configurer LiteRT dans Gradle, en choisissant entre les runtimes Play services et intégré.
- ✓Quand vous voulez exécuter un modèle .tflite sur le GPU avec un repli sur le CPU.
Lien du skill
https://github.com/android/skills/blob/main/device-ai/litert/litert-runtime/SKILL.mdCopiez-le et collez-le dans votre IA (Claude, ChatGPT ou Cursor) en lui demandant de suivre les instructions.
Comment l’utiliser
Le plus simple : copiez le message et collez-le dans votre IA. Elle lit le skill et suit les instructions.
Message prêt à coller
Pour Claude Code, Codex ou Cursor : lancez-la dans le dossier du projet et le skill est installé.
Téléchargez le SKILL.md et placez-le dans le dossier de skills de votre agent.
Exemple de demande à votre IA
“Ajoute la classification d'images avec un modèle .tflite à mon application Android en utilisant LiteRT sur le GPU.”
5 nouveaux skills par semaine. 1 minute de lecture.
Comment ça marche
Le skill vous aide à choisir le runtime, à ajouter les dépendances Gradle et à préparer les images pour le modèle. Il montre ensuite comment exécuter le modèle sur le GPU et revenir au CPU si nécessaire.
Contenu technique (SKILL.md original)
Texte original de l’auteur, en anglais, directement depuis 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 (
minSdkmust be24or 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")andimplementation("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: withlitertalone the app builds, andCompiledModel.createwithCompiledModel.Options(Accelerator.GPU)throwsLiteRtExceptionbecause the APK has no GPU accelerator library. WithCompiledModel.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, whichlitertdepends on, declares the OpenCL driver libraries (uses-native-library) in its own manifest. - A verified
.tflitemodel 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
CompiledModelinstance. Creation, inference, and closing must execute on that same background thread, never on the main thread. - Buffer lifecycle : Allocate
TensorBufferinstances once usingcreateInputBuffersandcreateOutputBuffers, reuse them across inferences, and close all input and output buffers before closing theCompiledModel. - Synchronous timing :
model.runenqueues work on the GPU and returns before GPU execution completes. Always measuremodel.runand output buffer readback (outputs[0].readFloat) together. - Warm-up and exception safety : Execute one warm-up inference inside the helper constructor's
initblock so first-run GPU shader compilation doesn't block the first user interaction, and callcloseincatchbefore rethrowing so neitherCompiledModelnorTensorBufferhandles 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.litecode, you must read the "Migrating from legacy TFLite Interpreter" section in full before you edit any file. Migrated code must not import anyorg.tensorflow.liteclass (includingDataType), every legacy GPU choice (GpuDelegate, or the Support library'sModel.Device.GPU) becomesCompiledModel.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
Environmentreplaces theenvironmentproperty of Step 2. Use version17.0.0exactly, and run `./gradlew assembleD
Questions fréquentes
Quel runtime dois-je utiliser ?
Le skill vous demande de choisir entre le runtime des services Google Play, qui est celui par défaut, et le runtime intégré.
Quelle version minimale d'Android faut-il ?
Le minSdk du projet doit être 24 ou plus, car les bibliothèques LiteRT l'exigent.
Utilise-t-il l'ancien interpréteur TensorFlow Lite ?
Non. Le skill précise de ne pas utiliser cette API obsolète et utilise CompiledModel à la place.
Détails
Description originale de l’auteur
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
Popularité
- Étoiles GitHub
- 10k
- Position au classement
- #11912
- Licence
- Apache-2.0
- Mis à jour
- 29/10/2026
Commentaires
- …
Autres skills de ce dépôt
agp-9-upgrade
10kAide votre agent à migrer une application Android vers Android Gradle Plugin 9 et à corriger les changements incompatibles. Hors Kotlin Multiplatform.
camerax
10kAide votre agent à créer des fonctions de caméra Android avec CameraX : vidéo, commandes matérielles, ML Kit et effets Media3.
android-for-cars-parked-apps
10kAide votre agent à adapter une application vidéo, de jeu ou de navigateur Android pour qu'elle tourne sur Android Auto ou Automotive.
appfunctions
10kAide votre agent à exposer les parcours clés d'une application Android sous forme d'AppFunctions, que les agents IA et la voix peuvent exécuter.
Skills similaires
android-clean-architecture
280kPermet à votre agent d'organiser efficacement les projets Android. Il structure le code pour faciliter la maintenance et le développement de nouvelles fonctionnalités.
compose-multiplatform-patterns
280kAide votre agent à utiliser les patterns Compose Multiplatform et Jetpack Compose dans les projets KMP : état, navigation, thèmes et performances.
dart-flutter-patterns
280kPermet à votre agent d'écrire du code pour les applications Dart et Flutter. Il utilise des patterns modernes pour des apps robustes et faciles à gérer.