◉GEMINI LABJP
●ARGON — gemini-4-argon is generally available as of 10/13, aimed at complex coding, agents, math and science●DEEP RES — deep-research-pro-preview-12-2025 shuts down today (10/23). Move to deep-research-preview-04-2026●NANO — gemini-nano-banana-2.1 is GA (10/06). gemini-3.1-flash-image is deprecated with no shutdown date yet●MCP — One write-up cut an 85,000-token tool result to 310 tokens by wrapping it in an ADK FunctionTool●FLASH — gemini-3.7-flash and 3.5-flash now route automatically to newer Flash models (10/08)●NEW — Stop Re-Reading Every Crash Report: Gre…●ARGON — gemini-4-argon is generally available as of 10/13, aimed at complex coding, agents, math and science●DEEP RES — deep-research-pro-preview-12-2025 shuts down today (10/23). Move to deep-research-preview-04-2026●NANO — gemini-nano-banana-2.1 is GA (10/06). gemini-3.1-flash-image is deprecated with no shutdown date yet●MCP — One write-up cut an 85,000-token tool result to 310 tokens by wrapping it in an ADK FunctionTool●FLASH — gemini-3.7-flash and 3.5-flash now route automatically to newer Flash models (10/08)●NEW — Stop Re-Reading Every Crash Report: Gre…
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-api289ios13android8cross-platform3mobile6swiftui4jetpack-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
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
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-06-23
Integrating Gemini 3.2 Pro Function Calling into iOS/Android Apps: Production Design Patterns
A practical guide to integrating Gemini 3.2 Pro Function Calling into iOS and Android apps. Includes working SwiftUI, Kotlin, and Python code, plus production patterns proven in a real indie wallpaper app — cost, latency, staged rollout, and regression testing.
◈ API / SDK2026-04-03
Gemini API × SwiftUI in Production: Streaming, Multimodal, Error Handling, and App Store Submission
A production-grade guide to integrating the Gemini API into SwiftUI apps at production quality. Covers streaming responses, multimodal input, error handling, test strategies, and App Store submission requirements.
⟐ Dev Tools2026-04-18
Building Cross-Platform AI Desktop Apps with Gemini API and Tauri 2.0
A practical guide to building macOS, Windows, and Linux desktop AI apps using Tauri 2.0 and Gemini API. Learn secure API key management with a Rust backend, real-time streaming via Tauri's event system, and native OS integration with working production code.