0Pricing
Kotlin Multiplatform Academy · Lección

Inicie Koin en Android e iOS

Inicialice el grafo desde el punto de entrada de cada plataforma

Inicie Koin en Android e iOS es una lección gratuita de Kotlin Multiplatform Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Kotlin Multiplatform Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

A Module Needs Starting

Declaring modules is only half the job. Each platform must start Koin at launch so the container is ready before anything asks for a dependency.

A Shared Init Helper

Put the common startup in shared code with a small initKoin function. Both platforms call it, so the wiring never drifts apart.

fun initKoin() = startKoin {
    modules(appModule())
}

Pass Extra Platform Config

Give initKoin a parameter so each platform can append its own setup, like Android needing the application context.

fun initKoin(extra: KoinAppDeclaration = {}) =
    startKoin { extra(); modules(appModule()) }

Start Koin on Android

On Android, call initKoin from your Application class so the graph exists for the whole process lifetime.

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        initKoin()
    }
}

Give Android the Context

Android modules often need a Context. Pass it through the extra block using androidContext so definitions can resolve it.

initKoin { androidContext(this@App) }

Register the Application Class

Remember to point your manifest at the custom Application class, or onCreate never runs and Koin never starts.

Start Koin on iOS

On iOS you call the same initKoin through the generated framework, typically from your SwiftUI App init or AppDelegate.

Call It from Swift

Koin exposes a tiny helper so Swift can launch the graph in one clean line at app startup. 🍎

// Swift
KoinKt.doInitKoin()

Start Only Once

Call startKoin a single time per app launch. Starting it twice throws an error, so keep init in one obvious entry point.

Add Logging While Learning

Turn on printLogger during development so Koin prints what it builds, making missing-definition mistakes easy to spot.

startKoin {
    printLogger()
    modules(appModule())
}

One Graph, Two Entry Points

The key idea: the same modules load from two platform entry points. Android uses its Application, iOS uses its App init, both share the wiring.

Quick Check

Where should startup logic live?

Recap

You exposed a shared initKoin, called it from Android's Application and iOS's App init, and learned to start it once with optional logging. The graph is live on both. ✅

Preguntas frecuentes

¿La lección «Inicie Koin en Android e iOS» es gratis?

Sí — el texto completo de «Inicie Koin en Android e iOS» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Kotlin Multiplatform Academy, actualiza a CoddyKit PRO. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Inicie Koin en Android e iOS»?

Inicialice el grafo desde el punto de entrada de cada plataforma Practicas Kotlin Multiplatform Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Kotlin Multiplatform Academy?

No se requiere experiencia previa. Kotlin Multiplatform Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.

¿Cuánto tiempo toma la lección «Inicie Koin en Android e iOS»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Kotlin Multiplatform Academy?

Sí. Cada lección de Kotlin Multiplatform Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Declare un módulo Koin compartido
  2. Scopes single frente a factory
  3. Inicie Koin en Android e iOS
  4. Inyecte dependencias en ViewModels y repositorios
← Volver a Kotlin Multiplatform Academy