Free weekly email: the week's new posts, videos and one tip from the books, every Sunday. Get it free →
Homelab the Right Way

Home › Posts

The Trap of Choosing the Tool Before the Task

Stop picking AI models first. Start by defining the job, then walk down the cheapest option list to find the right backend for your Android app.

· 5 min read · androidaiarchitectureml-kitdecision-tree

The Trap of Choosing the Tool Before the Task

Most new Android developers pick the tool before they define the job. They read about local language models or spend time getting a two-gigabyte model to load on their device, and then they use it to do something ML Kit's text recognizer would have handled in fifty milliseconds with no download at all. This is the trap. You assume that because a powerful model exists, you must use it. The result is an app that ships with massive downloads, drains battery, and fails on older devices, all for a feature that could have been solved with a simpler, bundled API.

The second most common mistake is the opposite: building a feature entirely on Gemini Nano, testing it exclusively on a Pixel phone, and then shipping to a user base where most phones cannot run it. This creates a broken experience for the majority of your users because device support for advanced on-device AI is not universal.

The fix is to start from the job and walk down a short list, choosing the cheapest option first. You must design your architecture to handle unavailability gracefully, treating missing backends as normal states rather than errors.

The trap: picking the most powerful model first

The temptation is to reach for the largest language model or the most complex runtime immediately. However, the book outlines five distinct options, arranged from simplest to most resource-intensive. You must evaluate them in this order and stop at the first one that fits your needs.

  1. ML Kit task APIs. These include text recognition, barcode scanning, face detection, image labeling, language identification, translation, smart reply, and more. Each is a narrow, well-tested model behind a small Kotlin API. Some ship inside your APK ("bundled"), while some are downloaded once through Google Play services ("unbundled"). There are no prompts, no tokens, and no model files for you to manage. If ML Kit has a feature for your job, it is almost always the right answer.
  2. Gemini Nano through the ML Kit GenAI APIs. This is Google's on-device language model, living inside AICore, an Android system service shared by every app. ML Kit wraps it in feature APIs like summarization, proofreading, rewriting, image description, and speech recognition, plus a general Prompt API. The catch is device support: only a published list of recent phones runs it.
  3. LiteRT with your own model. Formerly known as TensorFlow Lite, LiteRT runs your own .tflite model (classifier, detector, or embedding) on the CPU, GPU, or NPU if ML Kit does not offer it. MediaPipe Tasks sits on top of this runtime with ready-made wrappers for common jobs such as text embedding.
  4. LiteRT-LM with a small language model. This is Google's runtime for running language models like Gemma from a .litertlm file, replacing the maintenance-only MediaPipe LLM Inference API. It runs on almost any recent 64-bit phone, but the model file is hundreds of megabytes to a few gigabytes, and the user's RAM and battery pay for it.
  5. Your homelab server. Ollama on a computer at home exposes a simple HTTP API. Your app sends the conversation and streams the answer back. It can run models far larger than any phone can hold and costs the phone almost nothing, but it needs a network path to the server, and prompts leave the device.

The trap: ignoring device reach and availability

Even if you choose Gemini Nano, you cannot assume every user has a compatible device. The book emphasizes that "Reach" means the share of your users who can use the feature, which depends entirely on your audience's hardware. You must compare options based on what matters: Reach, Privacy, Quality, and Cost.

No single row wins everywhere in the comparison table provided in the chapter. This is why the best apps combine two paths: a small on-device path that always works and a bigger one when it is available. You cannot treat "no backend available" as an error dialog. It is a normal state on many phones. Instead, show the feature as "needs a download" or "connect a home server" with one button that fixes it, not a red banner.

The fix: one interface, several backends

Whatever you pick today, you will add a second backend later. Gemini Nano reaches new phone families, smaller Gemma builds arrive, or users ask to point the app at their own server. Apps that put AI calls directly in their ViewModels end up rewriting their screens each time. Apps that put them behind one small interface simply add a class.

You must define a single interface that your screen and ViewModel only know about. Each backend translates this interface into its own SDK. The code structure looks like this:

// The whole app talks to this, never to a specific SDK.
interface TextGenerator {
    /** A short, user-facing name: "On this phone", "Home server"... */
    val label: String

    /** Can this backend run right now on this device? */
    suspend fun availability(): Availability

    /** Streams the answer piece by piece. Cancel the collector to stop. */
    fun generate(messages: List<ChatMessage>): Flow<String>
}

sealed interface Availability {
    data object Ready : Availability
    data class NeedsDownload(val bytes: Long?) : Availability
    data class Unavailable(val reason: String) : Availability
}

data class ChatMessage(val role: Role, val text: String)
enum class Role { System, User, Assistant }

Three implementations of TextGenerator appear in the book: one for Gemini Nano, one for LiteRT-LM, and one for Ollama. A chat screen can be built on top of this interface that works with any of them.

The fix: runtime backend selection

You need a chooser that selects the best available backend at runtime. This chooser is deliberately boring: ask each backend whether it can run, in order of preference, and take the first that says yes. Keep the user in charge, so a person who prefers the home server can pin it.

The implementation uses a list of ordered generators and checks for a pinned selection first, then falls back to the best available option:

class BackendChooser(private val ordered: List<TextGenerator>) {
    suspend fun best(pinned: String? = null): TextGenerator? {
        ordered.firstOrNull { it.label == pinned }?.let { p ->
            if (p.availability() is Availability.Ready) return p
        }
        return ordered.firstOrNull { it.availability() is Availability.Ready }
    }
}

The book provides a specific tip for ordering this list: order by privacy first, then quality. The recommended order is Gemini Nano, then your local LiteRT-LM model, then the home server. A user on a supported phone never sends a prompt anywhere, and everyone else still gets an answer.

Checklist

  • Start from the job and walk down the five options list: ML Kit task APIs, Gemini Nano, LiteRT, LiteRT-LM, or homelab server.
  • Stop at the first option that fits your needs; do not jump to a language model if ML Kit suffices.
  • Define a single TextGenerator interface with label, availability(), and generate() methods.
  • Implement Availability as a sealed interface with Ready, NeedsDownload, and Unavailable states.
  • Use a BackendChooser class to select the first backend that reports Ready.
  • Order backends by privacy first, then quality: Gemini Nano, local LiteRT-LM, home server.
  • Never show a red error banner for unavailable backends; offer a button to download or connect instead.
  • Design your UI to work with any backend implementation behind the single interface.

More articles