Migrate from Kotlin synthetics to Jetpack view binding
Stay organized with collections
Save and categorize content based on your preferences.
Kotlin Android Extensions is deprecated, which means that using Kotlin
synthetics for view binding is no longer supported. If your app uses Kotlin
synthetics for view binding, use this guide to migrate to Jetpack view binding.
If your app doesn't already use Kotlin synthetics for view binding, see View
binding for basic usage information.
Update the Gradle file
Like Android Extensions, Jetpack view binding is enabled on a module-by-module
basis. For each module that uses view binding, set the viewBinding
build
option to true
in the module-level build.gradle
file:
Groovy
android {
...
buildFeatures {
viewBinding true
}
}
Kotlin
android {
...
buildFeatures {
viewBinding = true
}
}
If your app doesn't use Parcelable
features, remove the line that enables Kotlin Android Extensions:
Groovy
plugins {
id 'kotlin-android-extensions'
}
Kotlin
plugins {
kotlin("android.extensions")
}
To learn more about enabling view binding in a module, see Setup
instructions.
Update activity and fragment classes
With Jetpack view binding, a binding class is generated for each XML layout file
that the module contains. The name of this binding class is the name of the XML
file in Pascal case with the word Binding added at the end. For example, if
the name of the layout file is result_profile.xml
, the name of the generated
binding class is ResultProfileBinding
.
To use the generated binding classes instead of synthetic properties to
reference views, change your activity and fragment classes by doing the
following:
Remove all imports from kotlinx.android.synthetic
.
Inflate an instance of the generated binding class for the activity or
fragment to use.
Change all view references to use the binding class instance instead of
synthetic properties:
// 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
To learn more, see the Usage section in
the view binding guide.
Recommended for you
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 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)"]]