Migrating from kotlin-android-extensions to newer view binding

12.3k Views Asked by At

So if you are getting anything like below

> Configure project :app
Warning: The 'kotlin-android-extensions' Gradle plugin is deprecated.

This probably means which means that using Kotlin synthetics for view binding is no longer supported / deprecated.

So below is the answer where you can clearly get understanding on how to get / identify your ViewBinding class related to your views.

1

There are 1 best solutions below

0
On BEST ANSWER

In order to migrate to the newer way of binding you need to first remove the kotlin synthetics plugin which could have been added as below :

apply plugin: 'kotlin-android-extensions'

OR

plugins {
    ...
    id 'kotlin-android-extensions'
}

after removing synthetic plugin from app gradle you need to remove the imports which could like either of below :

  • activity /fragment view : import kotlinx.android.synthetic.main.<your_activity_view>.*
  • normal views : import kotlinx.android.synthetic.main.<your_layout_view>.view.*

Now begins actual migration

You need to add below inside your app gradle

android {
     ... 
     buildFeatures {
        viewBinding true
    }
}

After this you need to add a binding property where your view is to be bound. Below is an example :

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)

    binding.myTextView.text = "my text" //say your text view id is like : my_text_view"
}

Note : if your activity layout is activity_main.xml then your binding should be ActivityMainBinding

here you will find view binding example for fragment

here is official migration doc from google