IntermediateEnglish14 lectures · 1h 55m

Ship Stable Diffusion in an Android App

ONNX Runtime Mobile in Kotlin and React Native, acceleration, an offline assistant capstone and a benchmark

Watch 3 lectures free

01

What changes after this course

  • ●Instead of waiting for a pipeline object phones do not have, you write the sampling loop and tokenizer and ship image generation in an app
  • ●Instead of choosing native or cross-platform by preference, you choose from a fair benchmark of both
  • ●Instead of hearing about a slow build from users, you set up a performance gate that fails CI

02

Why it matters now

On a phone there is no pipeline object: you write the sampling loop, the tokenizer and the progress reporting yourself. This course ends with a fair dual-track benchmark and a CI performance gate, so you can make it with data.

03

What you leave with

  • ✓An Android native project and a React Native project
  • ✓An offline assistant capstone with checkpoint resume
  • ✓Benchmark hooks and a CI performance gate
  • ✓A LICENSES.md covering SD 1.5, SDXL Turbo and Gemma 2 obligations

04

Skills you'll build

  • →Write a Stable Diffusion sampling loop, Euler scheduler and CLIP tokenizer in Kotlin with ONNX Runtime Mobile
  • →Build the same feature in React Native with a native pixel encoder
  • →Chain an LLM that expands the prompt into the image pipeline, offline, with a safety filter

Inside the course

Screens from the lectures. The notebooks and React Native code on screen are the ones you download; their tables are reference figures and estimates, not results measured in the video. ONNX Runtime, React Native and Stable Diffusion are open third-party tools.

Android project tree with text_encoder.onnx at 246 MB, unet.onnx at 1.7 GB and vae_decoder.onnx at 200 MB, plus a note to download the U-Net on first launch

notebooks/12a_native_android_sd.ipynb

Three ONNX files, and one is too big to bundle

The project tree lists a 246 MB text encoder, a 1.7 GB U-Net and a 200 MB VAE decoder under assets, with a note to download the U-Net on first launch instead of bundling it.

▶ Lecture 1.1 · native sd kickoff project gradle sdengine pipeline
Table of ONNX Runtime execution providers CPU, NNAPI and QNN with ease of use, time for 20 steps, and vendor support

notebooks/12a_native_android_sd.ipynb

Which execution provider to run the U-Net on

The lecture compares CPU, NNAPI and QNN for 20 denoising steps, with the notebook's rough estimates: about 60 s on CPU, 20 to 30 s with one line of NNAPI, and 6 to 10 s with QNN on Snapdragon only.

Lecture 1.3 · viewmodel progress compose screen nnapi vs qnn pitfalls

Python function offline_assistant that expands a prompt with the LLM, checks it with moderate_prompt, generates an image, and prints the time for each step

notebooks/14_project_offline_assistant.ipynb

One prompt through the whole offline app

The capstone chains the LLM and Stable Diffusion: expand the user's prompt, block it if moderation fails, generate the image in 4 steps, and print how long each stage took.

Lecture 4.2 · sdxl turbo safety filter end to end porting rules

Decision tree: what matters most, leading to Native, React Native, Native with QNN, or React Native with a native module

notebooks/15_dual_track_benchmark.ipynb

Choosing a track by what matters most

The course ends with a decision tree: peak performance points to native, shipping iOS and Android together or dev speed points to React Native, and full NPU use points to native with QNN.

Lecture 5.3 · results analysis track decision and course finale

What you'll learn

  • ✓Implement a Stable Diffusion sampling loop, scheduler and tokenizer in Kotlin with ONNX Runtime Mobile
  • ✓Build the same feature in React Native with a native pixel encoder
  • ✓Select execution providers per platform and keep the UI thread responsive
  • ✓Build an offline assistant that chains an LLM prompt expansion into image generation
  • ✓Benchmark native versus React Native fairly and choose a track with data

About this course

On a phone there is no convenient pipeline object: you write the sampling loop, wire the tokenizer, manage tensor lifetimes and stream progress to the user yourself. This course builds that image app twice — in Kotlin and in React Native — and finishes with a capstone and a benchmark.

Track A is Kotlin with ONNX Runtime Mobile: project and Gradle setup, an SdEngine that runs text encoder, U-Net and VAE decoder, the Euler scheduler implemented from the equations, a CLIP byte-pair tokenizer, a ViewModel with streaming progress, a Compose screen, and the NNAPI versus QNN decision. Track B is React Native with onnxruntime-react-native, including a native pixel encoder on Android and iOS and the pain points of the cross-platform path. Then cross-platform acceleration: per-platform execution-provider selection, avoiding JS-thread freezes, iOS Instruments and Perfetto. The capstone chains a small language model that expands the prompt into the image pipeline, with a safety filter and checkpoint resume. The final session is a dual-track benchmark: metrics that are fair, in-app hooks, a CI performance gate, results analysis, and a decision framework for choosing a track.

The code is read by intent, and the narration lists explicitly what must be fixed in the sample code before shipping.

**Licensing — read before you build a business on this.** Putting model weights inside an app is redistribution, so this course ships a `Resources/LICENSES.md` with every asset checked against its actual licence text. Stable Diffusion 1.5 is CreativeML Open RAIL-M: commercial use is allowed, but you must carry its use restrictions through to your own users as an enforceable term. The capstone's image model, **SDXL Turbo, is under the Stability AI Community License — free for commercial use only while you are under US $1,000,000 annual revenue**, and it requires a "Powered by Stability AI" attribution. Gemma 2, which expands the capstone's prompts, requires you to hand every user a copy of the Gemma Terms and to carry its Prohibited Use Policy into your own terms.

Curriculum6 sections · 14 lectures · 1h 55m

Section 0. Welcome

Free preview

Section 1. Track A Native Android Stable Diffusion

Free preview

Section 2. Track B React Native Stable Diffusion

  • 🔒track b kickoff ort rn setup sdservice pipeline8:48
  • 🔒native pixel encoder sd screen track b pain points8:48

Section 3. Cross-Platform Acceleration

  • 🔒rn ep selection fallback chain qnn workarounds8:24
  • 🔒js thread freezes benchmark chart instruments perfetto comparison8:12

Section 4. Capstone Offline Assistant

  • 🔒capstone architecture and llm prompt expansion8:06
  • 🔒sdxl turbo safety filter end to end porting rules8:06
  • 🔒kotlin orchestrator events checkpoint resume8:30

Section 5. Dual-Track Benchmark and Wrap-up

  • 🔒benchmark metrics fairness and logging schema8:06
  • 🔒memory probes adb device farm and ci gate8:24
  • 🔒results analysis track decision and course finale8:18

Requirements

  • · A computer with Python 3.10+ (a free Google Colab account is enough for most sessions)
  • · Comfort reading Python code; you do not need to be an expert
  • · Exported Stable Diffusion components in ONNX form (our Profiling and Export course, or your own)
  • · Android Studio (Track A) and/or a React Native toolchain (Track B)
  • · Kotlin or TypeScript basics; you can follow one track only
  • · Willingness to read `Resources/LICENSES.md` before you ship: the capstone runs on SDXL Turbo, which is free commercially only under US $1M annual revenue

Who this is for

  • · Android and React Native developers adding on-device image generation
  • · Teams comparing a native and a cross-platform stack for AI features
  • · Engineers who want a capstone project rather than isolated snippets

Read alongside the course

    The voice-over in this course is synthesized with a text-to-speech model from scripts written and reviewed by the instructor, and the on-screen material (notebooks, code, slides) is the instructor's own work.

    Ship Stable Diffusion in an Android App | SOTAAZ Blog