Skip to content

Commit 897a3b5

Browse files
authored
add first batch of Kotlin snippets for Firebase AI Logic (#630)
1 parent 78b8f43 commit 897a3b5

35 files changed

+1173
-0
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[*.{java,kt}]
22
max_line_length = 120
3+
indent_size = 2

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ plugins {
1010
id("com.google.firebase.firebase-perf") version "1.4.2" apply false
1111
id("androidx.navigation.safeargs") version "2.9.0" apply false
1212
id("com.github.ben-manes.versions") version "0.52.0" apply true
13+
alias(libs.plugins.composeCompiler) apply false
1314
}
1415

1516
allprojects {

firebase-ai/.gitignore

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

firebase-ai/build.gradle.kts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
plugins {
2+
alias(libs.plugins.androidApplication)
3+
alias(libs.plugins.jetbrainsKotlinAndroid)
4+
alias(libs.plugins.composeCompiler)
5+
}
6+
7+
android {
8+
namespace = "com.google.firebase.example.ai"
9+
compileSdk = 36
10+
11+
defaultConfig {
12+
applicationId = "com.google.firebase.example.ai"
13+
minSdk = 21
14+
targetSdk = 36
15+
versionCode = 1
16+
versionName = "1.0"
17+
18+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
19+
}
20+
21+
buildTypes {
22+
release {
23+
isMinifyEnabled = false
24+
proguardFiles(
25+
getDefaultProguardFile("proguard-android-optimize.txt"),
26+
"proguard-rules.pro"
27+
)
28+
}
29+
}
30+
compileOptions {
31+
sourceCompatibility = JavaVersion.VERSION_11
32+
targetCompatibility = JavaVersion.VERSION_11
33+
}
34+
kotlinOptions {
35+
jvmTarget = "11"
36+
}
37+
buildFeatures {
38+
compose = true
39+
}
40+
}
41+
42+
dependencies {
43+
44+
implementation(libs.androidx.core.ktx)
45+
implementation(libs.androidx.lifecycle.runtime.ktx)
46+
implementation(libs.androidx.activity.compose)
47+
implementation(platform(libs.androidx.compose.bom))
48+
implementation(libs.androidx.ui)
49+
implementation(libs.androidx.ui.graphics)
50+
implementation(libs.androidx.ui.tooling.preview)
51+
implementation(libs.androidx.material3)
52+
53+
implementation(libs.firebase.ai)
54+
55+
testImplementation(libs.junit)
56+
androidTestImplementation(libs.androidx.junit)
57+
androidTestImplementation(libs.androidx.espresso.core)
58+
androidTestImplementation(platform(libs.androidx.compose.bom))
59+
androidTestImplementation(libs.androidx.ui.test.junit4)
60+
debugImplementation(libs.androidx.ui.tooling)
61+
debugImplementation(libs.androidx.ui.test.manifest)
62+
}

firebase-ai/proguard-rules.pro

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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<application
5+
android:allowBackup="true"
6+
android:icon="@mipmap/ic_launcher"
7+
android:label="@string/app_name"
8+
android:roundIcon="@mipmap/ic_launcher_round"
9+
android:supportsRtl="true"
10+
android:theme="@style/Theme.Snippetsandroid">
11+
<activity
12+
android:name=".MainActivity"
13+
android:exported="true"
14+
android:label="@string/app_name"
15+
android:theme="@style/Theme.Snippetsandroid">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.google.firebase.example.ai
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.activity.enableEdgeToEdge
7+
import androidx.compose.foundation.layout.fillMaxSize
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.material3.Scaffold
10+
import androidx.compose.material3.Text
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.tooling.preview.Preview
14+
import com.google.firebase.example.ai.ui.theme.SnippetsandroidTheme
15+
16+
class MainActivity : ComponentActivity() {
17+
override fun onCreate(savedInstanceState: Bundle?) {
18+
super.onCreate(savedInstanceState)
19+
enableEdgeToEdge()
20+
setContent {
21+
SnippetsandroidTheme {
22+
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
23+
Greeting(
24+
name = "Android",
25+
modifier = Modifier.padding(innerPadding)
26+
)
27+
}
28+
}
29+
}
30+
}
31+
}
32+
33+
@Composable
34+
fun Greeting(name: String, modifier: Modifier = Modifier) {
35+
Text(
36+
text = "Hello $name!",
37+
modifier = modifier
38+
)
39+
}
40+
41+
@Preview(showBackground = true)
42+
@Composable
43+
fun GreetingPreview() {
44+
SnippetsandroidTheme {
45+
Greeting("Android")
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.google.firebase.example.ai.googleai
2+
3+
import com.google.firebase.Firebase
4+
import com.google.firebase.ai.ai
5+
import com.google.firebase.ai.type.GenerativeBackend
6+
7+
class FirebaseAILogic {
8+
9+
private fun initialization() {
10+
// [START initialize_googleai_and_model]
11+
// Initialize the Gemini Developer API backend service
12+
// Create a `GenerativeModel` instance with a model that supports your use case
13+
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
14+
.generativeModel("gemini-2.0-flash")
15+
// [END initialize_googleai_and_model]
16+
}
17+
18+
19+
}

0 commit comments

Comments
 (0)