إعداد البيئة (Kotlin Multiplatform)
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتيح Kotlin Multiplatform (KMP) مشاركة رمز Kotlin على
منصات مختلفة. قبل بدء إنشاء التطبيقات باستخدام KMP، عليك
إعداد بيئتك كما هو موضّح في هذا المستند. يمكنك أيضًا الرجوع إلى المستندات الرسمية الخاصة بـ JetBrain.
تثبيت الأدوات المطلوبة أو تحديثها
- ثبِّت أحدث إصدار ثابت من Android Studio أو حدِّثه.
- يُرجى تحديث مكوّن Kotlin الإضافي المُضمّن في "استوديو Android"
إلى أحدث إصدار لتجنُّب مشاكل التوافق.
- (اختياري) لتطوير التطبيقات على نظام التشغيل iOS، ثبِّت Xcode لإنشاء واجهة مستخدم مثبّتة وأضِف رمز Swift أو Objective-C حسب الحاجة.
إنشاء مشروع Kotlin متوافق مع أنظمة التشغيل المتعددة
يمكنك استخدام معالج Kotlin Multiplatform من JetBrains ل
إنشاء مشروع جديد باستخدام KMP. احرص على اختيار الخيار عدم
مشاركة واجهة المستخدم للحفاظ على واجهة المستخدم الأصلية.
بنية المشروع
تتّبع مشاريع KMP بنية مشروع مشابهة لمشاريع Android.
يحتوي مشروع KMP على وحدات خاصة بالمنصة بالإضافة إلى وحدة مشترَكة.
أضِف الرمز المخصّص للمنصة إلى الوحدة ذات الصلة. على سبيل المثال، أضِف واجهة مستخدم
تطبيق Android في وحدة androidApp وواجهة مستخدم تطبيق iOS في iosApp.
يتم وضع أي رمز تريد مشاركته بين المنصات في الوحدة المشترَكة.
تستخدِم الوحدة المشتركة Gradle كنظام إنشاء تمامًا مثل بقية
المشروع. يمكنك الإفصاح عن التبعيات الشائعة والخاصة بالنظام الأساسي باستخدام
مجموعات المصادر. على سبيل المثال، إذا كان تطبيقك يستخدم Ktor للاتصال بالشبكة، عليك إضافة
تبعية OkHttp لنظام التشغيل Android وتبعية darwin لنظام التشغيل iOS. تجدر الإشارة إلى أنّ بعض
المكتبات لا تتطلّب سوى تبعيات شائعة ولا تحتاج إلى تبعيات
خاصة بالمنصة.
sourceSets {
commonMain.dependencies {
//put your multiplatform dependencies here
//...
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
//...
}
androidMain.dependencies {
implementation(libs.ktor.client.okhttp)
}
iosMain.dependencies {
implementation(libs.ktor.client.darwin)
}
}
عند إضافة مكتبة جديدة إلى الوحدة المشترَكة في تطبيقك، احرص على التحقّق من
التبعيات المطلوبة لكل نظام أساسي.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Setup your environment (Kotlin Multiplatform)\n\n[Kotlin Multiplatform](https://kotlinlang.org/lp/mobile/) (KMP) enables sharing Kotlin code across\ndifferent platforms. Before you start building apps with KMP, you'll need to\nset up your environment as described in this document. You can also refer to\nJetBrain's [official documentation](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-setup.html).\n\nInstall or update required tools\n--------------------------------\n\n- Install or update to the latest stable version of [Android Studio](/studio).\n- Update the [Kotlin plugin](https://kotlinlang.org/docs/releases.html#update-to-a-new-release) that is bundled with Android Studio to the latest version to avoid compatibility issues.\n- (Optional) For iOS development, install [Xcode](https://apps.apple.com/us/app/xcode/id497799835) to build the UI and add Swift or Objective-C code as needed.\n\nCreate a Kotlin Multiplatform project\n-------------------------------------\n\nYou can use the [Kotlin Multiplatform wizard](https://kmp.jetbrains.com/) from JetBrains to\ncreate a new KMP project. Make sure to choose the **Do not\nshare UI** option to keep the UI native.\n\n### Project structure\n\nKMP projects follow a project structure similar to Android projects.\n\nA KMP project contains platform-specific modules along with a shared module.\nAdd your platform-specific code to the relevant module. For example, add your\nAndroid app UI in the **androidApp** module and your iOS app UI in **iosApp** .\nAny code you want to share between platforms goes in the **shared** module.\n\nThe shared module uses Gradle as the build system just like the rest of the\nproject. You can declare common and platform-specific dependencies using\nsourcesets. For example, if your app uses Ktor for networking, you need to add\nan OkHttp dependency for Android and a darwin dependency for iOS. Note that some\nlibraries require only common dependencies and don't need platform-specific\ndependencies. \n\n sourceSets {\n commonMain.dependencies {\n //put your multiplatform dependencies here\n //...\n implementation(libs.ktor.client.core)\n implementation(libs.ktor.client.content.negotiation)\n implementation(libs.ktor.serialization.kotlinx.json)\n //...\n }\n androidMain.dependencies {\n implementation(libs.ktor.client.okhttp)\n }\n iosMain.dependencies {\n implementation(libs.ktor.client.darwin)\n }\n }\n\nWhen you add a new library to your app's shared module, make sure to check for\nthe required dependencies for each platform."]]