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

  1. Create your Developer Profile
  2. Download Android Studio
    • Any version is fine - stable one is Otter
  3. Open Android Studio to the “Welcome to Android Studio” screen

Activity 1

  1. Create an Android App using the New Project wizard - “Empty Activity”
  2. Open the preview pane
  3. Alter the text to see the preview update

Activity 2

  1. Create an emulator Create and manage virtual devices
  2. Run your app Run apps on the Android Emulator
  3. (Optional) Run your app on a real device Run apps on a hardware device

Activity 3

  1. Add a Column
  2. Create a TextField
  3. Save the text input to a state
  4. Add a submit Button
  5. Display the state as text on submit
    Example Code

    Example Code - Imports

  6. (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

  1. [Mandatory] Open Firebase Console and create a Firebase project & download a google-services.json file. Add the google-services.json file to your project directory.
  2. Add Google Services Plugin in libs.versions.toml
  3. Add Firebase AI Logic SDK in libs.versions.toml
  4. Add Google Services Plugin in project level build.gradle.kts
  5. Add Google Services Plugin in app level build.gradle.kts
  6. Add Firebase AI Logic SDK in build.gradle.kts
    Example Code


Activity 5

  1. Send the user input to Gemini using:
val generativeModel = Firebase
.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
  1. Display the output
  2. Run the app & try it out
    Example Code

    Example Code - Imports

You will need to create a coroutine scope and wrap the generateContent call in a coroutine which is inside the button’s onClick call

val coroutineScope = rememberCoroutineScope()
coroutineScope.launch {
    result = generativeModel.generateContent(input).text.orEmpty()
}

If needed, add verticalScroll(rememberScrollState()) to the Column modifier to allow it to scroll.

Example prompt:

Recommend one pet for me that is not a usual domesticated animal and why


Activity 6

  1. 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
  2. Join the inputs into a prompt
  3. Display the result
  4. Run the app & try it out
    Example Code

    Example Code - Imports

  5. Modify the model generationConfig & safetySettings to 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)

  1. Update your prompt to request the output in a Json format
    Example Code

  2. Parse the Json using Kotlin Serialization
    Example Code

  3. 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)

  1. Integrate Coil to your app

    Example Code

  2. 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