Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import org.codehaus.groovy.runtime.DefaultGroovyMethods.step
import org.jetbrains.kotlin.gradle.internal.builtins.StandardNames.FqNames.target

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.gradle.versions)
Expand Down Expand Up @@ -94,7 +91,11 @@ allprojects {
}
format("xml") {
target("**/*.xml")
targetExclude("**/build/**/*.xml", "spotless/**/*.xml")
targetExclude(
"**/build/**/*.xml",
"spotless/**/*.xml",
".idea/**",
)
// Look for the root tag or a tag that is a snippet
licenseHeaderFile(rootProject.file("spotless/copyright.xml"), "(<[a-zA-Z])|(<!--\\s+(//\\s*)?\\[START)").skipLinesMatching(".*START.*")
}
Expand Down
7 changes: 7 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ compose-latest = "1.9.3"
composeUiTooling = "1.5.3"
coreSplashscreen = "1.0.1"
coroutines = "1.10.2"
firebase-bom = "34.3.0"
glide = "1.0.0-beta08"
google-maps = "19.2.0"
gradle-versions = "0.53.0"
guava = "33.5.0-jre"
guava-android = "31.0.1-android"
reactive-streams = "1.0.4"
hilt = "2.57.2"
horologist = "0.8.2-alpha"
junit = "4.13.2"
Expand Down Expand Up @@ -175,11 +178,15 @@ appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat"
coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }
compose-foundation = { module = "androidx.wear.compose:compose-foundation", version.ref = "wearComposeFoundation" }
compose-ui-tooling = { module = "androidx.wear.compose:compose-ui-tooling", version.ref = "composeUiTooling" }
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebase-bom" }
firebase-ai = { module = "com.google.firebase:firebase-ai" }
glide-compose = { module = "com.github.bumptech.glide:compose", version.ref = "glide" }
google-android-material = { module = "com.google.android.material:material", version.ref = "material" }
googlemaps-compose = { module = "com.google.maps.android:maps-compose", version.ref = "maps-compose" }
googlemaps-maps = { module = "com.google.android.gms:play-services-maps", version.ref = "google-maps" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
guava-android = { module = "com.google.guava:guava", version.ref = "guava-android" }
reactive-streams = { module = "org.reactivestreams:reactive-streams", version.ref = "reactive-streams" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
hilt-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
horologist-compose-layout = { module = "com.google.android.horologist:horologist-compose-layout", version.ref = "horologist" }
Expand Down
5 changes: 5 additions & 0 deletions misc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
Expand Down Expand Up @@ -72,6 +73,10 @@ dependencies {
implementation(libs.androidx.startup.runtime)
implementation(libs.androidx.window.java)
implementation(libs.appcompat)
implementation(platform(libs.firebase.bom))
implementation(libs.firebase.ai)
implementation(libs.guava.android)
implementation(libs.reactive.streams)
testImplementation(libs.junit)
testImplementation(kotlin("test"))
androidTestImplementation(libs.androidx.test.ext.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.snippets.ai

import android.content.ContentResolver
import android.graphics.Bitmap
import android.net.Uri
import com.google.firebase.Firebase
import com.google.firebase.ai.ai
import com.google.firebase.ai.type.GenerativeBackend
import com.google.firebase.ai.type.ImagePart
import com.google.firebase.ai.type.ResponseModality
import com.google.firebase.ai.type.content
import com.google.firebase.ai.type.generationConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

object GeminiDeveloperApi25FlashModelConfiguration {
// [START android_gemini_developer_api_gemini_25_flash_model]
// Start by instantiating a GenerativeModel and specifying the model name:
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
// [END android_gemini_developer_api_gemini_25_flash_model]
}

object Gemini25FlashImagePreviewModelConfiguration {
// [START android_gemini_developer_api_gemini_25_flash_image_model]
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "gemini-2.5-flash-image-preview",
// Configure the model to respond with text and images (required)
generationConfig = generationConfig {
responseModalities = listOf(
ResponseModality.TEXT,
ResponseModality.IMAGE
)
}
)
// [END android_gemini_developer_api_gemini_25_flash_image_model]
}

@Suppress("unused")
fun textOnlyInput(scope: CoroutineScope) {
val model = GeminiDeveloperApi25FlashModelConfiguration.model
// [START android_gemini_developer_api_text_only_input]
scope.launch {
val response = model.generateContent("Write a story about a magic backpack.")
}
// [END android_gemini_developer_api_text_only_input]
}

@Suppress("unused")
fun textAndImageInput(scope: CoroutineScope, bitmap: Bitmap) {
val model = GeminiDeveloperApi25FlashModelConfiguration.model
// [START android_gemini_developer_api_multimodal_input]
scope.launch {
val response = model.generateContent(
content {
image(bitmap)
text("what is the object in the picture?")
}
)
}
// [END android_gemini_developer_api_multimodal_input]
}

@Suppress("unused")
fun textAndAudioInput(
scope: CoroutineScope,
contentResolver: ContentResolver,
audioUri: Uri
) {
val model = GeminiDeveloperApi25FlashModelConfiguration.model
// [START android_gemini_developer_api_multimodal_audio_input]
scope.launch {
contentResolver.openInputStream(audioUri).use { stream ->
stream?.let {
val bytes = it.readBytes()

val prompt = content {
inlineData(bytes, "audio/mpeg") // Specify the appropriate audio MIME type
text("Transcribe this audio recording.")
}

val response = model.generateContent(prompt)
}
}
}
// [END android_gemini_developer_api_multimodal_audio_input]
}

@Suppress("unused")
fun textAndVideoInput(
scope: CoroutineScope,
contentResolver: ContentResolver,
videoUri: Uri
) {
val model = GeminiDeveloperApi25FlashModelConfiguration.model
// [START android_gemini_developer_api_multimodal_video_input]
scope.launch {
contentResolver.openInputStream(videoUri).use { stream ->
stream?.let {
val bytes = it.readBytes()

val prompt = content {
inlineData(bytes, "video/mp4") // Specify the appropriate video MIME type
text("Describe the content of this video")
}

val response = model.generateContent(prompt)
}
}
}
// [END android_gemini_developer_api_multimodal_video_input]
}

@Suppress("unused")
fun multiTurnChat(scope: CoroutineScope) {
val model = GeminiDeveloperApi25FlashModelConfiguration.model
// [START android_gemini_developer_api_multiturn_chat]
val chat = model.startChat(
history = listOf(
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
content(role = "model") { text("Great to meet you. What would you like to know?") }
)
)

scope.launch {
val response = chat.sendMessage("How many paws are in my house?")
}
// [END android_gemini_developer_api_multiturn_chat]
}

@Suppress("unused")
fun generateImageFromText(scope: CoroutineScope) {
val model = Gemini25FlashImagePreviewModelConfiguration.model
// [START android_gemini_developer_api_generate_image_from_text]
scope.launch {
// Provide a text prompt instructing the model to generate an image
val prompt =
"A hyper realistic picture of a t-rex with a blue bag pack roaming a pre-historic forest."
// To generate image output, call `generateContent` with the text input
val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt)
.candidates.first().content.parts.filterIsInstance<ImagePart>()
.firstOrNull()?.image
}
// [END android_gemini_developer_api_generate_image_from_text]
}

@Suppress("unused")
fun editImage(scope: CoroutineScope, bitmap: Bitmap) {
val model = Gemini25FlashImagePreviewModelConfiguration.model
// [START android_gemini_developer_api_edit_image]
scope.launch {
// Provide a text prompt instructing the model to edit the image
val prompt = content {
image(bitmap)
text("Edit this image to make it look like a cartoon")
}
// To edit the image, call `generateContent` with the prompt (image and text input)
val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt)
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
// Handle the generated text and image
}
// [END android_gemini_developer_api_edit_image]
}

@Suppress("unused")
fun editImageWithChat(scope: CoroutineScope, bitmap: Bitmap) {
val model = Gemini25FlashImagePreviewModelConfiguration.model
// [START android_gemini_developer_api_edit_image_chat]
scope.launch {
// Create the initial prompt instructing the model to edit the image
val prompt = content {
image(bitmap)
text("Edit this image to make it look like a cartoon")
}
// Initialize the chat
val chat = model.startChat()
// To generate an initial response, send a user message with the image and text prompt
var response = chat.sendMessage(prompt)
// Inspect the returned image
var generatedImageAsBitmap: Bitmap? = response
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
// Follow up requests do not need to specify the image again
response = chat.sendMessage("But make it old-school line drawing style")
generatedImageAsBitmap = response
.candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image
}
// [END android_gemini_developer_api_edit_image_chat]
}
Loading