How to Use Gemini AI in Android Development? Use AI in Your Project!

Mert Ozan Kahraman
4 min readMay 7, 2024

Hello everybody, welcome again to another article! In this article, I will try to answer, the “How can I use Gemini AI in my Android project?”. If you are ready, I am too, Let’s dive into Gemini AI and Android Development things.

https://giphy.com/gifs/starwars-movie-star-wars-3ornk57KwDXf81rjWM

Firstly; What is the Gemini AI & API?

Gemini AI is a large language model developed by Google AI. Trained on a massive dataset consisting of text and code, it can generate text, translate languages, create creative content, and answer your questions informatively. Additionally, with the Gemini API, it provides developers with the ability to utilize artificial intelligence. Just like I generated this text using Gemini AI 😄

How to get API Key?

If we want to use Gemini API in our projects, we have to take API key from that link🔗

Select Get API Key from the side menu, then select Generate API Key and choose which of your projects will use the API.

When you fill all fields, you can see genereated API key.

After you got the key, you can open your project from Android Studio. Now we are going to look, how to implement API key to our projects.

https://giphy.com/gifs/reactionseditor-3o7bufPuskODYyUf4I

How to implement API Key?

When we open our project on Android Studio, firstly, let’s start by implementing Gemini AI in our project.

# Module build.gradle

dependencies{

val generativeAiVersion = "0.2.0"

implementation("com.google.ai.client.generativeai:generativeai:$generativeAiVersion")

}

After that, we add our new created API Key to local.properties file to save BuildConfig

# local.properties

sdk.dir=/Users/mertozan/Library/Android/sdk
GEMINI_API_KEY = "Aza*********sago-21v**********w1A"

After that, our API Key is able to use in project.

“Note: If we use API Keys in our projects, we need to hide it from others. Because these keys are unique for you. That’s why we use BuildConfig & local.properties .

Don’t forget to add this file to .gitignore 😄 (Source link 🔗)

When we put API key to project to local.properties with success, now we need to introduce this file to our project. We can do this by using the secret plugin (Source link 🔗) in our module:build.gradle file.

Example code block showing how to add a file to the secrets scope in build.gradle.kts

After all these steps, our Gemini API is ready to taking request.

https://giphy.com/gifs/percolategalactic-star-wars-storm-troopers-maythe4th-26FxzFK4yudFHRFde

How to use Gemini AI in project?

In fact, the Gemini API provides us with an easy-to-use structure. We can send our request with some basic functions.

However, despite using simple functions, thanks to Gemini, we can develop our own language model by customizing language’s safety settings, generationConfig, etc. In addition, we can even do image processing very easily.

Let’s have a look to these -basic- functions.

You can say, “What are all these words?”. Let’s answer these questions.

When we create functions, we need to give a return from the GenerativeModel class. The GenerativeModel class gives us some parameters to customize the AI. If we look at these parameters.

ModelName

Name of the model in the backend on the Gemini side. For example, with “gemini-pro-vision” we can process images, whereas with “gemini-pro” we cannot.

API Key

We give the api key of Gemini API here. Here we call the GEMINI_API_KEY that we saved in BuildConfig.

SafetySettings

  • harassment: This setting blocks content with a high level of harassment, prioritizing user safety.
  • hateSpeech: No content containing hate speech is blocked, allowing for freedom of expression.
  • sexualExplicit: Only content with a high level of sexually explicit material is blocked, maintaining a safe environment.
  • dangerousContent: Both low and high-level dangerous content are blocked, ensuring user safety from harmful material.

Generate Config

The config object represents a configuration for the text generation model. It defines key parameters for the text generation process.

  • temperature: Adjusts text creativity. Higher values mean more randomness, lower values mean more predictability (typically between 0 and 1).
  • topK: Limits word choices to the top most probable words.
  • topP: Refines word choices based on probabilities for diverse and consistent results.

After creating our models, it’s time to write our functions that will make requests through these models.

With all these steps, we are able to access to generated data from Gemini AI.

You can check the Github link below as a sample demo application project.

Github 🐙 project link: https://github.com/meetOzan/GeminiDemo

Thank you for reading my article, I hope you enjoyed it and found it useful.

https://giphy.com/gifs/star-wars-hello-han-solo-ehnBGPgDOusUM

Source

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

--