بدء Koin في Android وiOS
تهيئة الرسم البياني من نقطة دخول كل منصة
بدء Koin في Android وiOS درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. ✅
تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «بدء Koin في Android وiOS» مجاني؟
نعم — نص درس «بدء Koin في Android وiOS» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «بدء Koin في Android وiOS»؟
تهيئة الرسم البياني من نقطة دخول كل منصة تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «بدء Koin في Android وiOS»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف وحدة Koin مشتركة
- نطاقا single وfactory
- بدء Koin في Android وiOS
- حقن الاعتماديات في ViewModels وRepos