mlkit-agent-api
What it does
This skill guides building an Android agent that runs on Gemini Nano and can call app functions on its own. It is for Android developers building chat assistants or one-off agent tasks on the phone. It targets the AICore Developer Preview.
When to use it
- ✓When you want a multi-turn chat assistant in an Android app that runs on the device.
- ✓When you want the on-device agent to call your app's functions through AppFunctions.
- ✓When you need a single one-off request handled by an agent without keeping a chat history.
Skill link
https://github.com/android/skills/blob/main/device-ai/mlkit-agent-api/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Build an on-device chat assistant in my Android app that can call my app's functions.”
5 new skills a week. 1 minute to read.
How it works
The skill sets up an agent with its instructions and a runner, then keeps a chat session in a ViewModel or runs one-off requests. It declares app functions as tools the agent can call.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Targeting AICore Developer Preview. This skill provides authoritative execution patterns and failure-prevention rules for on-device agentic workflows in Android apps.
1. Prerequisites and setup
Gradle dependencies
// app/build.gradle.kts
dependencies {
// Core ML Kit GenAI Agent API
implementation("com.google.mlkit:genai-agent:1.0.0-alpha1")
// Android AppFunctions dependency for on-device tool declarations
implementation("androidx.appfunctions:appfunctions:1.0.0-alpha13")
}
[!IMPORTANT] Always use official SDK interfaces from
com.google.mlkit.genai.agent.*,androidx.appfunctions.*,com.google.mlkit.genai.prompt.*, andcom.google.mlkit.genai.common.*. Never declare local placeholder interfaces or fallback mocks.
2. Minimal agent and runner initialization
Configure an Agent with Agent.builder and wrap execution in AgentRunner:
import com.google.mlkit.genai.agent.Agent
import com.google.mlkit.genai.agent.AgentRunner
val agent = Agent.builder()
.setName("OnDeviceAssistant")
.setInstructions("You are a helpful on-device assistant.")
.build()
val runner = AgentRunner(agent)
Agent.builderdefaults tomaxTurns = 10automatically to prevent infinite tool loops.AgentRunnerdefaults to greedy decoding (temperature = 0.0f) for deterministic tool selection and schema compliance.AgentRunner(agent)automatically resolves application context usingMlKitContextfor AppFunction execution in Android apps. An explicitAgentRunner(context, agent)overload is available for headless tests or custom context scopes.
3. Multi-turn chat and lifecycle (ViewModel)
AgentSession preserves conversational history in memory and is not
Parcelable . To preserve dialogue history across Android configuration
changes (such as screen rotation), retain session inside an AndroidX
ViewModel.
When executing single-turn ephemeral queries, managing concurrent multi-sessions, or requiring explicit RAM cleanup, you must follow the guidelines in Session lifecycle.
package com.example.app
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.mlkit.genai.agent.Agent
import com.google.mlkit.genai.agent.AgentRunner
import com.google.mlkit.genai.agent.AgentSession
import com.google.mlkit.genai.agent.textTokens
import com.google.mlkit.genai.common.GenAiException
import com.google.mlkit.genai.prompt.Content
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
class ChatViewModel : ViewModel() {
private val agent = Agent.builder()
.setInstructions("You are a helpful on-device assistant.")
.build()
private val runner = AgentRunner(agent)
private val session: AgentSession =
runner.getOrCreateSession("chat_session")
private val _uiState = MutableStateFlow<ChatUiState>(ChatUiState.Idle)
val uiState: StateFlow<ChatUiState> = _uiState.asStateFlow()
fun sendMessage(prompt: String) {
viewModelScope.launch {
_uiState.value = ChatUiState.Loading("")
try {
// Content.builder() required; runAsync takes Content
val content = Content.builder().text(prompt).build()
var fullResponse = ""
session.runAsync(content)
// Extension function requiring textTokens import
.textTokens()
.collect { token ->
fullResponse += token
_uiState.value = ChatUiState.Loading(fullResponse)
}
_uiState.value = ChatUiState.Success(fullResponse)
} catch (e: GenAiException) {
// Catch GenAiException to prevent app crashes on AI errors
val err = e.localizedMessage ?: "AI error (${e.errorCode})"
_uiState.value = ChatUiState.Error(err)
}
}
}
override fun onCleared() {
super.onCleared()
// Evict conversation session from RAM when ViewModel
FAQ
Is this ready for production?
The skill targets the AICore Developer Preview, so expect the APIs to be preview versions.
How does it stop endless tool loops?
The agent's default setting limits it to 10 turns to prevent infinite tool loops.
What libraries does it need?
It uses the ML Kit genai-agent library and the AppFunctions library.
Details
Original description by the author
Builds autonomous on-device agentic workflows in Android apps using the ML Kit GenAI Agent API powered by Gemini Nano and AICore. Use this skill when implementing conversational agents, multi-turn chat with AgentSession in a ViewModel, autonomous tool calling using Android AppFunctions (@AppFunctionDeclaration and AppFunctionAgentTools.discover), ephemeral single-turn execution with runner.runSingleTurnAsync, or handling GenAiException error resilience. It solves the problem of orchestrating local reasoning loops, typed tool dispatch, and session persistence on-device with zero cloud latency a
repo: android/skills
Popularity
- GitHub stars
- 10k
- Ranking position
- #11914
- License
- Apache-2.0
- Updated
- 10/29/2026
Comments
- …
More skills from this repository
agp-9-upgrade
10kHelps your agent upgrade an Android app to Android Gradle Plugin 9 and work through its breaking changes. Not for multiplatform projects.
camerax
10kHelps your agent build Android camera features with CameraX, including video recording, hardware controls, ML Kit and Media3 effects.
android-for-cars-parked-apps
10kHelps your agent adapt an Android video, game or browser app to run as a parked app on Android Auto and Android Automotive.
appfunctions
10kHelps your agent find key workflows in an Android app and expose them as AppFunctions that AI agents and voice commands can run.
Similar skills
add-model-provider
394kLets your agent connect a new AI model provider to OpenClaw using safe setup commands, keep API keys out of plain text, and run a live test.
acp-router
394kHelps your agent send a task to a coding assistant such as Claude Code or Cursor through OpenClaw, so the work runs in that tool.
browser-automation
394kTeaches your agent to control web pages step by step, including logins, tabs, and recovery when a page stops responding.