Add Gemini capabilities to your Android app

1. Before you begin

The Gemini API gives you access to generative AI models from the Gemini family. It will enable you to build new experiences that until now have not been available.

This codelab guides you through the process of adding summarization capabilities to the JetNews application. JetNews is a news reading app built with Jetpack compose part of the compose-samples repository.

JetNews application in action

In this codelab, you will:

  • Craft a prompt to summarize news articles in Vertex AI studio.
  • Integrate the Vertex AI in Firebase SDK to JetNews.
  • Add a "Gemini summary" section at the top of the article view and a "summarization" button in the bottom bar.

7eee9f1dcad682c.png

The JetNews app with the summarization feature powered by the Gemini API.

Prerequisites

  • Basic Kotlin knowledge
  • Familiarity with how to use Android Studio and test apps in an emulator or physical device running Android API level 21 or higher
  • A Google Account to access Firebase

2. Design a prompt

The prompt is the message sent by your application to the Gemini model via the Gemini API. It can include text, images, audio, and video files.

Crafting the ideal prompt for a specific use case is more art than science, and it often requires multiple iterations. Vertex AI Studio is a great tool to craft your prompt.

For this example, you'll create a text prompt in Vertex AI Studio. Open Vertex AI Studio, and then click "Freeform" in the left navigation.

52e11cb6e67d7199.png

In this codelab, you want to summarize the content of a technical article into four bullet points. Your goal with this prompt is to make sure that the summary is relevant and in plain text without any Markdown formatting.

After a few iterations, you'll probably find a prompt that works particularly well. Something like the following:

Summarize the core findings of the following article in 4 concise bullet points. Ensure each bullet point is specific, informative and relevant. Return just the bullet points as plain text. Don't use markdown.

[article body]

Once you're satisfied with the output, you can now integrate the Gemini API into the Android application.

3. Check out the code

Start by checking out the starter source code from GitHub:

git clone https://github.com/android/codelab-gemini-summary

The JetNews app is designed from the ground up to follow Android architecture best practices and to scale well across any mobile device big or small.

Read more about the JetNews implementation approach to support large screens.

If the source code of the JetNews app is overwhelming, don't worry. To add the summarization feature powered by Gemini, you'll only modify the code in the GeminiRepositoryImpl class. The code that triggers summarization and surfaces the summary in the app is already implemented for you.

Final implementation

You can directly access the final implementation of the source code by checking out this branch:

git clone https://github.com/android/codelab-gemini-summary
git switch final

You'll need to follow the instructions detailed in step 4 of this codelab to create and set up your Firebase project.

4. Set up your Firebase project

Create a Firebase project

Create a Firebase project in the Firebase console. Create a new Firebase project by entering a new project name in the first step of the "Create project" workflow. The underlying Google Cloud project is created automatically.

In your project, go to the Build with Gemini page to do the following:

Connect your app to Firebase

Now that you've set up your Firebase project, you need to connect your Android app to that project.

  1. In the Firebase console, select your project.
  2. Click "Add app" and select "Android".
  3. Follow the on-screen instructions to register your app by providing the app's package name.
  4. Download the google-services.json file, and add it into the module (app-level) root directory of your app.
  5. Continue following the on-screen instructions to add the Google services Gradle plugin and the Firebase Bill of Materials (BoM) Gradle dependencies to your app.

Alternative: Use the Firebase Assistant in Android Studio to connect your app

Alternatively, you can use the Firebase Assistant in Android Studio to connect your app to Firebase with a few button clicks.

The Firebase Assistant lets you register your app with a Firebase project, adds the necessary Firebase files, plugins, and dependencies to your Android project – all from within Android Studio!

See the detailed instructions in the Firebase documentation.

5. Update Gradle imports

The JetNews app uses the Gradle version catalogs feature, which helps centralize versions of the dependencies.

  1. To add the Gradle import for the Vertex AI in Firebase SDK, add the following to libs.versions.toml:
    [versions]
    ...
    vertexai = "16.0.0"
    
    
    [libraries]
    ...
    firebase-vertexai = { module = "com.google.firebase:firebase-vertexai", version.ref = "vertexai"}
    
  2. Update build.gradle.kts:
    implementation(libs.firebase.vertexai)
    
  3. Click "Sync now" to fetch the Gradle artifacts.

If you don't want to use Gradle version catalogs, you can directly add the import in build.gradle.kts:

// Import the Firebase BoM
implementation(platform("com.google.firebase:firebase-bom:33.5.1"))
implementation("com.google.firebase:firebase-vertexai")

6. Kotlin integration

Now that the proper dependencies are added, update the Kotlin code.

Open the GeminiRepositoryImpl class. This class will host all the code interacting with the Gemini API.

Gemini model initialization

Instantiate the GenerativeModel:

private val generativeModel = Firebase.vertexAI.generativeModel(
   "gemini-1.5-flash",
   generationConfig = generationConfig {
      temperature = 0f
   },
      safetySettings = listOf(
         SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.LOW_AND_ABOVE),
                SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.LOW_AND_ABOVE),
                SafetySetting(HarmCategory.SEXUALLY_EXPLICIT, HarmBlockThreshold.LOW_AND_ABOVE),
                SafetySetting(HarmCategory.DANGEROUS_CONTENT, HarmBlockThreshold.LOW_AND_ABOVE),
       )
    )

In this example, you're accessing the Gemini 1.5 Flash model. This model is able to handle this example's text summarization use case, and it's more cost-effective than Gemini 1.5 Pro. Learn more about the available Gemini models in the Vertex AI in Firebase documentation.

This example also sets the temperature value to 0, which better ensures consistency in summary generation. Learn more about the model configuration parameters in the Vertex AI in Firebase documentation.

Finally, this example defines safety settings to make sure that the model generates appropriate language in its responses. Learn more about the safety setting features in the Vertex AI in Firebase documentation.

Summarization using Gemini

Next, update the summarizePost() function to summarize the post passed as a parameter.

First, extract the text from the post to pass it to the model:

val postString = StringBuilder()
for (paragraph in post.paragraphs) {
  postString.append(paragraph.text)
}

Create the prompt based on the prompt designed in step #2 of this codelab and passing in the post:

val prompt =
            "Summarize the following article in 4 concise bullet points. " +
                    "Ensure each bullet point is specific, informative and relevant. " +
                    "Return just the bullet points as plain text. " +
                    "Use plain text, don't use markdown. \n $postString"

Finally, pass the prompt to the model and return the response:

return generativeModel.generateContent(prompt).text

Test the implementation

To test the implementation, click "Run app" in Android Studio, and then install the JetNews app on your emulator or a physical device.

Open any story from the home page, and click the summarize button in the bottom bar:

Button triggering the Gemini summarization

You should see a "Gemini summary" section appearing at the top of the article, under the article illustration:

The Gemini summarization section appearing at the top of the article

JetNews app architecture

JetNews was built to showcase the current UI capabilities of Jetpack Compose. It was implemented to support different screen sizes (for details, see this blog post).

The purpose of this codelab isn't to dive in details in the JetNews app architecture. The Gemini integration mostly takes place in the GeminiRepositoryImpl class.

However, if you want to see how the button triggers the summarization, you can take a look at SummaryButton() in JetnewsIcons.kt. You can also review the UI implementation of the summarization UI in the SummarySection() function in PostContent.kt.

7. Conclusion

During this codelab, you learned how to:

  • Create and test a prompt in Vertex AI Studio.
  • Set up a project in Firebase to use Vertex AI in Firebase and the Gemini API.
  • Configure the Gradle dependencies to use Vertex AI in Firebase in an Android app.
  • Call the Gemini API to access the Gemini 1.5 Flash model from Kotlin code.

If you want to explore more about Vertex AI in Firebase for Android apps, you can review the Android code samples and review the documentation.