AI Chatbot App in Android Studio with Gemini and Firebase
Build a chat app where you ask a question and Google Gemini answers word by word. Firebase keeps the secret key safe, so no API key goes in the app. Jetpack Compose and Kotlin, step by step.
What you'll learn
- Connect an Android app to Firebase
- Switch on Firebase AI Logic to use Gemini
- Protect the app with App Check
- Show streaming replies in a Compose chat screen
Before you start
- Android Studio (any recent version)
- A Google account for Firebase
- A phone or emulator with internet
Build it step by step
1. Create the project
In Android Studio choose New Project → Empty Activity (the Compose one), with Minimum SDK API 24. To copy the code unchanged, name it GeminiChatbot with the package name com.example.geminichatbot.
2. Create a Firebase project
Open the Firebase console, click Create a project and follow the steps. Then click the Android icon to add your app, using the same package name.
3. Add google-services.json
Firebase gives you a google-services.json file. Put it in the app folder (next to app/build.gradle.kts). It tells the app which Firebase project to use. Keep it out of public GitHub repositories.
4. Switch on the AI
In the Firebase console open AI Logic, click Get started and choose the Gemini Developer API. Firebase now holds the secret key for Gemini, so no API key goes in the app.
5. Add the libraries
Add the Google services plugin and the Firebase libraries shown in Required setup, then press Sync Now.
6. Start Firebase with the app
ChatApp is an Application class: it runs once when the app opens. It starts Firebase and installs App Check, which proves the requests really come from your app. Register it in AndroidManifest.xml with android:name=".ChatApp", and add the Internet permission.
7. Talk to Gemini
ChatViewModel creates the model with Firebase.ai(backend = GenerativeBackend.googleAI()) and a short system instruction that sets the bot's personality. startChat() remembers the conversation, so the bot knows what you said before.
8. Stream the reply
sendMessageStream returns the answer in small pieces. Each piece is added to the last bubble, so the reply appears word by word, like the big chat apps. Errors show in the chat instead of crashing the app.
9. Build the chat screen
ChatScreen has a header with a New chat button, a LazyColumn of message bubbles (yours on the right, the bot's on the left) and a text box with Send. A LaunchedEffect scrolls down as the reply grows, and imePadding keeps the box above the keyboard.
10. Give Firebase the debug token
Run the app once and open Logcat. Search for DebugAppCheckProvider and copy the debug token. In the Firebase console go to App Check → Apps → Manage debug tokens and add it. Without this step Firebase rejects the requests.
Run it
Ask a question and watch the reply stream in. Ask a follow-up to see that it remembers the conversation, then tap New chat to start over.
The debug App Check provider is only for testing on your own phone. Before publishing to Google Play, switch to the Play Integrity provider.
Files affected
Check what to add and what to replace before copying code.
- app/build.gradle.ktsMerge additions
- build.gradle.kts (the top one)Merge additions
- app/src/main/java/com/example/geminichatbot/MainActivity.ktReplace file
- app/src/main/AndroidManifest.xmlReplace file
- app/src/main/java/com/example/geminichatbot/ChatApp.ktAdd these lines
- app/src/main/java/com/example/geminichatbot/ChatViewModel.ktAdd these lines
Required setup
These are additions to an existing project—not replacement project files.
Add Firebase AI Logic and App Check to the app
Merge additionsWhere: app/build.gradle.kts
The Firebase BoM picks matching versions for every Firebase library. Add these, then Sync Now.
plugins {
id("com.google.gms.google-services")
}
dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.11.0")
implementation(platform("com.google.firebase:firebase-bom:35.1.0"))
implementation("com.google.firebase:firebase-ai")
implementation("com.google.firebase:firebase-appcheck-debug")
}
Add the Google services plugin to the project
Merge additionsWhere: build.gradle.kts (the top one)
It reads google-services.json so the app knows which Firebase project it belongs to.
plugins {
id("com.google.gms.google-services") version "4.5.0" apply false
}
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.geminichatbot. Change that first line to your own package, the one at the top of your MainActivity.
app/src/main/java/com/example/geminichatbot/MainActivity.ktThe chat screen: header with New chat, message bubbles and the box to type in.
package com.example.geminichatbot
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MaterialTheme {
ChatScreen()
}
}
}
}
@Composable
fun ChatScreen(viewModel: ChatViewModel = viewModel()) {
var input by rememberSaveable { mutableStateOf("") }
val listState = rememberLazyListState()
LaunchedEffect(viewModel.messages.size, viewModel.messages.lastOrNull()?.text) {
if (viewModel.messages.isNotEmpty()) {
listState.scrollToItem(viewModel.messages.lastIndex, Int.MAX_VALUE)
}
}
Scaffold { innerPadding ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.imePadding()
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Column(modifier = Modifier.weight(1f)) {
Text(
text = "GEMINI + COMPOSE",
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.primary,
)
Text(
text = "Gemini Chatbot",
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold,
)
}
TextButton(onClick = { viewModel.newChat() }) {
Text("New chat")
}
}
LazyColumn(
state = listState,
modifier = Modifier
.weight(1f)
.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
if (viewModel.messages.isEmpty()) {
item {
Box(
modifier = Modifier.fillParentMaxSize(),
contentAlignment = Alignment.Center,
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text(
text = "Hi there!",
style = MaterialTheme.typography.headlineLarge,
fontWeight = FontWeight.Bold,
)
Text(
text = "How can I help you today?",
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}
}
}
items(viewModel.messages) { message ->
MessageBubble(message)
}
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
OutlinedTextField(
value = input,
onValueChange = { input = it },
modifier = Modifier.weight(1f),
placeholder = { Text("Ask me anything...") },
shape = RoundedCornerShape(24.dp),
)
if (viewModel.isLoading) {
CircularProgressIndicator(modifier = Modifier.padding(start = 8.dp))
} else {
Button(
onClick = {
viewModel.sendMessage(input)
input = ""
},
modifier = Modifier.padding(start = 8.dp),
) {
Text("Send")
}
}
}
}
}
}
@Composable
fun MessageBubble(message: ChatMessage) {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp),
contentAlignment = if (message.isUser) Alignment.CenterEnd else Alignment.CenterStart,
) {
Text(
text = message.text.ifEmpty { "..." },
modifier = Modifier
.widthIn(max = 300.dp)
.background(
color = if (message.isUser) MaterialTheme.colorScheme.primaryContainer
else MaterialTheme.colorScheme.surfaceVariant,
shape = RoundedCornerShape(16.dp),
)
.padding(12.dp),
)
}
}


