AI-assisted coding

Gemini in Android Studio empowers you to build apps faster than ever with AI-assisted coding features like custom code transforms and AI autocompletion.

Gemini code transforms

You can now prompt Gemini from the code editor for code suggestions to modify, optimize, or add code to your app as follows:

  1. To see an input field for your prompt, do one of the following from the code editor:
    • Right-click in the code editor and select Gemini > Generate code from the context menu.
    • Highlight code that you want Gemini to modify, right-click in the code editor, and select Gemini > Transform code from the context menu.
    • Press CTRL+\ (Command+\ on macOS).
  2. Prompt Gemini by describing how you want to modify or add to your code and press Enter. After Gemini processes your request, you should see a code diff.
    • For example, you can ask Gemini to simplify complex code by rewriting it, perform very specific code transformations such as "make this code idiomatic," or generate new functions you describe. Android Studio then shows you Gemini's code suggestion as a code diff, so you can review and accept only the suggestions you want.
  3. Review the code diff and do one of the following:
    • Further modify the suggested code by clicking Refine and entering a new prompt.
    • Click Accept All Changes to add the suggested changes to your code.

AI code completion

Gemini offers AI-enabled autocompletion of code in Android Studio that appears as gray italicized text as you type. We call this feature AI code completion. This feature saves you time and lets you complete coding projects faster by suggesting full functions. When AI code completion is enabled, Gemini might send additional information from your codebase such as surrounding pieces of your code, file types, and other necessary information to provide context to the LLM and provide more relevant suggestions.

Get started

To get started with AI code completion, follow these steps:

  1. Download the latest version of Android Studio Jellyfish or higher.
  2. To launch Gemini, open or start an Android Studio project and click View > Tool Windows > Gemini.
  3. Sign in to your Google Account.
  4. AI code completion only works when Gemini can access context from your codebase. Enable this during Gemini sign-in or any time through Android Studio > Settings > Gemini > Augment responses with information from your codebase.
  5. Open a file and start typing. Suggestions only trigger when the cursor is at the end of a line or anywhere on a blank line.
  6. Press Tab to accept a suggestion and Esc to clear a suggestion.

FAQ

How do I get access to AI code completion?

This functionality only works when Gemini can access context from your codebase. Enable this through Android Studio > Settings > Gemini > Context Awareness. AI-enabled autocompletion appears as you type. You can also use natural language comments to trigger code completions by adding comments.

Why am I not able to see a code completion?

First, check that you're logged in to Gemini and have opted in to AI code completion at Android Studio > Settings > Gemini. Keep in mind that the system won't always generate code completions. It's possible that the model doesn't have enough information to generate a response with high confidence.

How can I turn off AI code completion?

To disable AI code completion, go to Android Studio > Settings > Gemini and revoke consent and turn off AI code completion.