הגדרת הסביבה (Kotlin Multiplatform)
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
Kotlin Multiplatform (KMP) מאפשרת לשתף קוד Kotlin בפלטפורמות שונות. לפני שמתחילים לפתח אפליקציות באמצעות KMP, צריך להגדיר את הסביבה כפי שמתואר במסמך הזה. אפשר גם לעיין במסמכי העזרה הרשמיים של JetBrain.
התקנה או עדכון של הכלים הנדרשים
- מתקינים או מעדכנים לגרסה היציבה האחרונה של Android Studio.
- כדי למנוע בעיות תאימות, מעדכנים את הפלאגין של Kotlin שמצורף ל-Android Studio לגרסה האחרונה.
- (אופציונלי) לפיתוח ל-iOS, מתקינים את Xcode כדי ליצור את ממשק המשתמש ולהוסיף קוד Swift או Objective-C לפי הצורך.
יצירת פרויקט Kotlin Multiplatform
אפשר להשתמש באשף Kotlin Multiplatform של JetBrains כדי ליצור פרויקט KMP חדש. חשוב לבחור באפשרות Do not share UI כדי לשמור על ממשק המשתמש המקורי.
מבנה הפרויקט
לפרויקטים ב-KMP יש מבנה דומה לזה של פרויקטים ב-Android.
פרויקט KMP מכיל מודולים ספציפיים לפלטפורמה וכן מודול משותף.
מוסיפים את הקוד הספציפי לפלטפורמה למודול הרלוונטי. לדוגמה, מוסיפים את ממשק המשתמש של אפליקציית Android במודול androidApp ואת ממשק המשתמש של אפליקציית iOS ב-iosApp.
כל קוד שרוצים לשתף בין הפלטפורמות צריך להוסיף למודול shared.
המודול המשותף משתמש ב-Gradle כמערכת build, בדיוק כמו שאר הפרויקט. אפשר להצהיר על יחסי תלות נפוצים ותלות ספציפיות לפלטפורמה באמצעות קבוצות מקורות (sourcesets). לדוגמה, אם האפליקציה שלכם משתמשת ב-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 (שעון UTC).
[[["התוכן קל להבנה","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 (שעון UTC)."],[],[],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."]]