Las siguientes APIs de tiempo de ejecución de LiteRT están disponibles para el desarrollo de Android:
- API de
CompiledModel: Es el estándar moderno para la inferencia de alto rendimiento, que optimiza la aceleración de hardware en CPU/GPU/NPU. Obtén más información sobre por qué elegir la API de CompiledModel. - API de
Interpreter: Es la API de inferencia básica, que se mantiene para la retrocompatibilidad.
Comienza a usar la API de CompiledModel
Para modelos de AA clásicos, consulta las siguientes apps de demostración.
- App de Kotlin de segmentación de imágenes: Inferencia de CPU/GPU/NPU.
- App de C++ de segmentación de imágenes: Inferencia de CPU/GPU/NPU con ejecución asíncrona.
Para modelos de IA generativa, consulta las siguientes apps de demostración:
- App de C++ de similitud semántica de EmbeddingGemma: Inferencia de CPU/GPU/NPU.
Versiones y APIs de Android compatibles
| Versión de LiteRT | Estado | API compatible | Nivel mínimo del SDK | Versión mínima del NDK (si se usa) | Fecha de lanzamiento |
|---|---|---|---|---|---|
v2.1.4 |
✅ Más reciente | CompiledModel Interpreter(solo CPU) |
23 (Android 6 Marshmallow) |
r26a |
2026-04-10 |
v2.1.3 |
⚠️ Heredada | CompiledModel Interpreter(solo CPU) |
23 (Android 6 Marshmallow) |
r26a |
2026-03-16 |
v2.1.1 |
⚠️ Heredada | CompiledModel Interpreter(solo CPU) |
23 (Android 6 Marshmallow) |
r26a |
2026-01-27 |
v2.1.0 |
⚠️ Heredada | CompiledModel Interpreter(solo CPU) |
23 (Android 6 Marshmallow) |
r26a |
2025-12-19 |
v2.0.3 |
⚠️ Heredada | CompiledModel |
26 (Android 8 Oreo) |
r26a |
2025-11-08 |
v1.4.2 |
✅ Más reciente | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2026-03-16 |
v1.4.1 |
⚠️ Heredada | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-11-07 |
v1.4.0 |
⚠️ Heredada | Interpreter |
26 (Android 8 Oreo) |
r26a |
25/6/2025 |
v1.3.0 |
⚠️ Heredada | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-05-19 |
v1.2.0 |
⚠️ Heredada | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-03-13 |
Importante: Mantén tus dependencias actualizadas para garantizar la compatibilidad con las funciones y actualizaciones de seguridad más recientes.
Guía de inicio rápido de la API de CompiledModel
Agrega el paquete de Maven de LiteRT a tu proyecto de Android:
dependencies {
...
implementation `com.google.ai.edge.litert:litert:2.1.0`
}
Integra tu modelo .tflite con la API de CompiledModel. En el siguiente fragmento de código, se muestra la implementación básica en Kotlin y C++.
Kotlin
// Load model and initialize runtime
val compiledModel = CompiledModel.create(
"/path/to/mymodel.tflite",
CompiledModel.Options(Accelerator.CPU))
// Preallocate input/output buffers
val inputBuffers = compiledModel.createInputBuffers()
val outputBuffers = compiledModel.createOutputBuffers()
// Fill the input buffer
inputBuffers.get(0).writeFloat(input0)
inputBuffers.get(1).writeFloat(input1)
// Invoke
compiledModel.run(inputBuffers, outputBuffers)
// Read the output
val output = outputBuffers.get(0).readFloat()
C++
// Load model and initialize runtime
LITERT_ASSIGN_OR_RETURN(auto env, GetEnvironment());
LITERT_ASSIGN_OR_RETURN(auto options, GetOptions());
LITERT_ASSIGN_OR_RETURN(
auto compiled_model,
CompiledModel::Create(env, "/path/to/mymodel.tflite", options));
// Preallocate input/output buffers
LITERT_ASSIGN_OR_RETURN(auto input_buffers,compiled_model.CreateInputBuffers(signature_index));
LITERT_ASSIGN_OR_RETURN(auto output_buffers,compiled_model.CreateOutputBuffers(signature_index));
// Fill the input buffer
LITERT_ABORT_IF_ERROR(input_buffers[0].Write(input0));
LITERT_ABORT_IF_ERROR(input_buffers[1].Write(input1));
// Invoke
LITERT_ABORT_IF_ERROR(compiled_model.Run(signature_index, input_buffers, output_buffers));
// Read the output
LITERT_ABORT_IF_ERROR(output_buffers[0].Read(output0));