Di chuyển từ Kotlin Synthetics sang liên kết khung hiển thị Jetpack
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tiện ích Kotlin dành cho Android không còn được dùng nữa, nghĩa là việc sử dụng Kotlin
dữ liệu tổng hợp để liên kết khung hiển thị không còn được hỗ trợ. Nếu ứng dụng của bạn sử dụng Kotlin
dữ liệu tổng hợp để liên kết khung hiển thị, hãy sử dụng hướng dẫn này để di chuyển sang liên kết khung hiển thị Jetpack.
Nếu ứng dụng của bạn chưa sử dụng Kotlin Synthetics để liên kết khung hiển thị, hãy xem phần View
ràng buộc để biết thông tin sử dụng cơ bản.
Cập nhật tệp Gradle
Giống như các tiện ích dành cho Android, tính năng liên kết khung hiển thị Jetpack được bật trên từng mô-đun
cơ sở. Đối với mỗi mô-đun sử dụng tính năng liên kết khung hiển thị, hãy đặt bản dựng viewBinding
vào true trong tệp build.gradle ở cấp mô-đun:
Groovy
android{...buildFeatures{viewBindingtrue}}
Kotlin
android{...buildFeatures{viewBinding=true}}
Nếu ứng dụng của bạn không sử dụng Parcelable
tính năng, hãy xoá dòng bật Tiện ích Kotlin dành cho Android:
Groovy
plugins{id'kotlin-android-extensions'}
Kotlin
plugins{kotlin("android.extensions")}
Để tìm hiểu thêm về cách bật tính năng liên kết thành phần hiển thị trong một mô-đun, hãy xem phần Thiết lập
hướng dẫn.
Cập nhật lớp mảnh và hoạt động
Với tính năng liên kết khung hiển thị Jetpack, một lớp liên kết được tạo cho mỗi tệp bố cục XML
mà mô-đun có chứa. Tên của lớp liên kết này là tên của XML
kiểu viết hoa Pascal có từ Binding được thêm vào cuối. Ví dụ: nếu
tên của tệp bố cục là result_profile.xml, tên của phần tử được tạo
lớp liên kết là ResultProfileBinding.
Để sử dụng các lớp liên kết được tạo thay vì thuộc tính tổng hợp để
chế độ xem tham chiếu, thay đổi lớp hoạt động và lớp mảnh bằng cách thực hiện
sau:
Xoá tất cả dữ liệu nhập khỏi kotlinx.android.synthetic.
Tăng cường một thực thể của lớp liên kết đã tạo cho hoạt động hoặc
để sử dụng.
Thay đổi tất cả thông tin tham chiếu đến khung hiển thị để sử dụng thực thể lớp liên kết thay vì
thuộc tính tổng hợp:
// 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
Để tìm hiểu thêm, hãy xem phần Cách sử dụng trong
hướng dẫn liên kết khung hiển thị.
Đề xuất cho bạn
Lưu ý: văn bản có đường liên kết sẽ hiện khi JavaScript tắt
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 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)"]]