Best practices

Gemini 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 Gemini'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.

Use Agent Mode

Gemini in Android Studio's Agent Mode goes beyond what you can experience by just chatting with Gemini. You can give the agent a high-level goal, and the agent breaks down the problem and creates a plan to solve it. The agent calls on tools, edits multiple files, and fixes errors as it goes. Agent Mode helps you handle complex tasks more efficiently, speeding up your entire development process.

Be specific

Gemini in Android Studio 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 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:

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.
How do I add camera support to my app?

Generating code:

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.

Write a function to download an image.

Explaining a concept:

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?

Explain Android Services.

Describe the structure of the answer

By default, Gemini 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 Gemini into your app and need the code in a certain format so you can use it instantly, give Gemini clear instructions about generating and formatting the code.

Structuring the response:

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.

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:

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

Break down complex requests into smaller tasks

For complex, multi-step requests, make sure to try Agent Mode. However, if the agent's approach isn't what you expect, it might help to guide Gemini 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:

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 functionalitys

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.

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 Gemini in Android Studio 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 Gemini astray, whereas too little won't provide enough context for it to help. Use the available features to give Gemini access to the necessary context:

  • Attach images to provide a visual of the UI you're envisioning or the app architecture you're trying to understand.
  • Attach files to highlight relevant parts of the codebase.
  • Use .aiexclude files to make Gemini ignore certain parts of the codebase.
  • Use AGENTS.md 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 to extend Gemini's knowledge and capabilities with the external environment.

Ask for explanations

If you don't understand Gemini in Android Studio'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 Gemini is incorrect, and asking for an explanation will provide an opportunity for Gemini to correct itself.

Iterate on your instructions

Similar to when you're working with a coworker, working with Gemini in Android Studio sometimes takes some back and forth communication to get clarity on the task and expected outcome. If Gemini'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 or updating your AGENTS.md files.