mlkit-agent-api
できること
このスキルは、Gemini Nano上で動き、自分で判断してアプリの関数を呼び出せるAndroidエージェントの作成を導きます。端末上で動くチャットアシスタントや単発のエージェントの作業を作りたいAndroid開発者向けです。AICoreのデベロッパープレビューを対象としています。
使う場面
- ✓端末上で動く、複数回のやり取りができるチャットアシスタントをAndroidアプリに加えたいとき。
- ✓端末上のエージェントに、AppFunctionsを通じてアプリの関数を呼び出させたいとき。
- ✓チャットの履歴を残さず、エージェントに1回限りのリクエストを処理させたいとき。
スキルのリンク
https://github.com/android/skills/blob/main/device-ai/mlkit-agent-api/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“Androidアプリに、アプリの関数を呼び出せる端末上のチャットアシスタントを作ってください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
このスキルは、指示とランナーを使ってエージェントを設定し、ViewModelでチャットのセッションを保持するか、単発のリクエストを実行します。アプリの関数を、エージェントが呼び出せるツールとして宣言します。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)を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
よくある質問
本番環境で使えますか?
このスキルはAICoreのデベロッパープレビューを対象にしているため、APIはプレビュー版と考えてください。
無限のツール呼び出しの繰り返しをどう防ぎますか?
エージェントの初期設定では、無限のツールループを防ぐため、10ターンまでに制限されています。
どのライブラリが必要ですか?
ML Kitのgenai-agentライブラリと、AppFunctionsライブラリを使います。
詳細
作者による元の説明
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
人気
- GitHubのスター
- 10k
- ランキング
- #11914
- ライセンス
- Apache-2.0
- 更新日
- 2026/10/29
コメント
- …
このリポジトリの他のスキル
agp-9-upgrade
10kエージェントが、AndroidアプリをAndroid Gradle Plugin 9にアップグレードし、その破壊的変更に対応できるよう支援します。マルチプラットフォームのプロジェクトは対象外です。
camerax
10kエージェントが、CameraXを使ってAndroidのカメラ機能を作れるよう支援します。動画の録画、ハードウェアの制御、ML Kit、Media3のエフェクトを含みます。
android-for-cars-parked-apps
10kエージェントが、Androidの動画、ゲーム、ブラウザのアプリを、Android AutoとAndroid Automotiveで駐車中のアプリとして動かせるように適合させるよう支援します。
appfunctions
10kエージェントが、Androidアプリの主要な作業の流れを見つけ、AIエージェントや音声コマンドが実行できるAppFunctionsとして公開できるよう支援します。
似ているスキル
add-model-provider
394k安全なセットアップコマンドで新しいAIモデルプロバイダーをOpenClawに接続し、APIキーを平文で残さず、ライブテストを行うようエージェントを支援します。
acp-router
394kClaude CodeやCursorなどのコーディングアシスタントに、OpenClaw経由でタスクを渡し、その作業がそのツール内で行われるようエージェントを支援します。
browser-automation
394kログイン、タブ、応答しなくなったページからの復旧を含め、Webページを段階的に操作するようエージェントに教えます。