Gemini API quickstart

This quickstart shows you how to get started with the Gemini API using the SDK of your choice.


Prerequisites

This quickstart assumes that you're familiar with using Android Studio to develop Android apps.

To complete this quickstart, make sure that your development environment and Android app meet the following requirements:

  • Android Studio (latest version)
  • Your Android app must target API level 21 or higher.

Consider accessing Gemini on-device

The client SDK for Android described in this tutorial lets you access the Gemini models which run on Google's servers. For use cases that involve processing sensitive data, offline availability, or for cost savings for frequently used user flows, you may want to consider accessing Gemini Nano which runs on-device. For more details, refer to the Android (on-device) page.

Install the Gemini API SDK

  1. 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:

    Kotlin

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

    Java

    For Java, you need to add two additional libraries.

    dependencies {
    
        // add the dependency for the Google AI client SDK for Android
        implementation("com.google.ai.client.generativeai:generativeai:0.7.0")
    
        // Required for one-shot operations (to use `ListenableFuture` from Guava Android)
        implementation("com.google.guava:guava:31.0.1-android")
    
        // Required for streaming operations (to use `Publisher` from Reactive Streams)
        implementation("org.reactivestreams:reactive-streams:1.0.4")
    }
    
  2. Sync your Android project with Gradle files.

Authenticate

Set up your API key

To use the Gemini API, you'll need an API key. If you don't already have one, create a key in Google AI Studio.

Get an API key

Then, configure your key.

It is strongly recommended that you do not check an API key into your version control system. Instead, you should store it in a local.properties file (which is located in your project's root directory, but excluded from version control), and then use the Secrets Gradle plugin for Android to read your API key as a Build Configuration variable.

Kotlin

// Access your API key as a Build Configuration variable
val apiKey = BuildConfig.apiKey

Java

// Access your API key as a Build Configuration variable
String apiKey = BuildConfig.apiKey;

If you want to see the implementation of the Secrets Gradle plugin, you can review the sample app for this SDK or use the latest preview of Android Studio Iguana which has a Gemini API Starter template (which includes the local.properties file to get you started).

Initialize the model

Before you can make any API calls, you need to import and initialize the model. Gemini 1.5 models are versatile and work with both text-only and multimodal prompts.

Kotlin

val generativeModel = GenerativeModel(
      modelName = "gemini-1.5-flash",
      // Access your API key as a Build Configuration variable (see "Set up your API key" above)
      apiKey = BuildConfig.apiKey
)

Java

GenerativeModel gm = new GenerativeModel(/* modelName */ "gemini-1.5-flash",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
      /* apiKey */ BuildConfig.apiKey);

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Make your first request

Generate text

Kotlin

generateContent() is a suspend function and needs to be called from a Coroutine scope. If you're unfamiliar with Coroutines, read Kotlin Coroutines on Android.

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

Java

generateContent() returns a ListenableFuture. If you're unfamiliar with this API, see the Android documentation about Using a ListenableFuture.

Content content = new Content.Builder()
      .addText("Write a story about an AI and magic")
      .build();

Executor executor = // ...

ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
      @Override
      public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
        System.out.println(resultText);
      }

      @Override
      public void onFailure(Throwable t) {
        t.printStackTrace();
      }
}, executor);

What's next

Now that you're set up to make requests to the Gemini API, you can use the full range of Gemini API capabilities to build your apps and workflows. To get started with Gemini API capabilities, see the following guides:

If you're new to working with generative AI or the Gemini API, check out the following guides, which will help you understand the Gemini API programming model:

For in-depth documentation of Gemini API methods and request parameters, see the guides in the API reference.