Intro: Gemini in Android Studio
Set up Gemini in Android Studio and boost your coding with AI! This guide covers enabling Gemini, getting it running, and using its smart suggestions to speed up development and improvement.
With over 5 years of experience developing native Android apps, starting from Android Studio 2.0 all the way up to the latest Koala version (2024.1.1), I’m here to guide you through using Gemini AI in your Android development journey
First things ... we have to download latest Gemini in Android Studio

Step to enable Gemini in Android Studio:


View
Bar > Tool Windows > GeminiOnce you have the correct version and enabled data sharing (optional), here’s how to launch Gemini:
- Open or start an Android Studio project.
- Go to View > Tool Windows > Gemini.
- Sign in to your Google account if prompted.
Boost your productivity with Gemini in Android Studio

Dive into Gemini, your AI coding assistant in Android Studio! Give it a spin and see the magic for yourself.

Improve your code by asking Gemini
Having trouble understanding how code exits in Android Studio? Gemini can help! Ask it about different ways code execution can end, from normal termination to errors.

Exiting code flow:
public void onEventMainThread(GetTriviaEvent event) {
if (lvTrivia.isRefreshing()) {
lvTrivia.setRefreshing(false);
}
if (event.isRefresh()) {
adapter.clear();
}
if (event.getList().size() < TAKE) {
lvTrivia.hasMorePages(false);
} else {
lvTrivia.hasMorePages(true);
}
lvTrivia.onLoadMoreComplete();
adapter.addAll(event.getList());
}
Gemini Suggestion improvements:
public void onEventMainThread(GetTriviaEvent event) {
// Clear the adapter only if it's a refresh event
if(event.isRefresh()) {
adapter.clear();
}
// Add the new items to the adapter
adapter.addAll(event.getList());
// Update the loading state of the ListView
if (lvTrivia.isRefreshing()) {
lvTrivia.setRefreshing(false);
}
// Set the "hasMorePages" flag based on the size of the received list
lvTrivia.hasMorePages(event.getList().size() >= TAKE);
// Notify the ListView that the loading is complete
lvTrivia.onLoadMoreComplete();
}
Improvements
- Reordered code for better readability: The code is now organized to reflect the logical flow of actions: clear the adapter, add new items, update loading state, and finally notify the ListView. This makes the code easier to understand and follow.
- Combined hasMorePages logic: The if-else block for setting hasMorePages is simplified into a single line using a ternary operator. This makes the code more concise and readable.
- Improved comments: The comments are more concise and focus on explaining the purpose of each section of code, rather than simply stating what the code does.
- Removed unnecessary if statement: The if (lvTrivia.isRefreshing()) block is now only executed if the lvTrivia is actually refreshing. This avoids unnecessary code execution.
- Optimizes code: Highlights the improvement of code quality.
- Improves readability: Emphasizes the benefit of clearer code.
- Faster and easier: Concludes with the time-saving advantage.
My Conclusion
In my experience, Gemini in Android Studio is an excellent tool. Even when you restrict access to your project and disable suggestions, the chat box remains useful for asking about references or solving routine tasks. This way, you don’t need to leave your IDE to search for references in a browser.

Article by Tan Jack Hau