Build ADK agents for Android
Stay organized with collections
Save and categorize content based on your preferences.
The Agent Development Kit (ADK) for Android library lets you build and integrate sophisticated AI agents directly into your Android apps. ADK is an open-source developer framework for building AI-powered agents that run locally, on hosted services, and on Android mobile devices. The framework supports Kotlin and Java programming languages, allowing you to start building quickly agents and scale up to complex, multi-agent applications.
The ADK for Android library provides specialized dependencies and runtime support tailored for mobile environments. You can build agents that execute AI models on-device using Gemini Nano using ML Kit GenAI APIs, allowing you to create privacy-focused and low-latency AI experiences that can function without network access.
Use ADK Kotlin in Android projects
You can use the ADK Kotlin agent API to build AI agents that run inside Android apps. The agent code you write is identical to the ADK Kotlin Get started guide. The differences are the Gradle dependency, the project configuration, and how you invoke the agent at runtime.
Prerequisites
The ADK for Android library has the following development requirements:
- Android Studio
- Android SDK (compileSdk 34 or higher, minSdk 24 or higher)
Configure your Android project
In your Android project's build.gradle.kts, add the ADK Android dependency
and the KSP annotation processor:
plugins{
id("com.android.application")
kotlin("android")
id("com.google.devtools.ksp")version"2.1.20-2.0.1"
}
android{
namespace="com.example.agent"
compileSdk=34
defaultConfig{
applicationId="com.example.agent"
minSdk=24
targetSdk=34
}
}
dependencies{
implementation("com.google.adk:google-adk-kotlin-core-android:0.1.0")
ksp("com.google.adk:google-adk-kotlin-processor:0.1.0")
}
kotlin{
jvmToolchain(17)
}
Define your agent
The agent code is identical to the ADK Kotlin Quickstart. The
HelloTimeAgent code example with the @Tool, @Param, and
.generatedTools() syntax works without modification on Android:
packagecom.example.agent
importcom.google.adk.kt.agents.Instruction
importcom.google.adk.kt.agents.LlmAgent
importcom.google.adk.kt.annotations.Param
importcom.google.adk.kt.annotations.Tool
importcom.google.adk.kt.models.Gemini
classTimeService{
/** Mock tool implementation */
@Tool
fungetCurrentTime(
@Param("Name of the city to get the time for")city:String
):Map<String,String>{
returnmapOf("city"tocity,"time"to"The time is 10:30am.")
}
}
objectHelloTimeAgent{
@JvmField
valrootAgent=LlmAgent(
name="hello_time_agent",
description="Tells the current time in a specified city.",
model=Gemini(
name="gemini-flash-latest",
apiKey=System.getenv("GOOGLE_API_KEY")
?:error("GOOGLE_API_KEY environment variable not set."),
),
instruction=Instruction(
"You are a helpful assistant that tells the current time in a city. "
+"Use the 'getCurrentTime' tool for this purpose."
),
tools=TimeService().generatedTools(),
)
}
Run the agent from your Android app
On Android-powered devices, use InMemoryRunner to invoke the agent and collect
responses from a coroutine, as shown in the following code example:
importcom.google.adk.kt.runners.InMemoryRunner
importcom.google.adk.kt.sessions.InMemorySessionService
importcom.google.adk.kt.types.Content
importcom.google.adk.kt.types.Part
importcom.google.adk.kt.types.Role
importkotlinx.coroutines.CoroutineScope
importkotlinx.coroutines.launch
// Create a runner and session service
valsessionService=InMemorySessionService()
valrunner=InMemoryRunner(
agent=HelloTimeAgent.rootAgent,
sessionService=sessionService,
)
// Call the agent from a coroutine (e.g. in a ViewModel or Activity)
scope.launch{
runner.runAsync(
userId="user-123",
sessionId="session-123",
newMessage=Content(
role=Role.USER,
parts=listOf(Part(text="What time is it in New York?")),
),
).collect{event->
valtext=event.content?.parts?.firstOrNull()?.text
if(!text.isNullOrBlank()){
// Update your UI with the agent's response
}
}
}
On-device models with Gemini Nano
The ADK for Android artifact includes support for on-device inference using Gemini Nano through ML Kit GenAI API. This approach allows agents to run without network access, keeping data on the device.
To use an on-device model, create a GenaiPrompt model instead
of Gemini, as shown in the following code example:
importcom.google.adk.kt.models.mlkit.GenaiPrompt
importcom.google.mlkit.genai.prompt.GenerativeModel
// Create an ML Kit GenerativeModel for on-device inference
valgenerativeModel:GenerativeModel=// ... initialize using ML Kit
valonDeviceModel=GenaiPrompt.create(
generativeModel=generativeModel,
name="gemini-nano",
)
valagent=LlmAgent(
name="on_device_agent",
model=onDeviceModel,
instruction=Instruction("You are a helpful assistant."),
)
You can also combine cloud and on-device models in a multi-agent system: use a
cloud-based Gemini for the root orchestrator and on-device GenaiPrompt
models for sub-agents that handle privacy-sensitive tasks. For more on this
pattern, see the ADK for Kotlin and Android blog post.
For a complete working Activity and more examples, see the ADK Kotlin examples on GitHub.