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 Google AI SDK
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") }
Sync your Android project with Gradle files.
Set up authentication
The easiest way to authenticate to the Gemini API is to configure an API key, as described in this section. If you need stricter access controls, you can use OAuth instead.
If you don't already have an API key, create one in Google AI Studio.
Get an API key from Google AI Studio
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).
Import the library
Import the Google Generative AI library.
Kotlin
// other imports...
import com.google.ai.client.generativeai.GenerativeModel
Java
// other imports...
import com.google.ai.client.generativeai.GenerativeModel;
import com.google.ai.client.generativeai.java.GenerativeModelFutures;
import com.google.ai.client.generativeai.type.Content;
import com.google.ai.client.generativeai.type.GenerateContentResponse;
Make your first request
Use the
generateContent
method
to 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 generativeModel =
GenerativeModel(
// Specify a Gemini model appropriate for your use case
modelName = "gemini-1.5-flash",
// 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)
Java
generateContent()
returns a ListenableFuture
. If you're
unfamiliar with this API, see the Android documentation about Using a
ListenableFuture
.
// Specify a Gemini model appropriate for your use case
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);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Content content =
new Content.Builder().addText("Write a story about a magic backpack.").build();
// For illustrative purposes only. You should use an executor that fits your needs.
Executor executor = Executors.newSingleThreadExecutor();
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:
For in-depth documentation of Gemini API methods and request parameters, see the guides in the API reference.