Getting started with the Gemini API and Android

Learn how to use the Gemini API and the Google AI SDK to prototype generative AI in Android applications.

 

Introduction to the Gemini API and prompt engineering

Pathway

Explore Google AI Studio and the capabilities of the Gemini generative AI model. Learn how to design and test the different types of prompts (freeform, structured, and chat) and get an API key for the Gemini API.

This pathway can be useful for further experimentation with the Gemini API and lays the groundwork for integrating its features into your application. Optionally, you can also try out the API using a simple NodeJS web application. If you don't already have NodeJS and NPM on your machine, feel free to skip this step and return back to Android in this pathway.

Build your own generative AI powered Android app

Video

Watch this talk from Google I/O 2024 to learn how to add generative AI to your Android app using the Gemini API.

Explore use cases for generative AI in mobile apps and learn how to get started with the Gemini API and the Google AI client SDK on Android.

The Google AI client SDK is in preview and there are additional security considerations for using API keys in mobile and web client applications. Migrate to Vertex AI in Firebase for production use beyond prototyping for client apps or access the Gemini models server-side through Vertex AI.

Introduction to the Google AI client SDK for Android

Article

For mobile apps, you need to consider whether you want to use generative AI with a remote, cloud-based model or a local, on-device model. Take into consideration such factors as network dependency, the size of the model you want to use, cost, and privacy when choosing your approach.

This solution focuses on using the Google AI client SDK for Android to remotely access the Gemini API for generative AI. This approach features the following:

  • Is network-dependent and sends data to the cloud for processing access.
  • Provides native Kotlin and Java SDK and does not require working directly with REST APIs or custom server-side integrations.
  • Runs on Google's servers, providing access to larger and more performant models without any device or hardware dependencies.
  • Easy access to the latest improvements in Google's automatically updated models.

Getting started with the Google AI client SDK for Android requires setting up a project in Google AI Studio to obtain an API key for the Gemini API. Next, add the required dependencies to your app's build configuration, initialize the model that best fits your use case and submit a prompt to generate output.

If you want to use the alternative on-device approach, see the next step which covers a preview of Gemini Nano powered by Android AICore.

Calling the Gemini API directly from your app using the Google AI client SDK for Android is only recommended for prototyping and exploring the Gemini generative AI models. As you embed your API key directly in your Android app, you risk exposing it to malicious actors. For production and enterprise-scale apps, access the Gemini API server-side to keep your API key safe or migrate to the Vertex AI in Firebase instead, which offers additional security features.

If you use Android Studio, you can quickly get started with the Gemini API template that's described in more detail in a later step.

Access Gemini Nano on-device with Android AICore (in preview)

Article

The alternative approach to using the Google AI client SDK to access the Gemini API is using an on-device AI model such as Gemini Nano powered by Android AICore through the Google AI Edge SDK for Android.

Instead of calling a remote service that provides access to a generative AI model, the prompts are processed by a model that is stored on the device itself. This option removes the dependency on network access and completes all processing on-device. Consider this approach for potential cost-savings, offline access, smaller and narrower tasks, as well as local processing of sensitive data for your app.

The Google AI Edge SDK for Android is currently in early access private preview and is not generally available. In the meantime, learn more about on-device AI use-cases and its architecture.

Build with the Google AI client SDK in Android Studio

Article

Android Studio includes a new project template for the Gemini API that helps you explore and prototype generative AI in Android apps with the Google AI client SDK.

Follow the steps in the template to set up an API key (if you don't already have one). Then, configure the application and make your first API call. The template automatically sets up an Android app that connects to the Gemini API and summarizes text.

Note that there are additional security considerations for using API keys directly in mobile client applications. The final step in this solution shows how to prepare your Android app for production by migrating to Vertex AI in Firebase to access the Gemini API.

Explore the Android sample apps in Kotlin

Code sample

Explore the generative AI sample app for the Google AI client SDK for Android.

This example app demonstrates three key use cases: generating text, photo reasoning (using multimodal inputs), and multi-turn conversations (chat). It also shows how to use content streaming to improve response time by displaying partial results.

Follow the steps in the README to get started, which includes configuring your Google AI API Key.

Multimodal prompting using the Google AI SDK

Article

Multimodal prompts combine different types of media together, such as text, images and audio. For example, you could create prompts that identify objects in an image, extract text from a photo, or reference a picture.

To get started, read this guide about file prompting strategies and multimodal concepts, which includes best practices for designing multimodal prompts.

Next, explore the multimodal capabilities of the Gemini models in Google AI Studio by uploading or selecting a file as part of your prompt.

Learn how to use multimodal inputs using the Google AI client SDK for Android, find image requirements for prompts, and explore the multimodal photo reasoning demo in the sample app.

For further reading, see the solution Leveraging the Gemini Pro Vision model for image understanding, multimodal prompts and accessibility.

Prepare your app for production with Vertex AI in Firebase (in preview)

Article

When you are ready to prepare your application for production, transition to Vertex AI in Firebase.

Vertex AI in Firebase is currently in preview and is not yet recommended for production use. It is not subject to any SLA or deprecation policy and could change in backwards-incompatible ways. The team is actively looking for feedback from developers, so make sure to check the docs for the current release status of the SDKs. The team is committed to making these SDKs generally available as soon as possible!

When you want to call the Gemini API directly from your mobile Android app, we recommend using Vertex AI in Firebase. It offers enhanced security features for mobile and web apps, including integration with Google Cloud IAM for API authorization and Firebase App Check for integrity verification. It integrates with Google's wider Firebase developer platform (like Cloud Storage and Remote Config), while also giving you access to the tools, workflows and scale offered through Google Cloud. Among other differences , Vertex AI also supports increased request quotas and enterprise features.

Note that using the Google AI client SDK to call the Gemini API directly from a mobile/web client is only recommended for prototyping and experimentation. Follow this guide to migrate to the Vertex AI in Firebase SDK (preview) by updating your package dependencies, imports, and changing how the AI model is initialized.

Quiz

Test your knowledge and earn your 'Getting started with the Gemini API and Android' badge.