๐Ÿง  Android AI Projects and Production

LiteRT and LiteRT-LM on Android

ML Kit and Gemini Nano cover common tasks, but sometimes you need your own model, a custom classifier or an open LLM like Gemma that runs on any capable device. LiteRT (the successor to TensorFlow Lite) runs custom models; LiteRT-LM is its runtime for language models.

โœ๏ธ Yunok Tech ๐Ÿ“… Updated โฑ 3 min read ๐ŸŽฏ Advanced
On this page
  1. Where LiteRT fits
  2. LiteRT for custom models
  3. LiteRT-LM for language models
  4. Choosing and delivering a model
  5. Performance and devices
  6. Use cases
  7. Troubleshooting
  8. Frequently asked questions
  9. Conclusion

LiteRT is Google's on-device inference runtime for .tflite models, the evolution of TensorFlow Lite. LiteRT-LM builds on it to run language models efficiently, with conversation handling, streaming, and GPU or NPU acceleration.

APIs evolve quickly

The code below follows the LiteRT-LM Kotlin documentation at the time of writing. Check the dependency coordinates, versions and class names on the official pages before you build.

Where LiteRT fits

On-device AI options on Android At the top level, ML Kit offers ready-made tasks and ML Kit GenAI uses Gemini Nano. For custom models, LiteRT runs tflite models, and LiteRT-LM runs language models such as Gemma. All run on CPU, GPU or NPU hardware. ML Kit / ML Kit GenAIready-made tasks, Gemini Nano LiteRTyour .tflite models LiteRT-LMopen LLMs (Gemma) Acceleration: CPU ยท GPU ยท NPU More control and device reach than Gemini Nano, but you ship and manage the model.
Use ML Kit first; reach for LiteRT when you need a custom or open model.

LiteRT for custom models

Typical uses are image classifiers, audio models or custom detectors exported to .tflite. The flow is: load the model, prepare input tensors in the exact shape and normalisation used in training, run inference, and post-process outputs.

Classic LiteRT Interpreter usage (shape of the API)Kotlin
class Classifier(context: Context) : AutoCloseable {
    private val interpreter = Interpreter(
        FileUtil.loadMappedFile(context, "classifier.tflite"),
        Interpreter.Options().setNumThreads(4),
    )

    fun classify(input: FloatArray): FloatArray {
        val output = Array(1) { FloatArray(NUM_CLASSES) }
        interpreter.run(arrayOf(input), output)          // input must match the model's shape
        return output[0]
    }

    override fun close() = interpreter.close()
}

LiteRT also offers a newer CompiledModel API with improved GPU and NPU acceleration. Check the docs for the version you use.

LiteRT-LM for language models

LiteRT-LM's Kotlin API follows this pattern: configure an Engine with a model file and a backend, initialise it off the main thread (loading can take several seconds), open a conversation, and stream responses as a Flow.

LocalLlm.kt (based on the LiteRT-LM Android docs)Kotlin
class LocalLlm(private val modelPath: String, private val cacheDir: String) : AutoCloseable {
    private var engine: Engine? = null

    /** Slow: loads the model. Call from a background coroutine. */
    suspend fun load() = withContext(Dispatchers.Default) {
        val config = EngineConfig(
            modelPath = modelPath,
            backend = Backend.GPU(),          // or CPU / NPU depending on device
            cacheDir = cacheDir,              // speeds up subsequent loads
        )
        engine = Engine(config).also { it.initialize() }
    }

    /** Streams the reply; collect in a ViewModel and append chunks to UI state. */
    fun ask(prompt: String): Flow<String> {
        val conversation = checkNotNull(engine) { "Call load() first" }.createConversation()
        return conversation.sendMessageAsync(Message.of(prompt))
            .map { it.toString() }
            .onCompletion { conversation.close() }
    }

    override fun close() { engine?.close(); engine = null }
}

Choosing and delivering a model

  • Models: open models such as Gemma variants converted for LiteRT-LM. Smaller (around 1B parameters or less, quantised) models suit more devices.
  • Size: LLM files are often hundreds of MB to several GB. Don't bundle them in the base APK.
  • Delivery: download on demand over Wi-Fi with progress and checksums, or use Play for On-device AI for model delivery through Play.
  • Licences: check each model's licence and usage terms.

Performance and devices

ConcernGuidance
MemoryCheck available RAM before loading; low-RAM devices may fail or be killed
BackendGPU or NPU is faster where supported; fall back to CPU
ThermalLong generations heat devices; cap output length
Load timeLoad once and reuse; use a cache directory; show progress

See Offline AI and Device Compatibility for gating features by device capability.

Use cases

Custom classifier

Plant disease or product defect detection with a LiteRT model.

Offline assistant

A small Gemma model for private, offline Q&A on capable devices.

Field apps

AI that must work without connectivity.

Troubleshooting

  • Wrong outputs from a custom model: input preprocessing (size, normalisation, channel order) doesn't match training.
  • OOM or crash when loading an LLM: the model is too large for the device; use a smaller or more quantised model.
  • UI freezes: initialisation or inference on the main thread.

Frequently asked questions

Is TensorFlow Lite deprecated?

TensorFlow Lite has been renamed and continued as LiteRT. Existing .tflite models work with LiteRT.

LiteRT-LM or Gemini Nano?

Gemini Nano needs no model shipping but only runs on supported devices. LiteRT-LM lets you choose and ship your own model, at the cost of size and management.

Conclusion

LiteRT runs your custom models; LiteRT-LM runs open LLMs on device with streaming and hardware acceleration. Load off the main thread, deliver models on demand, gate by device capability, and check model licences.

Next step: process images and text on device with On-Device Image and Text Processing.

Official references

๐Ÿ“ฑ Apps built by Yunok Tech

Real Android apps on Google Play, free to download.

See all our apps โ†’

Ready to build real Android applications?

Every AI feature still runs inside an Android app that has to be fast, reliable and well structured. Strengthen the Kotlin, Jetpack Compose and architecture skills that AI integrations depend on, then apply these AI concepts with confidence.

Move beyond theory and strengthen your Android development skills with Yunoktech's Android course. Learn core concepts, practical implementation, modern Android development techniques, and the reasoning behind building maintainable applications.

Explore the Android course and take your next step as an Android developer.

Share: LinkedIn X / Twitter WhatsApp