מעבר מנכסים סינתטיים של Kotlin לקישור תצוגה של Jetpack
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
תוספי Kotlin ל-Android הוצאו משימוש, כלומר השימוש ב-Kotlin
אין יותר תמיכה בחומרים סינתטיים לקישורי תצוגות. אם באפליקציה נעשה שימוש ב-Kotlin
סינתטיים לקישורי תצוגה, השתמשו במדריך הזה כדי לעבור לקישור תצוגה של Jetpack.
אם האפליקציה לא משתמשת כבר בסינתטים של Kotlin לצורך קישור תצוגות מפורטות, צריך לעיין בהצגה
מקושר לפרטי שימוש בסיסיים.
עדכון קובץ Gradle
בדומה לתוספים ל-Android, קישור התצוגה של Jetpack מופעל במודול אחר מודול
במצב בסיסי. מגדירים את ה-build של viewBinding לכל מודול שמשתמש בקישור תצוגה
האפשרות true בקובץ build.gradle ברמת המודול:
מגניב
android{...buildFeatures{viewBindingtrue}}
Kotlin
android{...buildFeatures{viewBinding=true}}
אם באפליקציה לא נעשה שימוש ב-Parcelable
תכונות, מסירים את השורה שמפעילה את תוספי Kotlin ל-Android:
באמצעות קישור תצוגת Jetpack, נוצרת מחלקה מחייבת לכל קובץ פריסת XML
שהמודול מכיל. השם של מחלקת הקישור הזו הוא השם של ה-XML.
שלך באותיות פסקל, שהמילה Binding נוספת בסוף. לדוגמה, אם
שם קובץ הפריסה הוא result_profile.xml, שם הקובץ שנוצר
סיווג הקישור הוא ResultProfileBinding.
להשתמש בסוגי הקישור שנוצרו במקום במאפיינים סינתטיים,
לתצוגות עזר, לשנות את הפעילות ואת סיווגי המקטעים באמצעות ביצוע
הבאים:
צריך להסיר את כל הייבוא מ-kotlinx.android.synthetic.
ניפוח מופע של סוג הקישור שנוצר עבור הפעילות או
מקטע לשימוש.
שנו את כל ההפניות של התצוגות כך שישתמשו במופע של מחלקת הקישור במקום במופע של
נכסים סינתטיים:
// Reference to "name" TextView using synthetic properties.name.text=viewModel.nameString// Reference to "name" TextView using the binding class instance.binding.name.text=viewModel.nameString
מידע נוסף זמין בקטע שימוש ב-
את המדריך לקישור תצוגה.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. 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,["# Migrate from Kotlin synthetics to Jetpack view binding\n\nKotlin Android Extensions is deprecated, which means that using Kotlin\nsynthetics for view binding is no longer supported. If your app uses Kotlin\nsynthetics for view binding, use this guide to migrate to Jetpack view binding.\n\nIf your app doesn't already use Kotlin synthetics for view binding, see [View\nbinding](/topic/libraries/view-binding) for basic usage information.\n\nUpdate the Gradle file\n----------------------\n\nLike Android Extensions, Jetpack view binding is enabled on a module-by-module\nbasis. For each module that uses view binding, set the `viewBinding` build\noption to `true` in the module-level `build.gradle` file: \n\n### Groovy\n\n```groovy\nandroid {\n ...\n buildFeatures {\n viewBinding true\n }\n}\n```\n\n### Kotlin\n\n```kotlin\nandroid {\n ...\n buildFeatures {\n viewBinding = true\n }\n}\n```\n\nIf your app doesn't use [`Parcelable`](/reference/android/os/Parcelable)\nfeatures, remove the line that enables Kotlin Android Extensions: \n\n### Groovy\n\n```groovy\nplugins {\n id 'kotlin-android-extensions'\n}\n```\n\n### Kotlin\n\n```kotlin\nplugins {\n kotlin(\"android.extensions\")\n}\n```\n| **Note:** If your app uses `Parcelable` features, switch to using the standalone `kotlin-parcelize` Gradle plugin described in [Parcelable implementation generator](/kotlin/parcelize).\n\nTo learn more about enabling view binding in a module, see [Setup\ninstructions](/topic/libraries/view-binding#setup).\n\nUpdate activity and fragment classes\n------------------------------------\n\nWith Jetpack view binding, a binding class is generated for each XML layout file\nthat the module contains. The name of this binding class is the name of the XML\nfile in Pascal case with the word *Binding* added at the end. For example, if\nthe name of the layout file is `result_profile.xml`, the name of the generated\nbinding class is `ResultProfileBinding`.\n\nTo use the generated binding classes instead of synthetic properties to\nreference views, change your activity and fragment classes by doing the\nfollowing:\n\n1. Remove all imports from `kotlinx.android.synthetic`.\n\n2. Inflate an instance of the generated binding class for the activity or\n fragment to use.\n\n - For activities, follow the instructions in [Use view binding in\n activities](/topic/libraries/view-binding#activities) to inflate an instance in your activity's [`onCreate()`](/reference/kotlin/android/app/Activity#oncreate) method.\n - For fragments, follow the instructions in [Use view binding in\n fragments](/topic/libraries/view-binding#fragments) to inflate an instance in your fragment's [`onCreateView()`](/reference/kotlin/androidx/fragment/app/Fragment#oncreateview) method.\n3. Change all view references to use the binding class instance instead of\n synthetic properties:\n\n // Reference to \"name\" TextView using synthetic properties.\n name.text = viewModel.nameString\n\n // Reference to \"name\" TextView using the binding class instance.\n binding.name.text = viewModel.nameString\n\nTo learn more, see the [Usage](/topic/libraries/view-binding#usage) section in\nthe view binding guide.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [View binding](/topic/libraries/view-binding)\n- [Paging library overview](/topic/libraries/architecture/paging/v3-overview)\n- [Test your Paging implementation](/topic/libraries/architecture/paging/test)"]]