On-Device AI in Android: MediaPipe Tasks & Gemini Nano

Running Artificial Intelligence models directly on mobile hardware is one of the most transformative advances in modern software engineering. Unlike cloud-based AI that incurs server costs and network latency, On-Device AI executes locally on device NPUs and GPUs — delivering instantaneous zero-latency inference, zero server bills, and 100% user privacy.

Android On-Device AI Architecture Stack

Figure 41.1: Android On-Device AI Stack — MediaPipe Tasks and Gemini Nano Accelerated by Hardware NPUs and Neural Networks API (NNAPI).

MediaPipe Tasks: Computer Vision in 10 Lines of Kotlin

Google's MediaPipe Tasks API provides pre-trained on-device vision, text, and audio models ready for production:

import android.content.Context
import android.graphics.Bitmap
import com.google.mediapipe.tasks.vision.core.RunningMode
import com.google.mediapipe.tasks.vision.objectdetector.ObjectDetector
import com.google.mediapipe.tasks.vision.objectdetector.ObjectDetector.ObjectDetectorOptions
import com.google.mediapipe.framework.image.BitmapImageBuilder

class LocalObjectDetector(context: Context) {

    private val detector: ObjectDetector

    init {
        val options = ObjectDetectorOptions.builder()
            .setBaseOptions(
                com.google.mediapipe.tasks.core.BaseOptions.builder()
                    .setModelAssetPath("efficientdet_lite0.tflite")
                    .build()
            )
            .setScoreThreshold(0.5f)
            .setRunningMode(RunningMode.IMAGE)
            .build()

        detector = ObjectDetector.createFromOptions(context, options)
    }

    fun detectObjects(bitmap: Bitmap): List<String> {
        val mpImage = BitmapImageBuilder(bitmap).build()
        val results = detector.detect(mpImage)

        return results.detections().map { detection ->
            val label = detection.categories().firstOrNull()?.categoryName() ?: "Unknown"
            val score = detection.categories().firstOrNull()?.score() ?: 0f
            "$label (${(score * 100).toInt()}%)"
        }
    }
}

Android AICore & Gemini Nano

Starting in modern Android versions, Android AICore is a system-level service that provides direct access to Gemini Nano — Google's most efficient multimodal LLM designed for on-device silicon. Apps can execute Smart Reply, text summarization, proofreading, and audio transcription completely offline without making a single network call.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is an NPU in modern smartphones?

An NPU (Neural Processing Unit) is a specialized hardware silicon processor designed specifically to accelerate matrix multiplications and tensor operations required by deep learning neural networks with minimal battery consumption.

Q2. Do on-device AI models require an active internet connection?

No! Models loaded via MediaPipe or TensorFlow Lite run 100% locally on the device hardware, functioning perfectly in airplane mode or remote areas without cellular reception.

Q3. How large are on-device AI models?

Optimized quantized models (using INT8 or FP16 weights) typically range from 2 MB to 25 MB for computer vision (like object detection or OCR), and 1 GB to 2 GB for compressed on-device LLMs like Gemini Nano.

Explore Related Android Development Classes & Next Steps

Broaden your foundational and advanced Android application development skills across our curriculum:

Image Loading & Bitmap Processing

Convert bitmap images into MediaPipe MPImage tensor inputs.

Coroutines & Background Inference

Run heavy on-device neural network models on Dispatchers.Default.

App Security & Local Privacy

Keep user data 100% private on device with zero cloud transmission.

System Architecture & NNAPI

How Android Neural Networks API accelerates model execution on NPUs.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!