with the Gradle version 6.2 and Android studio version 4.0.1, I have the deprecation message for the Kotlin-android-extention. any body has any idea how to fix it.
-
Hi Ali Kotlin-android-extension has been deprecated, you should consider using view binding, checkout blog for detailed explanation and anti-patterns regarding it viewbindingChetan Gupta– Chetan Gupta2020-12-24 11:35:03 +00:00Commented Dec 24, 2020 at 11:35
-
Hi @ChetanGupta, Thanks for your comment. yeah you right. I did just like the answer and it's working fine for me.Yoshimitsu– Yoshimitsu2020-12-25 13:52:38 +00:00Commented Dec 25, 2020 at 13:52
-
1follow this doc - developer.android.com/topic/libraries/view-binding/migrationRanjithkumar– Ranjithkumar2022-04-03 22:10:14 +00:00Commented Apr 3, 2022 at 22:10
12 Answers
It's deprecated Base on the google document
Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.
for those who's wonder what the synthetic is. I should say a simple way to access to UI view id with kotlin which was possible by adding 'kotlin-android-extensions' in Gradle.
- If your app uses Parcelable you can use
'kotlin-parcelize'instead of'kotlin-android-extensions'. - If your app uses Kotlin synthetics for view binding, use this guide to migrate to Jetpack ViewBinding or Data Binding.
3 Comments
It's deprecated now
Remove
'Kotlin-android-extensions'from the plugin. (Used for kotlin's synthetic way to access UI elements)Add below to use
view bindingway to access UI elementandroid { ... buildFeatures { viewBinding true } }If your app contains
Parcelable, please add'kotlin-parcelize'in plugin to use a simple way to create parcelable by just adding@Parcelizeannotation to class
You can see the Google guideline regarding this update.
1 Comment
As "kotlin-android-extensions" is deprecated now it's better to use view binding.
For this first enable view binding in build.gradle file by writing these line of codes under android block.
buildFeatures {
viewBinding true
}
then in activity file to use view binding features
first declare a global variable for binding as
private lateinit var binding:ActivityMainBinding
Here ActivityMainBinding is a auto generated class
then write these codes in OnCreate() method
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Comments
In my opinion, using this plugin is still better than the methods introduced, we just need to pay attention to IDs. Because With the Android Kotlin Extensions Gradle plugin released in 2017 came Kotlin Synthetics. For every layout file, Kotlin Synthetics creates an autogenerated class containing your view— as simple as that. You just have to import this plugin in your Gradle file, and you are all set to directly refer to the view reference variables. It calls findViewById internally only once and then caches it. This was so convenient and such fun to use, so why is it being deprecated Let's analyze it. Pros No boilerplate code — Just configure it once in your Gradle file and you are all set to go. Kotlin Synthetics will automatically generate a class ready for you to use. Type safety — All views are picked from your layout file with the type known to it. Cons Partial null safety — It is generally null safe as all your views must be present in the layout file for it to generate. But what if you have multiple layout files based on configurations where some views are present in some layout files and missing in some? Here you have to then manually check for null. Pollutes the namespace — You can have the same view ID in different layouts, and you can accidentally import a view of some other layout, which will later at runtime throw NullPointerException. Kotlin only — Kotlin Synthetics can only be used with Kotlin. They are not supported with Java. There are still projects that are written in Java and may not be fully migrated to Kotlin yet, and hence Kotlin Synthetics may not be a consistent way of getting the ViewIds in your project. Because of these issues, Kotlin Synthetics is now being deprecated.
Now it's up to you to decide if you still want to use this plugin like me if you want to fix deprecated warning , you have to avoid using the deprecated method.😊
Comments
The above answers are correct, but when you remove 'kotlin-android-extensions' from your project, accessing view properties using kotlin's synthetic way won't be possible, so you need to migrate to use view binding, follow Google's this guide for migrating to view binding: link
Comments
Enable viewbindings. To enable the feature in the modules that will use it add the following into your build.gradle file.
android {
buildFeatures {
viewBinding true
}
}
Then add this code to your main activity
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
To
binding.objectID.text = "Text "
Comments
Following steps helps in managing the view binding for fragments :
Remove
kotlin-android-extensionsfrom the plugin.add following code in you
app/build.gradle:android { ... buildFeatures { viewBinding true } }Then in your fragment add following code :
class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private lateinit var viewModel: HomeViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = FragmentHomeBinding.inflate(layoutInflater)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
viewModel = ViewModelProvider(this)[HomeViewModel::class.java]
binding = FragmentHomeBinding.inflate(inflater, container, false)
binding.txtVw.text = menuItem.title
binding.imgVwIcon.setImageResource(menuItem.iconId)
return binding.root
}
}
Comments
As already stated, Kotlin Synthetics is deprecated and should be replaced by View Binding. In my case I wanted to introduce View Binding and gradually migrate from Kotlin Synthetics. It's possible to have both in one project, you just need to remember not to update Kotlin version - I had a build error with 4.1.21, so needed to downgrade to 1.4.10.
Also remember to delete Kotlin Synthetics imports from a class (usually activity/fragment), which has been migrated to using View Binding.
Comments
I changed my build.gradle of project file, As it is not supporting in upper versions of plugin so I downgraded the plugin version.
from this :
plugins{
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}
to this:
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}