Skip to content

Commit ce1e4d3

Browse files
Add hybrid SDK sample
1 parent 8e6dacb commit ce1e4d3

File tree

13 files changed

+928
-1
lines changed

13 files changed

+928
-1
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ dependencies {
9393
implementation(project(":samples:gemini-live-todo"))
9494
implementation(project(":samples:gemini-video-metadata-creation"))
9595
implementation(project(":samples:gemini-image-chat"))
96+
implementation(project(":samples:gemini-hybrid"))
9697

9798
testImplementation(libs.junit)
9899
androidTestImplementation(libs.androidx.junit)

app/src/main/java/com/android/ai/catalog/domain/SampleCatalog.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ import com.android.ai.samples.geminivideosummary.ui.VideoSummarizationScreen
3131
import com.android.ai.samples.genai_image_description.GenAIImageDescriptionScreen
3232
import com.android.ai.samples.genai_summarization.GenAISummarizationScreen
3333
import com.android.ai.samples.genai_writing_assistance.GenAIWritingAssistanceScreen
34+
import com.android.ai.samples.geminihybrid.GeminiHybridScreen
3435
import com.android.ai.samples.imagen.ui.ImagenScreen
3536
import com.android.ai.samples.imagenediting.ui.ImagenEditingScreen
3637
import com.android.ai.samples.magicselfie.ui.MagicSelfieScreen
3738
import com.android.ai.theme.extendedColorScheme
39+
import com.google.firebase.ai.type.PublicPreviewAPI
3840

41+
@OptIn(PublicPreviewAPI::class)
3942
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
4043
val sampleCatalog = listOf(
4144
SampleCatalogItem(
@@ -146,6 +149,15 @@ val sampleCatalog = listOf(
146149
needsFirebase = true,
147150
keyArt = R.drawable.img_keyart_todo,
148151
),
152+
SampleCatalogItem(
153+
title = R.string.gemini_hybrid_sample_list_title,
154+
description = R.string.gemini_hybrid_sample_list_description,
155+
route = "GeminiHybridScreen",
156+
sampleEntryScreen = { GeminiHybridScreen() },
157+
tags = listOf(SampleTags.GEMINI_NANO, SampleTags.GEMINI_FLASH, SampleTags.ML_KIT, SampleTags.FIREBASE),
158+
needsFirebase = true,
159+
keyArt = R.drawable.img_keyart_chatbot,
160+
),
149161

150162
// To create a new sample entry, add a new SampleCatalogItem here.
151163
)

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<string name="gemini_live_todo_list_description">"Simple to-do app using the Gemini Live API to interact with the items in the list"</string>
2828
<string name="gemini_image_chat_list_title">Chat with Nano Banana Pro</string>
2929
<string name="gemini_image_chat_list_description">Conversational Image generation with Gemini 3 Pro Image</string>
30+
<string name="gemini_hybrid_sample_list_title">Gemini Hybrid</string>
31+
<string name="gemini_hybrid_sample_list_description">Inference with Firebase Hybrid SDK using either Gemini Nano on-device or Gemini Flash in the Cloud.</string>
3032
<string name="firebase_required">Firebase Required</string>
3133
<string name="firebase_required_description">This feature requires Firebase to be initialized.</string>
3234
<string name="close">Close</string>

gradle/libs.versions.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[versions]
22
agp = "8.8.2"
33
coilCompose = "3.1.0"
4+
firebaseAiOndevice = "16.0.0-beta01"
45
firebaseBom = "34.5.0"
56
lifecycleRuntimeCompose = "2.9.1"
67
mlkitGenAi = "1.0.0-beta1"
@@ -25,6 +26,7 @@ mlkitSegmentation = "16.0.0-beta1"
2526
runtimeLivedata = "1.7.6"
2627
media3 = "1.8.0"
2728
firebaseCommonKtx = "21.0.0"
29+
firebaseAi ="17.10.0"
2830
uiToolingPreviewAndroid = "1.8.1"
2931
spotless = "7.0.4"
3032
uiToolingPreview = "1.8.3"
@@ -40,8 +42,10 @@ richtext = "1.0.0-alpha02"
4042
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
4143
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycleRuntimeCompose" }
4244
coil-compose = { module = "io.coil-kt.coil3:coil-compose", version.ref = "coilCompose" }
45+
firebase-ai-ondevice = { module = "com.google.firebase:firebase-ai-ondevice", version.ref = "firebaseAiOndevice" }
46+
firebase-ai-ondevice-interop = { module = "com.google.firebase:firebase-ai-ondevice-interop", version.ref = "firebaseAiOndevice" }
4347
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" }
44-
firebase-ai = { group = "com.google.firebase", name = "firebase-ai" }
48+
firebase-ai = { module = "com.google.firebase:firebase-ai", version="firebaseAi"}
4549
firebase-common-ktx = { group = "com.google.firebase", name = "firebase-common-ktx", version.ref = "firebaseCommonKtx" }
4650
genai-image-description = { module = "com.google.mlkit:genai-image-description", version.ref = "mlkitGenAi" }
4751
genai-proofreading = { module = "com.google.mlkit:genai-proofreading", version.ref = "mlkitGenAi" }

samples/gemini-hybrid/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

samples/gemini-hybrid/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Gemini Hybrid Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates a hybrid approach to generative AI, utilizing both on-device (Gemini Nano via ML Kit) and cloud-based (Gemini via Firebase AI SDK) models. It showcases how to fallback to the cloud when on-device capabilities are unavailable or when more complex reasoning is required.
8+
9+
## How it works
10+
11+
The application first attempts to perform a task (e.g., summarization) using the on-device Gemini Nano model through the ML Kit GenAI API. If the model is not supported on the device or fails to download, it seamlessly falls back to the Gemini Flash model in the cloud using the Firebase AI SDK.
12+
13+
### Key Snippets
14+
15+
#### On-Device Inference (ML Kit)
16+
```kotlin
17+
val summarizer = Summarization.getClient(options)
18+
val featureStatus = summarizer.checkFeatureStatus().await()
19+
if (featureStatus == FeatureStatus.READY) {
20+
summarizer.runInference(request) { ... }.await()
21+
}
22+
```
23+
24+
#### Cloud Inference (Firebase AI)
25+
```kotlin
26+
val generativeModel = Firebase.ai.generativeModel("gemini-1.5-flash")
27+
val response = generativeModel.generateContent(prompt)
28+
```
29+
30+
Read more about [Gemini on Android](https://developer.android.com/ai/gemini) in the official documentation.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
alias(libs.plugins.android.library)
19+
alias(libs.plugins.jetbrains.kotlin.android)
20+
alias(libs.plugins.ksp)
21+
alias(libs.plugins.compose.compiler)
22+
}
23+
24+
android {
25+
namespace = "com.android.ai.samples.geminihybrid"
26+
compileSdk = 35
27+
28+
buildFeatures {
29+
compose = true
30+
}
31+
32+
defaultConfig {
33+
minSdk = 26
34+
35+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
36+
consumerProguardFiles("consumer-rules.pro")
37+
}
38+
39+
buildTypes {
40+
release {
41+
isMinifyEnabled = false
42+
proguardFiles(
43+
getDefaultProguardFile("proguard-android-optimize.txt"),
44+
"proguard-rules.pro",
45+
)
46+
}
47+
}
48+
compileOptions {
49+
sourceCompatibility = JavaVersion.VERSION_17
50+
targetCompatibility = JavaVersion.VERSION_17
51+
}
52+
kotlinOptions {
53+
jvmTarget = "17"
54+
}
55+
}
56+
57+
dependencies {
58+
implementation(libs.androidx.core.ktx)
59+
implementation(libs.androidx.appcompat)
60+
implementation(libs.androidx.material3)
61+
implementation(libs.androidx.activity.compose)
62+
implementation(platform(libs.androidx.compose.bom))
63+
implementation(libs.androidx.material.icons.extended)
64+
implementation(libs.hilt.android)
65+
implementation(libs.hilt.navigation.compose)
66+
implementation(libs.androidx.runtime.livedata)
67+
implementation(libs.genai.summarization)
68+
implementation(libs.kotlinx.coroutines.guava)
69+
implementation(libs.androidx.lifecycle.runtime.compose)
70+
implementation(platform(libs.firebase.bom))
71+
implementation(libs.firebase.ai)
72+
implementation(libs.firebase.ai.ondevice)
73+
implementation(libs.firebase.ai.ondevice.interop)
74+
75+
implementation(project(":ui-component"))
76+
debugImplementation(libs.ui.tooling)
77+
ksp(libs.hilt.compiler)
78+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

0 commit comments

Comments
 (0)