Die folgenden LiteRT-Laufzeit-APIs sind für die Android-Entwicklung verfügbar:
CompiledModelAPI: Der moderne Standard für leistungsstarke Inferenz, der die Hardwarebeschleunigung für CPU/GPU/NPU optimiert. Weitere Informationen zu Auswahl der CompiledModel API.InterpreterAPI: Die grundlegende Inferenz-API, die aus Gründen der Abwärtskompatibilität beibehalten wird.
Erste Schritte mit der CompiledModel API
Für klassische ML-Modelle finden Sie die folgenden Demo-Apps.
- Bildsegmentierung – Kotlin-App: CPU/GPU/NPU-Inferenz
- Bildsegmentierung – C++-App: CPU/GPU/NPU-Inferenz mit asynchroner Ausführung
Für GenAI-Modelle finden Sie die folgenden Demo-Apps:
- EmbeddingGemma – semantische Ähnlichkeit – C++-App: CPU/GPU/NPU-Inferenz
Unterstützte Android-Versionen und APIs
| LiteRT-Version | Status | Unterstützte API | Mindest-SDK-Level | Mindest-NDK-Version (falls verwendet) | Veröffentlicht am |
|---|---|---|---|---|---|
v2.1.4 |
✅ Aktuell | CompiledModel Interpreter(nur CPU) |
23 (Android 6 Marshmallow) |
r26a |
2026-04-10 |
v2.1.3 |
⚠️ Legacy | CompiledModel Interpreter(nur CPU) |
23 (Android 6 Marshmallow) |
r26a |
2026-03-16 |
v2.1.1 |
⚠️ Legacy | CompiledModel Interpreter(nur CPU) |
23 (Android 6 Marshmallow) |
r26a |
2026-01-27 |
v2.1.0 |
⚠️ Legacy | CompiledModel Interpreter(nur CPU) |
23 (Android 6 Marshmallow) |
r26a |
2025-12-19 |
v2.0.3 |
⚠️ Legacy | CompiledModel |
26 (Android 8 Oreo) |
r26a |
2025-11-08 |
v1.4.2 |
✅ Aktuell | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2026-03-16 |
v1.4.1 |
⚠️ Legacy | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-11-07 |
v1.4.0 |
⚠️ Legacy | Interpreter |
26 (Android 8 Oreo) |
r26a |
2025-06-25 |
v1.3.0 |
⚠️ Legacy | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-05-19 |
v1.2.0 |
⚠️ Legacy | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-03-13 |
Wichtig:Halten Sie Ihre Abhängigkeiten auf dem neuesten Stand, um die Kompatibilität mit den neuesten Funktionen und Sicherheitsupdates zu gewährleisten.
Schnellstart mit der CompiledModel API
Fügen Sie Ihrem Android-Projekt das LiteRT Maven-Paket hinzu:
dependencies {
...
implementation `com.google.ai.edge.litert:litert:2.1.0`
}
Binden Sie Ihr .tflite-Modell in die CompiledModel API ein. Das folgende Code-Snippet zeigt die grundlegende Implementierung in Kotlin und 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));