Intelligent Apps

Build secure and cost-optimized Android Apps with Firebase AI Logic and Gemini Nano 4

Speakers

  • Ankita Saxena

    Product Manager

  • Arthur Thompson

    Developer Relations Engineer

About

About the talk

Building production-ready generative AI into Android apps requires more than connecting to an LLM—it demands enterprise-grade security and cost controls. In this session, discover how Firebase AI Logic empowers developers to deploy secure, budget-conscious AI experiences at scale.

We will cover from security foundations and Firebase App Check, cost management, dynamic model routing to leveraging the multimodal capabilities.

Summary

  • Firebase AI Logic routes requests from client SDKs directly to Gemini APIs without a custom back end. Server prompt templates store prompts in the cloud to avoid app updates.
  • Firebase App Check protects resources against abuse by ensuring requests originate from your app. It integrates attestation providers like Play Integrity and reCAPTCHA Enterprise before you enforce checks.
  • Gemini Nano 4 executes on-device inference to lower latency and safeguard user privacy. Firebase AI Logic manages fallback routing to the cloud when needed.
  • Spend caps and Firebase AI Logic triggers keep cloud costs predictable. Firebase automatically pauses your AI service at 100% spend, and triggers let you run custom functions before or after generation.
  • Gemini Live API powers bidirectional conversational audio and video streaming. The client SDK manages the underlying WebSocket connections and streaming protocols.

Gemini Notebook

Topics

  • firebase-ai-logic
  • gemini-nano
  • firebase-app-check
  • on-device-ai
  • cost-optimization
  • gemini-live-api

Transcript

Introduction and Firebase AI Logic overview

  • Welcome to Android Developer Summit.

  • I'm Ankita Saxena, Product Manager Firebase.

  • And I'm Arthur Thompson, Developer Relations Engineer on the Firebase team.

  • Today, we'll show you how to add generative features to your Android apps.

  • All right.

  • What is Firebase?

  • Raise your hand for the people who have heard Firebase.

  • Oh, I see a lot.

  • Great.

  • Firebase is Google's app development platform.

  • What Firebase provides here is a unified platform that has 20-plus products integrated together, so you don't have the fragmented experience that you would get with any additional, softwares.

  • Let's talk about the challenges of shipping GenAI to the mobile apps.

  • We have identified top three challenges.

  • First, API key leaks: it takes minutes to extract them.

  • Second, updating model names: it's, we always recommend developers not to hard-code them in your apps, otherwise, every time a new model comes, you have to push another release.

  • Third is file privacy: it becomes very important for any developer or any app user to have their data protected and to have the right guard rails in place for the app.

  • Introducing Firebase AI Logic as the key solution to handle the challenges that we discussed.

  • What is the value proposition that Firebase AI Logic provides to you?

  • We basically take care of all the complex custom back-end, and so that you don't have to, develop it on your end.

  • Second, by using our client SDKs, you can call the Gemini API directly and securely from your mobile apps.

  • Let's look under the hood, and understand the how the architecture flows.

  • On the left side, we have the Firebase client SDKs.

  • We have a wide variety of client SDKs that we support today.

  • I do want to point out Kotlin that we also support.

  • When a developer wants, when you want to perform an AI action, the SDK sends the request to Firebase AI Logic, which then routes it safely to either Gemini Developer API or Agent Platform Gemini API.

  • So, basically we are handling all the custom back-end that you that you're creating today.

  • And the value that we're getting out of it is, you're accelerating the time to market at zero cost.

FriendlyMeals app demo

  • Now the fun part.

  • This is going to be a demo showcasing FriendlyMeals app.

  • On a side note, we will be using the same app for our remaining demos in this session, so feel free to download it.

  • You can scan it using the QR code here.

  • Let's run through the demo.

  • FriendlyMeals app basically helps you identify recipes based on the ingredients, you enter.

  • So, in this workflow, you see, the two ways of, basically, capturing those ingredients.

  • One, you enter the ingredients that you have.

  • Second, you take a picture of the ingredients.

  • I'm going to go and use the first work flow, which is, entering the ingredients.

  • That's all I could find in my fridge.

  • As it this is generating the recipe, I do want to call out a few things.

  • So under the hood, we are calling the Gemini models here.

  • For this specific use case, we are calling three Gemini 3.8 Flash.

  • If you see top, the image, that is actually using Gemini Nano Banana model, which is 2.1 Nano Banana, the latest version.

  • Before we jump into the code, I want to reinforce the value proposition that you get via Firebase AI Logic.

  • So, the first one is the serverless SDKs.

  • You want to add an AI feature, but you don't want to have to stand up a back-end.

  • That's where our client-side SDKs come in.

  • You call directly to Gemini from your Android app.

  • The second one is robust built-in security features.

  • So, your calls to Gemini, you can leverage things like Firebase App Check as well as Server Prompt Templates and others, and we'll talk more about those security features a bit later.

  • Thirdly, is the support for advanced features like multimodal inputs and streaming.

  • So let's say you want to build an app that adds a simple, AI feature to generate a story about a magic backpack.

    import com.google.firebase.Firebase
    import com.google.firebase.ai.ai
    
    @Composable
    fun MagicBackpackGenerator() {
        var output by remember { mutableStateOf("") }
    
        LaunchedEffect(Unit) {
    
           val model=Firebase.ai.generativeModel("gemini-4-argon")
    
            val response = model.generateContent("Write a story about a magic backpack")
    
            output = response.text.toString()
    
        }
        MagicBackpackUI(output)
    }
  • It takes these three simple steps to add that AI feature and have it ready for production.

  • So with Firebase AI Logic making it this easy to add AI features to your app, the first question you should be asking is how are you going to secure this feature?

Security and prompt management

  • So let's jump into the security details, starting with Firebase App Check.

  • Firebase App Check helps you safeguard your data, protect your users, and control who has access to your API resources by attesting that requests are coming from your app.

  • It's an additional layer of security to help prevent your app from being abused.

  • Before getting into the code, there are a couple steps that you need to take in the Firebase console.

  • So under App Check in the Firebase console, you go to register your Android app, and you add the respective attestation providers.

  • So things like Play Integrity or reCAPTCHA Enterprise.

  • Notice that once you start, once you set up App Check, you want to start with unenforced mode.

  • This way, you are allowing your app to provide telemetry to make sure that all of your apps are getting, succeeding API calls to your back end before you enforce.

  • Once you see that that telemetry is coming through nicely, you can then enforce App Check, at which point any requests that don't have a valid, hardware-attested token will be rejected.

  • So here's what the code for implementing App Check look This is what it looks like.

    class FireTravelApp : Application() {
       lateinit var appContainer: AppContainer
           private set
       override fun onCreate() {
           super.onCreate()
           if (BuildConfig.DEBUG) {
               Firebase.appCheck.installAppCheckProviderFactory(
                   DebugAppCheckProviderFactory.getInstance()
               )
           } else {
               Firebase.appCheck.installAppCheckProviderFactory(
                   PlayIntegrityAppCheckProviderFactory.getInstance()
               )
           }
           appContainer = AppContainer(this)
       }
    }
  • In your application class, you install the App Check provider.

  • In the development stages, you would want to use the development the debug provider, and then as you're ready to go to production, you switch to either the Play Integrity or reCAPTCHA Enterprise.

  • Always remember, though, you will want to configure App Check first before you make any other Firebase calls.

  • When building AI features, developer often hard-code prompts directly into their apps code, but this creates two big challenges.

  • One, if there is any improvisation required for the prompt, you have to push the full update.

  • Second, whoever unpacks your app has access to your proprietary prompt engineering.

  • To solve this issue, Firebase AI Logic uses server prompt templates.

  • Instead of keeping your prompts on device, this stores in cloud.

  • This is the view of server prompt templates how it looks in Firebase console.

  • You author the templates in .prompt format.

    ---
    model: 'gemini-3-flash'
    input:
      schema:...
    tools:...
    ---
    {{role "system"}}
    You're a storyteller that tells nice and joyful stories with happy endings.
    
    {{role "user"}}
    Create a story about {{topic}} with the length of {{length}} words in the {{language}} language.
  • You can set the model name right here.

  • You can update it any time.

  • You don't need to push push the full app update.

  • You can update the system instructions as well, and it also takes a strict JSON schema for input and output.

  • Again, no app release is required.

  • Because it lives in cloud, you can tune or tune a prompt or swap a model instantly.

  • And this is how the code This is what the code looks like in Kotlin.

    val generativeModel = Firebase.ai.templateGenerativeModel()
    
    val response = generativeModel.generateContent("storyteller-v1",
        mapOf(
            "topic" to topic,
            "length" to length,
            "language" to language
        )
    )
    
    output.value = response.text
  • A call-out here to mention is server prompt templates is in public preview.

  • You can also implement Auth-only Mode.

  • This uses Firebase Auth user tokens to authorize request.

  • To round out our security features and completely lock down your application, you can also use Template-Only Mode directly from the Firebase console.

AI monitoring and telemetry dashboard

  • And once your app is live, you might want to get visibility into how it is behaving.

  • In the AI Monitoring tab, by just one click, you can enable AI Monitoring and also configure your sampling rate.

  • Firebase will automatically capture metrics, latencies, and token counts without you writing a custom analytics code for that.

  • A call-out to mention the two modes that we just saw, they are all available under the same window.

  • Once you have enabled AI Monitoring on your Firebase console, this feeds directly into the AI Monitoring dashboard.

  • You can observe your 95th 95th-percentile latency, spot sudden error spikes, and track token consumption across different modalities like text, images, and audio.

  • You can also inspect Trace viewer to see certain traces across.

  • So, in this case, as it is showcasing like the diff-- the model that we're using, you can see the latency, success rate.

Hybrid inference with Gemini Nano

  • In the Firebase console, where you can get all of the telemetry and the visibility into how your AI feature is operating.

  • So, in those in that case, let's say that you see a sudden spike in token usage or you see a sudden spike in latency, hybrid is one way to bring those costs down, or to bring that latency down.

  • And as generative AI features become a key part of our our apps, relying 100% on cloud only is probably not the right choice.

  • So, by utilizing Nano 4, on-device models on Android, it unlocks three major benefits, which you might all be familiar with.

  • The first one is offline mode.

  • So, whether you're in a large conference or you're on a plane or in a subway, your AI feature is still going to work.

  • The cost.

  • So, because the inference is running on device, you can add these features and don't have to worry about rising costs for your AI feature.

  • And then the third one is data privacy.

  • So, if it's sensitive data, and you can guarantee to your users that none of the inference is leaving your device.

  • So, using Firebase AI Logic, you can set these inference modes and have clear routing strategies, such as PREFER_ON_DEVICE, PREFER_IN_CLOUD, or ONLY_ON_DEVICE.

  • When you choose ONLY_ON_DEVICE, the SDK taps directly into the on-device model, bypassing the cloud entirely.

  • Let's switch to the phone.

  • So, again, we're going to go to FriendlyMeals, and we're going to take a look at some of the on-device capabilities.

  • So, the first thing we're going to try out is creating a recipe based on some of the ingredients that might be available in our fridge.

  • So, before, you saw Ankita use the, like the manual method by just typing in the ingredients, and then generating the recipe.

  • So, let's try another way.

  • So, let's take this very real refrigerator, totally not generated.

  • There's a lot of olives in this refrigerator, so let's see what type of recipe we get generated here.

  • So, I want to point out a couple of things there.

  • One is that the image never left the device.

  • The image was analyzed locally by Nano by Gemma…

  • Nano 4.

  • And then, also, the second thing is the speed at which the response came back.

  • So, the ingredients was generated.

  • So, you get the two benefits.

  • The data doesn't leave the device, but also there's a significant reduction in latency.

  • All right, so let's see what recipe we get from the olive-filled fridge.

  • So, this time, we are using the cloud.

  • So, we're taking the ingredients that were identified from the fridge and then sending them to the cloud now all using Firebase AI Logic to generate a recipe.

  • I didn't see salmon in the refrigerator, but I see some salmon in the recipe.

  • But That's Let's go back to the slides, please.

  • So, as we saw in the demo, switching between a local model and a cloud model is pretty straightforward.

  • Today, Firebase AI Logic enables inference using basic routing based on network availability.

  • To manage this inference, the You can think of it as guardrails.

  • So, the architecture includes built-in support for features like multimodal and input and output schemas, as well as complete integration with Gemma, sorry, with Gemini Nano 4.

  • So, let's take a look at how these guardrails are implemented in practice.

  • So, the hybrid routing doesn't require you to build complex middleware or to build a lot of the fallback logic yourself.

  • Code-wise, it's just an extra parameter.

  • You pass a parameter an onDeviceConfig to set the mode that you prefer.

    import com.google.firebase.Firebase
    import com.google.firebase.ai.ai
    
    @Composable
    fun MagicBackpackGenerator() {
        var output by remember { mutableStateOf("") }
    
        LaunchedEffect(Unit) {
    
            val model = Firebase.ai.generativeModel(modelName = "gemini-3.8-flash",
    onDeviceConfig = OnDeviceConfig(mode = InferenceMode.PREFER_ON_DEVICE))
    
           val response = model.generateContent("Write a story about a magic backpack")
    
            output = response.text.toString()
    
        }
        MagicBackpackUI(output)
    }
  • The SDK tries In this case, the SDK will try the local model first and if it's not available, it falls back to the cloud automatically.

Cost optimization and AI Logic triggers

  • Now that we have covered how to build secure and-- secure apps and leverage on-device models, let's talk about the cost optimization.

  • Generative features can scale quickly and so your cloud bill.

  • So, we need to have proper guardrails in place to make sure we have the right proactive infrastructure controls and automated features that can help to make your budgets predictable and your applications financially efficient.

  • While Arthur just highlighted that on-device inference is completely free, there are still always going to be the scenarios which requires complex reasoning tasks and also requires fallback and that requires routing to cloud.

  • When those cloud calls happen, you need guardrails to keep your budget predictable.

  • This is That's the very reason where Firebase spend caps come in.

  • With spend caps, you can have more granular control on the cost for your apps.

  • So, instead of a generic, project-wide budget setting, budget alerts that just send you an email, you can actually set a specific budget limit for individual services like Firebase AI Logic.

  • When your spend reaches 100%, Firebase automatically pauses the AI service to prevent any runaway bills.

  • Beyond simple spend caps, there are several advanced cost optimization strategies.

  • The one we would like to highlight today is Firebase AI Logic triggers.

  • What if you need a custom logic in the middle?

  • That's Firebase AI Logic triggers.

  • You can run a function before generate content so, basically, or after it.

  • Use it to check wheth-- check the user, filter input, add context, and also the adjust the response way back.

Gemini Live API and model capabilities

  • So, now we've covered cost optimization and security, let's shift gears to model capability and see what is offered by Firebase AI Logic.

  • To power these multimodal experiences, Firebase AI Logic provides a comprehensive catalog of production-ready models and tools.

  • For high-speed reasoning, text, code, and media, you can use Gemini 4 Argon, Gemini 3.1 Pro, Gemini 3.8 Flash, and others.

  • You can also connect to these models connect these models to real-world data using Search and Maps Grounding.

  • And finally, for voice, we provide natural text-to-speech and Gemini Live audio generation.

  • Let's switch to the phone one more time.

  • So, here we have our sweet and savory.

  • Oh, sorry, not that one.

  • Here we have our Quick Cheesy Ketchup Toast for our recipe.

  • Sounds yummy.

  • And the first thing we want to take a look at is the Live API.

  • So, in this case, we'll be able to use the video that's coming back and I can say, "Hey Gemini, could you tell me what's on the table for the ingredients?"

  • Based on the image on your laptop screen, the ingredients you have are bread, cheddar cheese, and ketchup.

  • All right.

  • So, can I get started with the first step of the recipe?

  • Yes.

  • The first step is to preheat your toaster oven or broiler to medium-high heat.

  • Thank you.

  • All right.

  • So, in that case, it was the Live API and that conversation would remain open until I effectively closed it.

  • The other one I'd like to show is speech-to-text, or text-to-speech, sorry.

  • And with text-to-speech, in this example, we have the recipe instructions.

  • And so, instead of having to read it, I can hit "Generate" or "Speak" and it will send the text of the instructions off to the text-to-speech model.

  • It will generate the audio and send the audio back to the device.

  • I can play it, cache it for later, and it will play as we see here.

  • 1.

  • Preheat toaster oven or broiler to medium-high heat.

  • 2.

  • Place two slices of bread on a baking sheet or toaster tray.

  • Nice.

  • Cool, right?

  • So, Firebase AI Logic allows us to implement these types of cool features with just a few lines of code.

  • Let's switch back to the slides.

  • Now that we have seen in the demonstration how the Gemini Live API works, what if you want to build a hands-free conversational experience yourself?

  • This is where the Gemini Live API comes in, and it allows you to create a bidirectional voice conversations.

  • Your Android app can stream live microphone audio and camera frames upstream and receive natural, low-latency, and spoken voice back in real time.

  • In Kotlin, the setting this up seems pretty familiar.

    val model = Firebase.ai.liveModel(
        modelName = "gemini-3.1-flash-live-preview",
        generationConfig = liveGenerationConfig {
            responseModality = ResponseModality.AUDIO
       },
       systemInstruction = "Your main job is to change the color...",
       tools = listOf(Tool.functionDeclarations(listOf(changeColorFunction)))
    )
    
    val session = model.connect(::handler)
    
    session.startAudioConversation(::handler)
    
    session.stopAudioConversation()
  • You instantiate a live model configured for audio responses, and connect to the session with a stream handler.

  • The number one point that we would like to call out here is this is the SDK handles the complete WebSocket stuff and streaming protocols under the hood.

  • So, you don't have to code it, right?

  • Keep writing lines.

Firebase agent skills and key takeaways

  • As we wrap up our demos and look towards the future of app development, we would like to highlight one powerful tool for your workflow, Firebase Agent Skills.

    # Execute to add agent capabilities to your workspace
    npx skills add firebase/agent-skills
  • Additionally, for those of you using Antigravity and Android Studio, we have already integrated Firebase AI Logic directly into the onboarding flow.

  • To leave you with three key takeaways: start building, lean on the production tools early, like the security ones which we covered today, third is use use agent skills wherever you can.

  • Thank you.

Code

Code snippets

Kotlin snippet at 05:14 in "FriendlyMeals app demo"
import com.google.firebase.Firebase
import com.google.firebase.ai.ai

@Composable
fun MagicBackpackGenerator() {
    var output by remember { mutableStateOf("") }

    LaunchedEffect(Unit) {

       val model=Firebase.ai.generativeModel("gemini-4-argon")

        val response = model.generateContent("Write a story about a magic backpack")

        output = response.text.toString()

    }
    MagicBackpackUI(output)
}
Kotlin snippet at 07:07 in "Security and prompt management"
class FireTravelApp : Application() {
   lateinit var appContainer: AppContainer
       private set
   override fun onCreate() {
       super.onCreate()
       if (BuildConfig.DEBUG) {
           Firebase.appCheck.installAppCheckProviderFactory(
               DebugAppCheckProviderFactory.getInstance()
           )
       } else {
           Firebase.appCheck.installAppCheckProviderFactory(
               PlayIntegrityAppCheckProviderFactory.getInstance()
           )
       }
       appContainer = AppContainer(this)
   }
}
YAML snippet at 08:18 in "Security and prompt management"
---
model: 'gemini-3-flash'
input:
  schema:...
tools:...
---
{{role "system"}}
You're a storyteller that tells nice and joyful stories with happy endings.

{{role "user"}}
Create a story about {{topic}} with the length of {{length}} words in the {{language}} language.
Kotlin snippet at 08:54 in "Security and prompt management"
val generativeModel = Firebase.ai.templateGenerativeModel()

val response = generativeModel.generateContent("storyteller-v1",
    mapOf(
        "topic" to topic,
        "length" to length,
        "language" to language
    )
)

output.value = response.text
Kotlin snippet at 15:09 in "Hybrid inference with Gemini Nano"
import com.google.firebase.Firebase
import com.google.firebase.ai.ai

@Composable
fun MagicBackpackGenerator() {
    var output by remember { mutableStateOf("") }

    LaunchedEffect(Unit) {

        val model = Firebase.ai.generativeModel(modelName = "gemini-3.8-flash",
onDeviceConfig = OnDeviceConfig(mode = InferenceMode.PREFER_ON_DEVICE))

       val response = model.generateContent("Write a story about a magic backpack")

        output = response.text.toString()

    }
    MagicBackpackUI(output)
}
Kotlin snippet at 21:02 in "Gemini Live API and model capabilities"
val model = Firebase.ai.liveModel(
    modelName = "gemini-3.1-flash-live-preview",
    generationConfig = liveGenerationConfig {
        responseModality = ResponseModality.AUDIO
   },
   systemInstruction = "Your main job is to change the color...",
   tools = listOf(Tool.functionDeclarations(listOf(changeColorFunction)))
)

val session = model.connect(::handler)

session.startAudioConversation(::handler)

session.stopAudioConversation()
Shell snippet at 21:30 in "Firebase agent skills and key takeaways"
# Execute to add agent capabilities to your workspace
npx skills add firebase/agent-skills