Yum News Yum News

Technology  ·  Artificial Intelligence  ·  Developer Insights

Breaking
Gemini Task Automation Guide: India UK Japan Apps ✦   How to Set Up ChatGPT Wallet for Agent Purchases ✦   How to Set Up and Use Gemini Live on Apple CarPlay ✦   How to Set Up Gemini Live on Apple CarPlay ✦   How to Use the Gemini Decisions API for Fast Choices ✦   How to Use Gemini Task Automation on Pixel Phones   ✦   Gemini 3.8 Live launches with native audio dialogue  ✦  Android September Drop adds Find Hub memory  ✦  ML Kit now supports Gemini Nano on-device inference
How to Build On-Device Agents with ML Kit Gemini Nano 4
androidai-toolstutorialsgeminideveloper

How to Build On-Device Agents with ML Kit Gemini Nano 4

Step-by-step tutorial to create private on-device AI agents in Android apps using the ML Kit Agentic API powered by Gemini Nano 4.

Android developers can now build autonomous agents that run entirely on the device. Google’s ML Kit Agentic API, powered by Gemini Nano 4, handles multi-turn conversations, tool calling, and guardrails without sending user data to the cloud.

This guide walks through the exact setup, prerequisites, and code needed to create your first on-device agent. You will learn how to configure the library, define an agent, run conversations, and add tools. All information comes from official Google developer documentation.

Why On-Device Agents Matter

On-device agents keep prompts and reasoning private. There is zero network latency for the core loop. Gemini Nano 4 adds native function calling, so the model can plan multi-step actions and execute them inside your app.

The Agentic API automates the orchestration loop, conversation history, and safety limits. You focus on defining useful tools and clear instructions instead of writing low-level prompt management code.

Android phone showing code on screen

Prerequisites

Your project must meet these requirements before the Agentic API will work:

  • Android 14 (API 34) or higher, enrolled in the AICore Developer Preview program.
  • A supported device that can run Gemini Nano 4 (Pixel 10 and later series, selected Honor, OnePlus, OPPO, and other flagship devices). Check the official device list.
  • Minimum SDK 26.
  • Kotlin 2.3 or higher.

Enroll in the AICore Developer Preview through the official Google form so your device receives the preview models.

Step 1: Add the Dependencies

Open your module’s build.gradle.kts and add the genai-agent library. Include AppFunctions if you plan to expose typed tools:

dependencies {
    implementation("com.google.mlkit:genai-agent:1.0.0-alpha1")

    // Optional for AppFunctions tools
    implementation("androidx.appfunctions:appfunctions:1.0.0-alpha13")
    ksp("androidx.appfunctions:appfunctions-compiler:1.0.0-alpha13")
}

Sync the project. The library is currently in alpha, so expect possible API changes before stable release.

Step 2: Define the Agent and Runner

An agent consists of a configuration (name, instructions, optional max turns) and an AgentRunner that executes it.

import com.google.mlkit.genai.agent.Agent
import com.google.mlkit.genai.agent.AgentRunner

val agent = Agent.builder()
    .setName("Assistant")
    .setInstructions("You are a helpful on-device assistant. Keep answers concise.")
    .setMaxTurns(10) // Prevents runaway loops
    .build()

val runner = AgentRunner(agent)

The maxTurns limit defaults to 10 and acts as a battery and performance guardrail.

Step 3: Run a Conversation

For multi-turn chats, create a session. The runner keeps history automatically.

import com.google.mlkit.genai.prompt.Content
import kotlinx.coroutines.launch

val session = runner.getOrCreateSession("chat_session")

val prompt = Content.builder()
    .text("Help me plan a short morning routine.")
    .build()

lifecycleScope.launch {
    session.runAsync(prompt)
        .textTokens()
        .collect { token ->
            // Append token to your UI
            println(token)
        }
}

For one-shot questions that do not need history, call runner.runSingleTurnAsync(prompt) instead.

Always close the runner when the ViewModel or Activity is destroyed:

runner.close()

This releases sessions and cancels background work.

Step 4: Add Tools with AppFunctions

Tools let the agent take real actions. The recommended path is declarative Android AppFunctions. They auto-generate schemas and can be discovered automatically.

Add .enableAppFunctions(true) to the builder so the agent finds every AppFunction in your package. Then implement the functions with the AppFunctions Jetpack library. When the model decides a tool is needed, the AgentRunner executes it and continues the loop.

Common patterns include photo edits, playback controls, preference toggles, or local data queries. Because the reasoning stays on-device, sensitive information never leaves the phone.

Laptop and smartphone on a developer desk

Tips for Production Use

  • Test on a physical device enrolled in the AICore preview. Emulators do not run Gemini Nano.
  • Keep instructions specific and short. Long system prompts consume context.
  • Set a reasonable maxTurns value for your use case.
  • Combine with Firebase AI Logic when you need a cloud model for complex tasks and fall back to on-device for private or offline work.
  • Monitor battery and memory. Gemini Nano 4 improves efficiency (up to 3x faster inference and 4x less memory in internal tests), but long loops still cost resources.

Watch the official Android Developers session for a full walkthrough with the Jetpacker sample app:

If you are also exploring system-level integration, see our guide on the Android intelligence system and intelligent apps.

Sources

  • Android Developers Blog: What’s new in intelligent apps at the Android Developer Summit
  • ML Kit documentation: Get started with agentic workflows
  • ML Kit: Build agentic workflows with Gemini Nano
  • YouTube: Unlock agentic capabilities in your app with ML Kit GenAI

Share this story