GEMINI LABJP
SIRI — WWDC 2026 confirms the revamped Siri runs on a Google Gemini model, though it won't ship in the EU at iOS 27 due to the DMAFLASH3.5 — Gemini 3.5 Flash is now GA, the top Flash model for sustained frontier performance on agentic and coding tasksIMAGE-GA — Gemini 3.1 Flash Image and 3.1 Pro Image are GA as native visual models; the preview versions shut down Jun 25MANAGED-AGENTS — Managed Agents launch in public preview in the Gemini API, running autonomous agents in Google-hosted isolated Linux sandboxesFILE-SEARCH — File Search now supports multimodal search, with native image embedding and retrieval via gemini-embedding-2DEPRECATION — gemini-3.1-flash-image-preview and gemini-3-pro-image-preview shut down Jun 25 — migrate to the GA models soonSIRI — WWDC 2026 confirms the revamped Siri runs on a Google Gemini model, though it won't ship in the EU at iOS 27 due to the DMAFLASH3.5 — Gemini 3.5 Flash is now GA, the top Flash model for sustained frontier performance on agentic and coding tasksIMAGE-GA — Gemini 3.1 Flash Image and 3.1 Pro Image are GA as native visual models; the preview versions shut down Jun 25MANAGED-AGENTS — Managed Agents launch in public preview in the Gemini API, running autonomous agents in Google-hosted isolated Linux sandboxesFILE-SEARCH — File Search now supports multimodal search, with native image embedding and retrieval via gemini-embedding-2DEPRECATION — gemini-3.1-flash-image-preview and gemini-3-pro-image-preview shut down Jun 25 — migrate to the GA models soon
Articles/Dev Tools
Dev Tools/2026-04-18Advanced

Gemini API × Kotlin Multiplatform: to Shared AI Logic for iOS and Android

A complete guide to integrating Gemini API with Kotlin Multiplatform (KMP) for shared AI logic across iOS and Android. Covers Gradle setup, Ktor HTTP client, SwiftUI/Compose UI, secure API key management, multimodal image analysis, and production deployment.

kotlin-multiplatformkmpgemini-api285ios12android9cross-platform3mobile4swiftui4jetpack-compose3

Premium Article

When you're building AI features for both iOS and Android, you end up writing the same logic twice — once in Swift, once in Kotlin. What I found more painful than the duplicate code itself was dealing with bug fixes and API changes across two platforms simultaneously.

Kotlin Multiplatform (KMP) changes that equation fundamentally. The Gemini API call logic, response parsing, error handling, and state management — write it once in a shared module, and both iOS and Android run the same code. Native UI experience intact.

This guide walks through integrating Gemini API into a KMP project, from initial setup to production deployment, with working code you can run today.

Why KMP + Gemini, and What to Watch Out For

Why Not Flutter or React Native?

Flutter and React Native are both viable cross-platform options. But when it comes to serious AI feature integration, each has meaningful limitations.

Flutter's Dart SDK for Gemini is more limited compared to the official SDKs (Python, JS, Swift, Kotlin), and Google tends to add features to those first. React Native gives you access to Gemini's JS SDK, but native camera integration and image processing performance can be a bottleneck.

KMP's decisive advantage is that you can use Ktor (Kotlin's KMP-compatible HTTP client) in the shared module to call Gemini's REST API from both iOS and Android. You get native UI with SwiftUI and Jetpack Compose while keeping the AI call logic in one place.

When KMP + Gemini Is the Right Fit

  • Text generation, chat, and summarization via network calls to Gemini API
  • Same business logic across iOS and Android, only UI differs
  • Future plans to expand to Desktop (macOS, Windows) or Web

If you need on-device inference (Gemini Nano) or deep integration with platform-specific ML frameworks, native implementations will serve you better.

Project Setup

Prerequisites

  • Android Studio Giraffe or later (with KMP plugin support)
  • Xcode 15 or later (required for iOS builds)
  • JDK 17 or later
  • Gemini API key (free from Google AI Studio)

Gradle Configuration

Create a new KMP project using Android Studio's "New Project → Kotlin Multiplatform App" wizard. Then update shared/build.gradle.kts to add Gemini API dependencies:

// shared/build.gradle.kts
 
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
 
plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
    kotlin("plugin.serialization") version "2.0.0"
}
 
kotlin {
    androidTarget {
        @OptIn(ExperimentalKotlinGradlePluginApi::class)
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_11)
        }
    }
 
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->
        iosTarget.binaries.framework {
            baseName = "Shared"
            isStatic = true
        }
    }
 
    sourceSets {
        commonMain.dependencies {
            // Ktor HTTP client (KMP compatible across all platforms)
            implementation("io.ktor:ktor-client-core:2.3.12")
            implementation("io.ktor:ktor-client-content-negotiation:2.3.12")
            implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.12")
            // JSON serialization
            implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1")
            // Coroutines
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
        }
        androidMain.dependencies {
            // Ktor engine for Android
            implementation("io.ktor:ktor-client-okhttp:2.3.12")
            implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
        }
        iosMain.dependencies {
            // Ktor engine for iOS (Darwin = Apple platforms)
            implementation("io.ktor:ktor-client-darwin:2.3.12")
        }
    }
}

Why Ktor instead of the official SDK? Google's official Kotlin SDK (com.google.ai.client.generativeai) targets Android/JVM only and can't be used directly in a shared module that includes iOS targets. Using Ktor with Gemini's REST API directly gives you true cross-platform support — iOS, Android, and beyond.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Developers stuck on KMP + Gemini API setup can get working Gradle configuration, HTTP client patterns, and coroutine design today
Learn to separate iOS (SwiftUI) and Android (Jetpack Compose) UIs from shared AI logic with working code covering both platforms in one codebase
Implement secure API key management, rate limiting with retry logic, and multimodal image analysis to ship a production-quality AI-powered cross-platform app
Secure payment via Stripe · Cancel anytime
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API / SDK2026-05-12
Integrating Gemini 3.2 Pro Function Calling into iOS/Android Apps: Design Patterns from 12 Years of Indie Development
A practical guide to integrating Gemini 3.2 Pro Function Calling into iOS and Android apps. Includes working SwiftUI and Kotlin code examples, plus production patterns learned from 12 years of indie development and 50 million app downloads.
Dev Tools2026-04-02
Gemini API × Android Jetpack Compose Complete Integration Guide — Production Design Patterns for Kotlin Native AI Apps
Build native Android AI apps with Kotlin and Jetpack Compose. Covers Google AI SDK, MVVM, multimodal chat UI, Room DB, WorkManager, and production security.
API / SDK2026-05-05
Never Embed Your Gemini API Key in a Mobile App: Complete Multi-Layer Security Architecture with Firebase App Check
A production-grade guide to securing Gemini API access in mobile apps. Covers Firebase App Check, Cloud Functions proxy, rate limiting, and anomaly detection — with complete iOS and Android code examples.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →