← All courses ← Course home

Lesson 04 of 07

Create the Android Studio LiteRT project

Add the current LiteRT Android library, put clothing.tflite in assets, and create CompiledModel buffers once.

Agent brief (llms.md)

Environment setup

Install Android Studio (a current stable 2026 release). Let its bundled JDK set up the project. Install Android SDK Platform 35 or newer, plus an emulator image. A physical device with USB debugging also works. Create a Kotlin Android app with minimum SDK 23, then add the LiteRT Maven runtime.

java -version
adb version
# In Android Studio: New Project → Empty Activity → Kotlin → min SDK 23.
# Copy the converted model into app/src/main/assets/.
mkdir -p app/src/main/assets
cp clothing.tflite app/src/main/assets/clothing.tflite

LiteRT’s current runtime package is com.google.ai.edge.litert. org.tensorflow:tensorflow-lite is the older TFLite package name. Pin a known published runtime. If you change the version, run the smoke test again.

Gradle dependency

// app/build.gradle.kts
android {
    namespace = "com.example.clothinglitert"
    compileSdk = 35
    defaultConfig {
        applicationId = "com.example.clothinglitert"
        minSdk = 23
        targetSdk = 35
        versionCode = 1
        versionName = "1.0"
    }
}
dependencies {
    // Published CompiledModel artifact used by this lesson.
    implementation("com.google.ai.edge.litert:litert:2.1.5")
}

Sync Gradle. LiteRT 2.x gives you the current CompiledModel API. You pick CPU, GPU, or NPU through options. This course starts with CPU so the first results are easy to repeat. The older Interpreter API is not used for new code.

Package and load the asset

Files in app/src/main/assets live inside the APK. They are not ordinary file paths on disk. Copy the asset to the app’s files directory once, then pass that absolute path to the API.

package com.example.clothinglitert

import android.content.Context
import java.io.File

fun copyModelFromAssets(context: Context): File {
    val destination = File(context.filesDir, "clothing.tflite")
    if (!destination.exists()) {
        context.assets.open("clothing.tflite").use { input ->
            destination.outputStream().use { output -> input.copyTo(output) }
        }
    }
    check(destination.length() > 0) { "clothing.tflite was not packaged" }
    return destination
}

Create the compiled model and buffers

import com.google.ai.edge.litert.Accelerator
import com.google.ai.edge.litert.CompiledModel

val modelFile = copyModelFromAssets(this)
val compiledModel = CompiledModel.create(
    modelFile.absolutePath,
    CompiledModel.Options(Accelerator.CPU),
)
val inputBuffers = compiledModel.createInputBuffers()
val outputBuffers = compiledModel.createOutputBuffers()
check(inputBuffers.size == 1) { "expected one input buffer" }
check(outputBuffers.size == 1) { "expected one output buffer" }

Create the compiled model and its buffers once, for example in a ViewModel or when the activity starts. Do not create them for every selected image. The path-based overload is the documented quick start. If your pinned SDK has a verified asset-manager overload, you can use that instead.

First APK check

  1. Build and install the empty app.
  2. Open APK Analyzer and confirm assets/clothing.tflite is present.
  3. Run the asset-copy check and log its non-zero size.
  4. Only then add image UI and inference.

Key concepts

  • Kotlin app, min SDK 23. LiteRT dependency is com.google.ai.edge.litert:litert.
  • Copy clothing.tflite from assets to filesDir, then pass the absolute path.
  • Create CompiledModel with CompiledModel.Options(Accelerator.CPU).
  • Allocate input and output buffers once. Start on CPU.

Takeaways

  • Confirm assets/clothing.tflite in APK Analyzer before you add UI.
  • Do not recreate CompiledModel or buffers on every image.
  • Do not use the older Interpreter API for new code.