How I created Email Helper Android application using Google Gemini Pro

Generative AI in Application Developement

Preethi Rao
Machine learning for apps
4 min readMay 22, 2024

--

Google has recently launched the Gemini Pro Flash-APIs, providing support for all Android and iOS application developers. This development has created new opportunities for app developers, marking a significant advantage of Google Gemini over OpenAI. Unlike OpenAI, which lacks official library support and presents challenges for integration especially for Applications, Google Gemini offers a more accessible and developer-friendly solution.

If you are also willing to develop an AI application or willing to integrate AI features in any of your application please refer this official page for more documentation.

Lets go through Sample application which I created , this will help to you to write your emails using Google Gemini Pro.

This application has 2 main feature

  • You can choose email topic from the existing templates
    or
  • You can just write the context of the email

The email is will be auto generated for you, then you can share this email directly with the email clients(eg: gmail, microsoft outlooks)you have installed in your phone.

Lets go through step by step how this application was built

Step 1: Get an API key:

you need to get an API key in-order to access Google Gemini Pro API, you can go to this official site to get a api key , (keep in mind don’t expose your api key pubically)
Free version of the Gemini Pro 1.5 flash has following benefits
Rate Limits
- 15 RPM (requests per minute)
- 1 million TPM (tokens per minute)
- 1,500 RPD (requests per day)
Paid Version Benifits (pay as you use)
-
360 RPM (requests per minute)
- 10 million TPM (tokens per minute)
- 10,000 RPD (requests per day)
You can refer the following site for more inputs.

Step 2: add the dependency in your android application:

once you get the API in your Android Studio create a new Project
File -> New Project you will be able to see new template for Gemini Project

You will be asked to paste your api key at the Project Console itself

if you dont paste your keys here, you might have to add it later in local.properties

apiKey=your_api_key_here

There are few things to consider while integrating your api key inside you application, as you need to keep them secure.

Lets go through the dependency library that is required to access Gemini API

dependencies {
// ... other androidx dependencies

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

Accessing Generative AI model of Gemini Pro

val generativeModel = GenerativeModel(
// Use a model that's applicable for your use case (see "Implement basic use cases" below)
modelName = "gemini-1.0-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = BuildConfig.apiKey
)

when user gives the context for the email, I access this specific method in the generativeModel

 fun generateEmail(contextText: String?) {
inputText?.let {
_uiState.value = EmailGenerationUiState.Loading
val prompt = "Write an Email regarding: $contextText"
viewModelScope.launch {
try {
var outputContent = ""
generativeModel.generateContentStream(prompt)
.collect { response ->
outputContent += response.text
_uiState.value = EmailGenerationUiState.Success(outputContent)
}
Caution: The Google AI client SDK for Android is recommended for prototyping only. For non-prototyping use cases, we strongly recommend that you use a backend SDK to access the Google AI Gemini API. If you embed your API key directly in your Android app or fetch it remotely at runtime, you risk potentially exposing your API key to malicious actors.} catch (e: Exception) {
_uiState.value = EmailGenerationUiState.Error(e.localizedMessage ?: "")
}
}
}
}

This is a very simple application for now, but you can make it more user friendly by adding additional functionality, here is the github sample you more detailed implementation.

Drawback:

  • currently the Gemini Pro API is not enabled for all the countries.
  • Caution: The Google AI client SDK for Android is recommended for prototyping only. (For non-prototyping use cases, you need to use a backend SDK to access the Google AI Gemini API. If you embed your API key directly in your Android app or fetch it remotely at runtime, you risk potentially exposing your API key to malicious actors)

But I believe these are temporary limitations which will be resolved by google soon.

If you like this post, dont forget to follow me on LinkedIn for more such post

--

--