تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
لإنشاء وحدة Kotlin Multiplatform (KMP) ضمن مشروع Android، استخدِم
نموذج وحدة Kotlin Multiplatform المشتركة المتوفّر في Android Studio
Meerkat و"المكوّن الإضافي لنظام Gradle المتوافق مع Android" الإصدار 8.8.0 والإصدارات الأحدث.
يُنشئ نموذج الوحدة وحدة جديدة تلقائيًا باستخدام الحد الأدنى من الإعدادات التي تستهدف منصّتَي Android وiOS.
إعداد وحدة KMP المشتركة
لإنشاء وحدة KMP مشترَكة، اتّبِع الخطوات التالية:
اختَر ملف > جديد > وحدة جديدة.
اختَر نموذج وحدة مشترَكة لنظام التشغيل Kotlin المتوافق مع أنظمة التشغيل المتعددة في لوحة
النماذج:
الشكل 1. إنشاء وحدة KMP جديدة
في ما يلي الحقول الواردة في النموذج:
اسم الوحدة: يحدِّد اسم وحدة Gradle، بالإضافة إلى اسم إطار عمل iOS (يمكن تغييره لاحقًا).
اسم الحزمة: يحدِّد اسم الحزمة للملفات في هذه الوحدة.
انقر على إنهاء واسمح لـ Gradle بالمزامنة مع المشروع. قد يُطلب منك أيضًا
إضافة ملفات الوحدة التي تم إنشاؤها حديثًا إلى وحدة التحكّم في المصدر.
بعد اكتمال العملية، يعرض عرض المشروع في "استوديو Android" الوحدة المشترَكة الجديدة
بالإضافة إلى مجموعة مصادر لكل نظام أساسي.
الشكل 2. عرض المشروع الذي يعرض وحدات مشتركة جديدة
ربط الوحدة المشترَكة بتطبيق Android
لا يضيف معالج الوحدات الوحدة التي تم إنشاؤها حديثًا كعنصر تابع لأي
وحدة حالية. في الخطوة التالية، عليك ربط الوحدة المشترَكة بإحدى
وحدات Gradle الحالية تمامًا مثل متطلّبات Android الأخرى.
لا يمكن لغة Swift استخدام وحدات Kotlin مباشرةً، وتتطلّب إنشاء إطار عمل ثنائي مُجمَّع.
يضبط نموذج الوحدة الجديد في Android Studio الوحدة المشترَكة لمحاولة
إنشاء إطار عمل لكل بنية من بنى iOS. يمكنك العثور على الرمز التالي في ملف build.gradle.kts الخاص بالوحدة المشترَكة:
لتفعيل الوصول إلى الرمز المشترَك من مشروع iOS، أضِف مرحلة نص برمجي ل
إنشاء إطار عمل Kotlin قبل تجميع مصادر Swift:
انقر بزر الماوس الأيمن على الملف في Android Studio واختَر فتح في وفتح
في التطبيق المرتبط. سيؤدي ذلك إلى فتح تطبيق iOS في Xcode.
الشكل 3. الفتح في التطبيق المرتبط
افتح إعدادات المشروع عن طريق النقر مرّتين على اسم المشروع في ملف
التنقّل بالمشروع.
الشكل 4. مربّع حوار إعدادات مشروع Xcode
غيِّر الاسم التلقائي Run Script (تشغيل النص البرمجي) إلى Compile Kotlin Framework (تجميع إطار عمل Kotlin) لتحديد دور هذه المرحلة بشكلٍ أفضل. انقر مرّتين على عنوان تشغيل النص البرمجي لتعديله.
وسِّع مرحلة الإنشاء، وفي حقل النص Shell، أدخِل رمز الرمز التالي:
الشكل 5. تشغيل مرحلة إنشاء النص البرمجي
اسحب مرحلة تشغيل النص البرمجي قبل مرحلة تجميع المصادر.
الشكل 6. تشغيل مرحلة إنشاء النصوص البرمجية قبل تجميع المصادر
يمكنك إنشاء المشروع في Xcode من خلال النقر على ⌘B أو الانتقال إلى قائمة
المنتج واختيار إنشاء.
عند نجاح عملية الإنشاء، سيظهر لك الرمز التالي.
الشكل 7. اكتمال عملية الإنشاء
الوصول إلى الرمز المشترَك في تطبيق iOS
للتأكّد من أنّ تطبيق iOS يمكنه الوصول إلى الرمز البرمجي من الوحدة المشترَكة بنجاح،
يُرجى اتّباع الخطوات التالية:
في مشروع iOS، افتح ملف ContentView.swift على الرابط التالي:
Sources/View/ContentView.swift
أضِف رمز الاستيراد sharedKit في أعلى الملف.
عدِّل عرض النص لتضمين معلومات Platform_iosKt.platform()
في السلسلة المعروضة على النحو التالي:
يتحقّق هذا التعديل مما إذا كان بإمكان التطبيق استدعاء دالة platform() من
الوحدة المشترَكة، والتي من المفترض أن تعرِض "iOS" عند تشغيلها على نظام التشغيل iOS.
الشكل 8. محاكي Xcode الذي يشغّل تطبيق iOS
مراجع إضافية
إذا كنت مبتدئًا في تطوير تطبيقات KMP، يمكنك الاطّلاع على المستندات الرسمية لـ KMP للحصول على المزيد من الأدلة.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ 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,["# Add Kotlin Multiplatform to an existing project\n\nTo create a Kotlin Multiplatform (KMP) module within your Android project, use\nthe **Kotlin Multiplatform Shared Module** template, available in Android Studio\nMeerkat and Android Gradle Plugin [version 8.8.0](https://maven.google.com/web/index.html?q=build#com.android.tools.build:gradle) and higher.\n\nThe module template automates the creation of a new module with the minimum\nconfiguration targeting Android and iOS platforms.\n| **Note:** If you want to build an iOS app, you need a macOS machine with [Xcode\n| installed](https://developer.apple.com/documentation/safari-developer-tools/installing-xcode-and-simulators). You also need to install an iOS simulator, or have an iPhone [prepared for developing](https://developer.apple.com/documentation/xcode/running-your-app-in-simulator-or-on-a-device#Connect-real-devices-to-your-Mac), to run the app.\n\nSet up the shared KMP module\n----------------------------\n\nTo create a shared KMP module, follow these steps:\n\n1. Select **File \\\u003e New \\\u003e New Module**\n2. Select the **Kotlin Multiplatform Shared Module** template in the **Templates** panel:\n\n**Figure 1.** Creating a new KMP Module\n\nThe fields in the template are the following:\n\n- **Module name** -- defines the Gradle module name, as well as the iOS framework name (can be changed later)\n- **Package name** -- defines the package name for files in this module\n 1. Click **Finish** and allow Gradle to sync with the project. You might also be prompted to add the newly created module files to source control.\n\n| **Note:** The generated module includes some basic placeholder functions and tests. These placeholder functions ensure that the module compiles and runs successfully..\n\nOnce complete, the Android Studio **Project View** shows the new shared module\nalong with a sourceset for each platform.\n**Figure 2.** Project view showing new shared modules\n\n\u003cbr /\u003e\n\nLink the shared module to the Android app\n-----------------------------------------\n\nThe module wizard doesn't add the newly created module as a dependency to any\nexisting module. As a next step, you need to link the shared module to one of\nyour existing Gradle modules similarly to other Android dependencies \n\n dependencies {\n ...\n implementation(project(\":shared\"))\n }\n\n| **Note:** If your project enabled Gradle's [Type-safe project\n| accessors](https://docs.gradle.org/current/userguide/declaring_dependencies_basics.html#sec:type-safe-project-accessors) feature, you can access the shared module with projects.shared.\n\nOnce enabled, you can access code as usual. From the Android app, you're able to\naccess code that is available either in androidMain or in commonMain.\n\nFor more information on Kotlin Multiplatform project structure, see the [basics\nof Kotlin Multiplatform project structure](https://kotlinlang.org/docs/multiplatform-discover-project.html)\n\nSet up the shared module to the iOS app\n---------------------------------------\n\nSwift can't use Kotlin modules directly and requires a compiled binary framework\nto be produced.\n| **Note:** Kotlin Gradle Plugin also supports [Swift Package Manager\n| integration](https://kotlinlang.org/docs/native-spm.html) or [Cocoapods integration](https://kotlinlang.org/docs/native-cocoapods.html) to share and consume the common code built for Kotlin \\& Native (iOS).\n\nThe new module template in Android Studio configures the shared module to\nproduce a framework for each of the iOS architectures. You can find the\nfollowing code in the shared module's `build.gradle.kts` file: \n\n val xcfName = \"sharedKit\"\n\n iosX64 {\n binaries.framework {\n baseName = xcfName\n }\n }\n\n iosArm64 {\n binaries.framework {\n baseName = xcfName\n }\n }\n\n iosSimulatorArm64 {\n binaries.framework {\n baseName = xcfName\n }\n }\n\nSee [Hierarchical project structure](https://kotlinlang.org/docs/multiplatform-hierarchy.html) for information on defining\nother architecture types.\n| **Note:** The output framework is called sharedKit by default, as defined in the shared module's `build.gradle.kts` file. In your own project, you can rename it as needed controlling the `xcfName` variable.\n\nLink the shared library in the iOS project\n------------------------------------------\n\nTo enable access to the shared code from the iOS project, add a script phase to\ngenerate the Kotlin framework before compiling the Swift sources:\n| **Note:** You can also open the Xcode project by double-clicking the **xcodeproj** file in Finder.\n\n1. Right-click the file in the Android Studio and select **Open In** and **Open\n in Associated Application**. This will open the iOS app in Xcode.\n\n**Figure 3.** Open in Associated Application\n\n1. Open the project settings by double-clicking the project name in the project navigator\n\n**Figure 4.** Xcode project settings dialog\n\n1. Change the default **Run Script** name to ***Compile Kotlin Framework*** to better identify what this phase does. Double-click the *Run Script* title to edit it.\n2. Expand the build phase, and in the **Shell** text field, enter the following script code:\n\n**Figure 5.** Run script build phase **Note:** `cd \"$SRCROOT/..\"` navigates the script to the root folder of the KMP Gradle project, which in this case is a parent folder. If in your case the iOS project is on a different path, you need to tweak that here.\n\n1. Drag the **Run Script** phase before the **Compile Sources** phase.\n\n **Figure 6.** Run script build phase before compile sources\n\n \u003cbr /\u003e\n\n2. Build the project in Xcode by clicking **⌘B** or navigating to the\n **Product** menu and selecting **Build**.\n\nWhen the build succeeds, you'll see the following icon.\n**Figure 7.** Build successful\n\nAccess the shared code in the iOS app\n-------------------------------------\n\nTo verify that the iOS app can successfully access code from the shared module,\ndo the following:\n\n1. In the iOS project, open the `ContentView.swift` file at: `Sources/View/ContentView.swift`\n2. Add import `sharedKit` at the top of the file.\n3. Modify the Text view to include the `Platform_iosKt.platform()` information in the displayed string as follows:\n\n| **Note:** The string interpolation in Swift uses (code) syntax.\n\nThis update checks whether the app can call the `platform()` function from the\nshared module, which should return \"iOS\" when running on the iOS platform.\n**Figure 8.** Xcode simulator running the iOS app\n\nAdditional Resources\n--------------------\n\n- If you're new to KMP development, see the [official KMP\n documentation](https://kotlinlang.org/docs/multiplatform.html) for more guides.\n- If you're new to iOS development, see [Swift Basics\n documentation](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/).\n- For common KMP setup issues, see [Possible issues and\n solutions](https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-setup.html#possible-issues-and-solutions).\n- For sample apps, see [Android Kotlin Multiplatform Samples](https://github.com/android/kotlin-multiplatform-samples)."]]