Google has introduced the new Google AI SDK to simplify the integration of Gemini Pro, its highest performing model to date, into Android apps. This SDK eliminates the need for developers to build and manage their own backend infrastructure.
Google says Gemini Pro is its best model with features for a wide range of text and image inference tasks. Gemini Pro runs off-device within Google’s data centers and is accessible through the Gemini API. According to Google, the easiest way to use Gemini is with Google AI Studio, a web-based tool that allows you to prototype and run prompts in your browser. Once you’re happy with the results, you can export your model to code and use it in your favorite language, such as Python, running on the backend.
For Android apps, Google provides the Google AI Client SDK for Android. It wraps the Gemini REST API into an idiomatic Kotlin API. Using it, developers no longer need to work directly with his REST API or implement server-side services to access Gemini models in Android apps.
The following snippet shows how to generate text from a text-only prompt using the Google AI SDK.
val generativeModel = GenerativeModel(
modelName = "gemini-pro",
apiKey = BuildConfig.apiKey
)
val prompt = "Write a story about a magic backpack."
val response = generativeModel.generateContent(prompt)
print(response.text)
In addition to text-only models, Gemini also offers multimodal models that can generate text from text and image inputs (gemini-pro-vision
) supports streaming for faster interactions. In this case, use: generateContentStream
Instead of generateContent
as shown below:
var fullResponse = ""
generativeModel.generateContentStream(inputContent).collect { chunk ->
print(chunk.text)
fullResponse += chunk.text
}
To further simplify developer workflows, the latest preview of Android Studio introduces new project templates that guide developers through the steps necessary to use Gemini Pro, starting with generating an API key in Google AI Studio It has been.
Besides Gemini Pro, Google also offers Gemini Nano, a smaller model that can run on your device. This enables applications where data must never leave the device and ensures predictable delays even when the network is unavailable. Gemini Nano is now available on select devices through AICore, a new system service for Android 14 aimed at simplifying the incorporation of AI into Android apps by handling model management, runtime, safety, and more. Available at