Android AI Samples

repository·main·Indexed 20 days ago

https://github.com/android/ai-samples

A collection of official Android sample applications and modules demonstrating the integration of Generative AI into development workflows. The repository includes the Android AI Sample Catalog and various implementations covering hybrid and cloud inference (Gemini Pro, Gemini Flash), multimodal and image generation (Nano Banana), on-device AI via ML Kit (summarization, image description, writing assistance), and video analysis. Samples utilize the Firebase AI SDK and Gemini Live API for voice-based interactions.

Tokens
10.9K
Snippets
23
Records
50
Agent score
70%

What's inside android-ai-samples

  1. Overview of the Magic Selfie Sample

    main
    The Magic Selfie Sample demonstrates semantic image editing using the Nano Banana 2 (gemini-3.1-flash-image) model. It allows users to replace the background of a photo with a generated image based on a text prompt while preserving the original subject. This is achieved through multimodal prompting (image + text) via the Firebase AI SDK for Android.
  2. Overview of Android AI Samples

    main
    The android/ai-samples repository provides official sample applications and developer tools for integrating Generative AI into Android apps. It demonstrates how to use both on-device and cloud-based models for tasks such as hybrid inference, chat, multimodal interactions, summarization, writing assistance, and more.
  3. Explore Android AI Samples

    main

    The /samples directory contains various implementations of generative AI features on Android. These samples cover several key patterns:

    Hybrid & Cloud Inference

    • Hybrid Inference: Demonstrates using both on-device (Gemini Nano via ML Kit) and cloud-based (Gemini via Firebase AI SDK) models, with a fallback mechanism to the cloud when on-device capabilities are unavailable. [Code: samples/gemini-hybrid]
    • Gemini Chatbot: A chatbot using the Gemini Flash model. Supports customizing system instructions to modify model tone or persona. [Code: samples/gemini-chatbot]
    • Gemini Live API To-do App: Uses the Gemini Live API for voice-based interaction to manage a to-do list. [Code: samples/gemini-live-todo]

    Multimodal & Image Generation

    • Gemini Image Chat: Uses the Gemini 3 Pro Image model ("Nano Banana Pro") for image generation and iterative tweaks via conversation. [Code: samples/gemini-image-chat]
    • Gemini Multimodal: Leverages Gemini Flash for text and image-to-text prompting. [Code: samples/gemini-multimodal]
    • Nanobanana: Uses the Gemini 3.1 Flash Image model ("Nano Banana") to generate artistic landscapes, objects, and people. [Code: samples/nanobanana]
    • Magic Selfie: Combines the ML Kit subject Segmentation SDK to remove backgrounds with Nano Banana to generate new ones. [Code: samples/magic-selfie]

    On-device AI (Gemini Nano via ML Kit)

    Video Analysis

    • Gemini Video Summarization: Uses Gemini Flash to summarize videos with large file support. [Code: samples/gemini-video-summarization]
    • Gemini Video Metadata Creation: Uses Gemini Flash to generate thumbnails, descriptions, hashtags, and chapters by providing a YouTube video link in the model context. [Code: samples/gemini-video-metadata-creation]
  4. Gemini Video Summarization Sample Overview

    main
    This sample demonstrates how to generate text summaries from video content using the Gemini Flash model. It allows users to select a video, which the generative model then analyzes to provide a concise summary of the key information. The sample is part of the larger AI Sample Catalog and requires cloning the entire repository to build and run.
  5. Use the Gemini Live API for voice-based task management

    main

    The Gemini Live Todo sample demonstrates real-time, voice-based interactions using the Gemini Live API. It allows users to manage a to-do list (add, remove, update tasks) through hands-free, conversational commands. The implementation relies on the Firebase AI SDK for Android to interact with the gemini-2.5-flash-native-audio-preview-12-2025 model.

    val generativeModel = Firebase.ai(backend = GenerativeBackend.vertexAI()).liveModel(
        "gemini-2.5-flash-native-audio-preview-12-2025",
        generationConfig = liveGenerationConfig,
        systemInstruction = systemInstruction,
        tools = listOf(
            Tool.functionDeclarations(
                listOf(getTodoList, addTodo, removeTodo, toggleTodoStatus),
            ),
        ),
    )
    
    try {
        session = generativeModel.connect()
    } catch (e: Exception) {
        Log.e(TAG, "Error connecting to the model", e)
        liveSessionState.value = LiveSessionState.Error
    }
  6. Overview of JetPacker Architecture

    main

    JetPacker is a multi-module Android application for trip management. It uses a clean architecture organized by responsibility:

    • UI: Jetpack Compose
    • Dependency Injection: Dagger/Hilt
    • Local Persistence: Room Database
    • State Management: ViewModels with StateFlow
    • On-Device AI: ML Kit GenAI (Prompt, Speech Recognition, Translation)
    • Cloud & Hybrid AI: Firebase AI Logic (Gemini grounded with URL/Maps/Search)
    • App Security: Firebase App Check
    • Assistant Integration: Android AppFunctions (androidx.appfunctions)
  7. Implement function calling with Gemini Live

    main

    To enable the Gemini model to perform actions (like managing a to-do list), you must provide a set of functionDeclarations via the tools parameter when initializing the liveModel. In this sample, the model is given access to the following functions to manage state:

    • getTodoList
    • addTodo
    • removeTodo
    • toggleTodoStatus

    When a user speaks a command, the model processes the audio and executes these corresponding functions to interact with the application logic.

    tools = listOf(
        Tool.functionDeclarations(
            listOf(getTodoList, addTodo, removeTodo, toggleTodoStatus),
        ),
    )
  8. Use ML Kit GenAI for proofreading and rewriting

    main

    This sample demonstrates on-device text manipulation using ML Kit GenAI APIs powered by Gemini Nano. It provides two primary capabilities:

    1. Proofreading: Correcting grammar and spelling errors.
    2. Rewriting: Changing the style of the input text.

    The core implementation uses Proofreader and Rewriter clients. When performing inference, the APIs may return multiple results ordered by confidence; the highest quality result is typically the first element in the results list.

    // Example of running proofreading inference
    private suspend fun runProofreadingInference(textToProofread: String) {
        val proofreadRequest = ProofreadingRequest.builder(textToProofread).build()
        
        // Results are returned in descending order of quality/confidence.
        // We use the first result (index 0) for the highest quality.
        val results = proofreader.runInference(proofreadRequest).await()
        val polishedText = results.results[0].text
    }