The agent in Android Studio acts as your coding collaborator, accelerating your
development workflow by generating code, summarizing complex topics, finding
specific resources, and more. Effective communication is the key to unlocking
the agent's full potential.

This guide outlines strategies for setting up your development environment
and writing prompts that yield useful and reliable responses. For multiple
strategies, we've provided example prompts for different use cases. For more
example prompts, see the [Prompt gallery](https://developer.android.com/studio/prompt-gallery).

## Be specific

The agent performs best with clear, specific instructions. If
there are certain libraries, APIs, or approaches that you'd like to use, include
them in your question---or include them in your project's root [AGENTS.md file](https://developer.android.com/studio/gemini/agent-files)
to persist them across interactions. Think of it as filing a bug report: the
more information you provide, the faster and more effectively the problem can be
solved.

**Implementing new functionality:**

### Do

How do I add camera support to my app using CameraX? Make sure to include any dependencies and permissions I need to use the library.

### Don't

How do I add camera support to my app?

**Generating code:**

### Do

Write a Kotlin suspend function that uses the Coil library
to download an image from a given URL string.

The function should be
named `fetchImageAsBitmap`. It should take a
`Context` and a `String` URL as arguments and return
a nullable `Bitmap`. If there's any network or decoding error,
it should catch the exception, log it, and return
`null`.

### Don't

Write a function to download an image.

**Explaining a concept:**

### Do

I'm building a music player app and need to continue
playback when the app is in the background.

What are my options for
keeping my app running while the user is doing other things on the device?
What are the API level requirements and battery life implications for each
approach?

### Don't

Explain Android Services.

## Describe the structure of the answer

By default, the agent provides answers in a conversational, paragraph-based
format. While this is often useful for explanations, you can get more effective
and immediately usable results by telling it how to structure the response.
Specifying the output format directs the model to organize the information in
a clear, predictable way, saving you the time of reformatting it yourself.

If you want to insert code generated by the agent into your app and need the
code in a certain format so you can use it instantly, give the agent clear
instructions about generating and formatting the code.

**Structuring the response:**

### Do

Make a Room entity for a user profile. It should contain
the fields `id`, `userName`, `email`, and
`memberSince`. The `id` field should be
auto-generated. The `memberSince` field should represent a
timestamp.

### Don't

Make a Room entity for a user profile.

If you're asking a more open-ended question, ask it for *x* number of options.

**Limiting the response:**

### Do

What tools are available for app performance optimization? Give me a list of the top three.

### Don't

What tools are available for app performance optimization?

## Break down complex requests into smaller tasks

If the agent's approach isn't what you expect, it might help to guide the agent
through a series of smaller, sequential prompts rather than asking for
everything at once. This approach gives you more control and lets you verify
each step before proceeding to the next.

**Applying sequential prompts:**

### Do

**Prompt 1: The data model**   
First, create
a Kotlin data class named `User`. It should have three
properties: `id` (an Int), `name` (a String), and
`email` (a String).  

**Prompt 2: The network
layer**   
Now, create a Retrofit API interface called
`ApiService`. It needs a single `suspend fun` called
`getUsers()` that returns a
`List<User>`.

**Prompt 3: The state
management**   
Next, write a `UsersViewModel` that
takes the `ApiService` as a dependency. The `UsersViewModel`
should expose a
`StateFlow` of a sealed `UiState` class that can
represent `Loading, Success(users: List)`, and
`Error(message: String)` states. Create a function in the
`ViewModel` to fetch the users and update the state. Write unit tests for
the new functionality.

**Prompt 4: The UI
layer**   
Finally, write a Jetpack Compose function named
`UserListScreen` that accepts the `UiState` as a
parameter. Use a `when` statement to display a
`CircularProgressIndicator` for the loading state, a
`LazyColumn` of user names for the success state, and a
`Text` element with the error message for the error state. Add
tests that verify the new functionality.

### Don't

Create a Jetpack Compose screen that fetches a list of users from a JSON API and displays them in a list. It should have a `ViewModel` that uses Retrofit and coroutines, a repository to handle the data logic, a `User` data class, and it needs to show loading and error states.

## Manage context

A major part of working with the agent is providing the right
context. Provide relevant code snippets, file contents, and project structure
information to get the most helpful responses. You might have to iterate to
strike the right balance: too much information might lead the agent astray,
whereas too little won't provide enough context for it to help. Use the
available features to give the agent access to the necessary context:

- [Attach images](https://developer.android.com/studio/gemini/attach-image) to provide a visual of the UI you're envisioning or the app architecture you're trying to understand.
- [Attach files](https://developer.android.com/studio/gemini/attach-file) to highlight relevant parts of the codebase.
- Use [`.aiexclude` files](https://developer.android.com/studio/gemini/aiexclude) to make the agent ignore certain parts of the codebase.
- Use [AGENTS.md files](https://developer.android.com/studio/gemini/agent-files) to give project-specific instructions and share them across your team. When using AGENTS.md files, use Markdown headers and formatting to make your instructions clear.
- When using Agent Mode, configure [MCP servers](https://developer.android.com/studio/gemini/add-mcp-server) to extend the agent's knowledge and capabilities with the external environment.

## Ask for explanations

If you don't understand the agent's response, ask for an
explanation. This is a great way to learn and to verify that the proposed
solution is appropriate for your use case. If the response wasn't what you
expected, it could actually be that the agent is incorrect, and asking for an
explanation will provide an opportunity for the agent to correct itself.

## Iterate on your instructions

Similar to when you're working with a coworker, working with the agent
sometimes takes some back and forth communication to get clarity on the
task and expected outcome. If the agent's initial response is insufficient,
respond to it with feedback and suggestions for how to improve. If its response
improves based on your suggestions, incorporate that suggestion in the original
prompt by updating the entry in the [Prompt Library](https://developer.android.com/studio/gemini/prompt-library) or updating your
[AGENTS.md files](https://developer.android.com/studio/gemini/agent-files).