Getting Started with Google's Gemini AI in Android

Google Cloud credits are provided for this project.

Unlock the potential of Google’s Gemini AI in Android development with this comprehensive tutorial. Learn how to integrate Gemini AI seamlessly into your Android apps, from setup to implementation. Start building smarter, more intuitive applications today!

In the ever-evolving landscape of mobile app development, integrating artificial intelligence (AI) has become increasingly essential. Google’s Gemini AI offers a powerful solution for enhancing Android applications with intelligent features. This tutorial will guide you through the process of getting started with Gemini AI in Android development, from installation to implementation.




To implement Gemini API in Android apps, we will be using the Rest API. At the time of writing this article, Gemini Pro API is available and this article will be using that API. Although we can directly use API normally in Android app, Google also provides a native Android SDK for Gemini integration. Let’s implement that SDK in our app.

I encourage you to follow along but if you’re keen to read the source code, you can find it on GitHub.


Setting up Android App

Android Studio’s canary versions Android Studio Jellyfish | 2023.3.1 Canary 9 has a built-in Gemini API starter template project. In this tutorial, we will use that template. First, you need to install canary version of Android Studio Jellyfish. You can easily do so by downloading Jetbrains Toolbox which allows you to install multiple instances of Android Studio at the same time.

Jetbrains Toolbox Jetbrains Toolbox

Once installed, launch the Android Studio and create a New Project. Select the Gemini Starter from the project template gallery.

Android Studio Templates Gallery Android Studio Templates Gallery

It will show a dialog where you need to put your Gemini API Key. You can put any random letters here (for now) and click Finish button.

Gemini API Key Dialog Gemini API Key Dialog

This will take some time to download the dependencies and build the project.

Gemini API project building Gemini API project building

Once build is successful, run the app in either Android Emulator or device. You will see app like the image below.

Initial Gemini API App after launch Initial Gemini API App after launch

Now, our app setup is complete.

Integrating Gemini SDK in Your Existing Project

If you want to integrate the Gemini API in any of your existing project, then you need to manually put dependencies in your app. In your module (app-level) Gradle configuration file (like <project>/<app-module>/build.gradle.kts), add the dependency for the Google AI SDK for Android:

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Google AI client SDK for Android
  implementation("com.google.ai.client.generativeai:generativeai:0.1.2")
}

Once the project is synced and ready to build, it’s time for the API key.



Getting API Key for Gemini AI

First of all you need to generate the Google Cloud API key. To do this, go to https://aistudio.google.com/ and click on Create API Key button.

Click on Create API Key Click on Create API Key

It will ask you to select a Google Cloud Project. If you don’t have one yet, go to https://console.cloud.google.com. Click on Create New Project.

Click on Create New Project in Google Cloud Click on Create New Project in Google Cloud

Add the name of the project and click on Create. It will take few moments and will redirect you to the project IAM console.

New Project in Google Cloud New Project in Google Cloud

Now head back to Google AI Studio (https://aistudio.google.com) where you were creating API key for Gemini Pro. And select your Google Cloud project in the drop down menu.

New Project in Google Cloud New Project in Google Cloud

It will take a few moments and will show you a list of all the API keys you have created.

API Keys in Google AI Studio API Keys in Google AI Studio

Click on the API you just created and it will show you a dialog with your key. Copy that key as we have to add it in our Android app now.

Copy API Key in Google AI Studio Copy API Key in Google AI Studio

Adding API Key in Your Android App

It’s advised to not include the API key in your project’s version control and also make sure it is secure. You can read on how to secure API keys in this article with different ways. In this tutorial, we will add the key in local.properties file which will be accessed through BuildConfig class.

Open your local.properties in Android project. And add a variable apiKey=YOUR_API_KEY. It shows like this for my project.

API Key in local.properties file API Key in local.properties file

If you are doing this in your existing project, then you can use the Gemini SDK like the code below.

val generativeModel = GenerativeModel(
    // For text-only input, use the gemini-pro model
    modelName = "gemini-pro",
    // Access your API key as a Build Configuration variable (see "Set up your API key" above)
    apiKey = BuildConfig.apiKey
)

val prompt = "Write a story about a magic backpack."
val response = generativeModel.generateContent(prompt)
print(response.text)

But if are doing this in Gemini Starter template project, then just run the app. Enter any prompt and it will fetch the result from Gemini API through its AI model.

Gemini API App Demo Gemini API App Demo

You can read more about how to generate images, or upload images in your prompt etc on Gemini API docs. The full source code of this project is available on my Github profile on the following link.



At the end, please Subscribe to my newsletter #Time with Wajahat to learn learn about the life experiences, lessons, career advices, technology & programming tips manually handcrafted and curated by Wajahat Karim.


If you liked this article, you can read my new articles below:


profile card
Wajahat Karim
🌍 Making the world a better place, one app at a time.
🔥 Google Developer Expert (GDE) in Android . 📱 Professional Android Developer with ~10 years experience. 💻 Creator of various Open Source libraries on Android . 📝 Author of two technical books and 100+ articles on Android. 🎤 A passionate Public Speaker giving talks all over the world.
Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan