LiteRT para Android

As seguintes APIs de tempo de execução do LiteRT estão disponíveis para desenvolvimento no Android:

  • API CompiledModel: o padrão moderno para inferência de alto desempenho, simplificando a aceleração de hardware em CPU/GPU/NPU. Saiba mais sobre por que escolher a API CompiledModel.
  • API Interpreter: a API de inferência básica, mantida para compatibilidade com versões anteriores.

Começar a usar a API CompiledModel

Versões e APIs do Android compatíveis

Versão do LiteRT Status API com suporte Nível mínimo do SDK Versão mínima do NDK (se usada) Data do lançamento
v2.1.4 ✅ Mais recente CompiledModel
Interpreter(somente CPU)
23 (Android 6 Marshmallow) r26a 2026-04-10
v2.1.3 ⚠️ Legado CompiledModel
Interpreter(somente CPU)
23 (Android 6 Marshmallow) r26a 2026-03-16
v2.1.1 ⚠️ Legado CompiledModel
Interpreter(somente CPU)
23 (Android 6 Marshmallow) r26a 2026-01-27
v2.1.0 ⚠️ Legado CompiledModel
Interpreter(somente CPU)
23 (Android 6 Marshmallow) r26a 2025-12-19
v2.0.3 ⚠️ Legado CompiledModel 26 (Android 8 Oreo) r26a 2025-11-08
v1.4.2 ✅ Mais recente Interpreter 21 (Android 5 Lollipop) r26a 2026-03-16
v1.4.1 ⚠️ Legado Interpreter 21 (Android 5 Lollipop) r26a 2025-11-07
v1.4.0 ⚠️ Legado Interpreter 26 (Android 8 Oreo) r26a 25/06/2025
v1.3.0 ⚠️ Legado Interpreter 21 (Android 5 Lollipop) r26a 2025-05-19
v1.2.0 ⚠️ Legado Interpreter 21 (Android 5 Lollipop) r26a 2025-03-13

Importante:mantenha suas dependências atualizadas para garantir a compatibilidade com os recursos e as atualizações de segurança mais recentes.

Guia de início rápido com a API CompiledModel

Adicione o pacote do Maven LiteRT ao seu Projeto do Android:

dependencies {
  ...
  implementation `com.google.ai.edge.litert:litert:2.1.0`
}

Integre seu modelo .tflite à API CompiledModel. O snippet de código a seguir mostra a implementação básica em Kotlin e 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));