0Pricing
Kotlin Multiplatform Academy · Lección

Inyecte implementaciones de plataforma

Proporcione el actual adecuado mediante Koin o factorías.

Inyecte implementaciones de plataforma es una lección gratuita de Kotlin Multiplatform Academy en CoddyKit. Esta es la lección 4 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.

Who Supplies the Real Thing

An interface defines a capability, but something must hand shared code the real platform implementation. That wiring is dependency injection.

Avoid new in Shared Code

Shared logic should not construct platform classes itself. Instead it receives the implementation from outside, keeping it decoupled and testable.

Inject via Constructor

The simplest approach is a constructor parameter. Pass the capability in, and the class never knows which platform built it.

class Profile(private val info: DeviceInfo)

Provide the Android Instance

On Android, the app module creates the real actual and passes it down when it builds the shared object.

val profile = Profile(DeviceInfo())

Provide the iOS Instance

On iOS, Swift constructs the framework's DeviceInfo and injects it the same way, so shared code stays identical across platforms.

let profile = Profile(info: DeviceInfo())

Scale Up With Koin

For many dependencies, a DI library like Koin declares each capability once in a shared module and resolves it on demand.

val appModule = module {
  single<DeviceInfo> { DeviceInfo() }
}

Bind Interface to Actual

In Koin you bind the interface type to the platform implementation, so callers ask for DeviceInfo and get the right concrete one.

single<Storage> { PlatformStorage() }

Resolve Where You Need It

Anywhere in shared code you can pull a dependency from the graph by its type, no manual construction required.

val info: DeviceInfo by inject()

Swap Fakes in Tests

Because everything is injected, a test simply provides a fake implementation. No platform, emulator, or device is involved. ✅

Profile(FakeDeviceInfo())

One Wiring Point per App

Keep injection at each app's entry point. That single setup spot decides which actuals run, while shared code stays blissfully unaware.

Factories for Cheap Cases

If a DI library feels heavy, a plain factory function works too. It builds the platform object in one place and hands it to shared code.

fun deviceInfo(): DeviceInfo = DeviceInfo()

Quick Check

Think about why shared code receives its dependencies.

Recap

You learned to inject platform implementations via the constructor or Koin, binding interfaces to actuals so shared code stays clean and testable. 🎉

Preguntas frecuentes

¿La lección «Inyecte implementaciones de plataforma» es gratis?

Sí — el texto completo de «Inyecte implementaciones de plataforma» 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 «Inyecte implementaciones de plataforma»?

Proporcione el actual adecuado mediante Koin o factorías. 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 4 de 4.

¿Cuánto tiempo toma la lección «Inyecte implementaciones de plataforma»?

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. Diseñe una interfaz de capacidades
  2. expect/actual para la información del dispositivo
  3. Sistema de archivos y rutas por plataforma
  4. Inyecte implementaciones de plataforma
← Volver a Kotlin Multiplatform Academy