Activity Instructions & Code Snippets
Having problems?
In each activity description below there is example code within collapsible sections to help you. I have also included the imports separately in case you can’t find the right one.
If you get stuck or fall behind, here is a the demo app with branches for each Activity. Just check out the activity before the one you want to work on. Demo Android App - Github
Note - you will need to add your own google-services.json file for Activity 4, step 0 (Firebase AI Logic SDK).
Introduction to Android
Preparation work
- Create your Developer Profile
- Download Android Studio
- Any version is fine - stable one is Otter
- Open Android Studio to the “Welcome to Android Studio” screen
Activity 1
- Create an Android App using the New Project wizard - “Empty Activity”
- Open the preview pane
- Alter the text to see the preview update
Activity 2
- Create an emulator Create and manage virtual devices
- Run your app Run apps on the Android Emulator
- (Optional) Run your app on a real device Run apps on a hardware device
Activity 3
- Add a
Column - Create a
TextField - Save the text input to a state
- Add a submit
Button - Display the state as text on submit
Example Code
Example Code - Imports
- (Extension) Add some logic to display an error message if the text field is empty on submit, disable the button if the input is not valid
Example Code
Adding AI with Gemini
Activity 4
- [Mandatory] Open Firebase Console and create a Firebase project & download a
google-services.jsonfile. Add thegoogle-services.jsonfile to your project directory. - Add Google Services Plugin in
libs.versions.toml - Add Firebase AI Logic SDK in
libs.versions.toml - Add Google Services Plugin in project level
build.gradle.kts - Add Google Services Plugin in app level
build.gradle.kts - Add Firebase AI Logic SDK in
build.gradle.ktsExample Code
Activity 5
- Send the user input to Gemini using:
val generativeModel = Firebase
.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
- Display the output
- Run the app & try it out
Example Code
Example Code - Imports
You will need to create a coroutine scope and wrap the
generateContentcall in a coroutine which is inside the button’sonClickcall
val coroutineScope = rememberCoroutineScope()
coroutineScope.launch {
result = generativeModel.generateContent(input).text.orEmpty()
}
If needed, add
verticalScroll(rememberScrollState())to theColumnmodifierto allow it to scroll.
Example prompt:
Recommend one pet for me that is not a usual domesticated animal and why
Activity 6
- Create 3 user inputs that are saved in variables:
- Where do you live? -
home - What are your hobbies? -
hobbies - What is your family like? -
family
- Where do you live? -
- Join the inputs into a prompt
- Display the result
- Run the app & try it out
Example Code
Example Code - Imports
- Modify the model
generationConfig&safetySettingsto produce different results. What produces the most interesting results?Example Code
Example Code - Imports
Example prompt:
Recommend one pet for me that is not a usual domesticated animal, I live in $home, I like to $hobbies and my family is $family.
Activity 7 (Extension)
- Update your prompt to request the output in a Json format
Example Code
- Parse the Json using Kotlin Serialization
Example Code
- Display the result in a formatted way
Example Code
Example Code - Imports
Example prompt:
“Recommend one pet for me that is not a usual domesticated animal, I live in $home, I like to $hobbies and my family is $family. Return the result as a json object that fits the format of this data class: data class Pet( val name: String, val description: String, )”
Example data class:
data class Pet(
val name: String,
val description: String,
)
Activity 8 (Extension)
Integrate Coil to your app
Example Code
Use the pet name and description in a new prompt to generate image of the animal
- You will need to use a model that supports images:
gemini-2.5-flash-image - You will need to adjust your model configuration to produce images:
configBuilder.responseModalities = listOf(
ResponseModality.TEXT,
ResponseModality.IMAGE
)
- To get the image from the result:
val generatedBitmap = generativeModel.generateContent(imagePrompt)
val result = generatedBitmap.candidates.first()
.content.parts.filterIsInstance<ImagePart>()
.firstOrNull()?.image
You may need to add billing to your Firebase account as the free accounts have usage limits for images. Use the free credits provided in this workshop!
Example Code
Example Code - Imports