disable kotlin-android-extensions deprecation warning - android

My project's migration to View Binding is underway but in the meantime this warning is distracting when looking at build logs
Warning: The 'kotlin-android-extensions' Gradle plugin is deprecated. Please use this migration guide (https://goo.gle/kotlin-android-extensions-deprecation) to start working with View Binding (https://developer.android.com/topic/libraries/view-binding) and the 'kotlin-parcelize' plugin.
How can I disable it?

I also recently faced this problem and found out.
In Kotlin 1.4.20-M2, JetBrains deprecated the Kotlin Android Extensions compiler plugin in favor of View Binding, and Also, Google is promoting modularisation, but synthetic properties don’t work cross-module.
So to fix this warning. Remove apply plugin: 'kotlin-android-extensions' in your build.gradle file
Note: If you're using Parcelize.
Don’t forget that the Parcelize feature in Kotlin is part of the kotlin-android-extensions compiler plugin, so removing the plugin will end up making all your Parcelable classes not compiling if they depend on the Parcelize annotation.
JetBrains extracted the Parcelize from Kotlin Android Extensions to a new plugin, kotlin-parcelize
First, you will need to add kotlin-parcelize plugin to your project build.gradle file.
Plugins {
...
id 'kotlin-parcelize'
}
Then change your old import statement from
import kotlinx.android.parcel.Parcelize
to
import kotlinx.parcelize.Parcelize
For more info, I recommend you to read this Blog Migrating the deprecated Kotlin Android Extensions compiler plugin

Just you need to remove this line from your Gradle:
apply plugin: 'kotlin-android-extensions'

Follow the steps:
kotlin-android-extensions is deprecated so replace it from kotlin-parcelize
If there is #Parcelize annotation used for model classes so you have to replace its import from kotlinx.android.parcel.Parcelize to import kotlinx.parcelize.Parcelize

The report is a warning that you are using a deprecated plugin, I don't understand why you would want to disable it since sooner or later I expect the compile will fail and you may not remember why.
The article says: "If your app does not use Parcelize features, remove the line that enables Kotlin Android Extensions: (aka, just remove the line apply plugin: 'kotlin-android-extensions'). It may turn out you don't need the plugin, hard to say since you didn't indicate if you are using any features supplied by the deprecated plugin.
If you are annotating any classes with 'Parcelable' then there is a bit or work you will need to do, as indicated in the article.
Edit: also gone is view synthetics (the feature that allowed you to not use all those 'findViewById'. that's (IMO) is the larger loss and I have not yet ported my apps over so I cannot comment on the amount of work it will take to move.
For the immediate future, I've chosen to wait and implement the new code later but you will need to make your own decision as to when it's appropriate to migrate away from the deprecated plugin.

Related

Unable to add Kotlin Android Extensions to my android studio

hen i try to add kotlin-android-extensions via:
apply plugin: 'kotlin-android-extensions'
to my project Android Studio tells me Plugin with 'kotlin-android-extensions not found??
What is going wrong? I am Running Android Studio
Kotlin Android Extensions has been deprecated for two years and should not be used in new products.
As of Kotlin 1.8, it is not available at all anymore.
I don't think they've officially turned it off yet, since Kotlin 1.8 hasn't officially been released, so there is probably some config issue in your project's Gradle files, but it doesn't matter, because you should not rely on a plugin that is going to stop working within 3 months.
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.
you can add any of it like the following
android {
...
buildFeatures {
dataBinding true
}
}
As "kotlin-android-extensions" is deprecated now, it's better to use view binding.
just head to the module-level build.gradle and add inside the android block:
buildFeatures {
viewBinding = true
}
then you will be able to use binding variables to reference views from your activities and fragments, all you need is here, but if you have any additonal questions to viewBinding just leave a comment

android.synthetic library isn't being recognized in android studio

In my Activity.kt class, the synthetic isn't being recognized in the import
import kotlinx.android.synthetic...
I have included the following in my build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
When trying it import, it will recognize extensions and parcel
import kotlinx.android.extensions...
import kotlinx.android.parcel...
So why is synthetic the only one not being recognized?
Other info:
I have tried invalidating caches and restarting.
I have another computer that also has this android studio project (Same gradle versions and everything). Everything works fine there.
Edit:
I understand the current way to do it is to use data binding. But this is just a project we need to sustain, not improve and upgrade. We have a custom sdk (added many years ago) in the project that is making it hard to upgrade gradle and modernize.
The project, even though synthetic is not recognized, is building and uploading fine. I just want to remove these red lines of error.
Any advice on how to start trouble shooting would be greatly appreciated.
Synthetics were deprecated and removed in the latest version of Android Studio (Chipmunk). The recommendation is to migrate to using view binding. If you cannot do that, then you'll need to use an older version of Android Studio.
Migrating to view binding from synthetics is generally straightforward, so that's the better long term solution of course, but changing the IDE version is a quick short term solution.
Even projects in maintenance mode require occasional maintenance - like dealing with deprecations.

How to Enable Kotlin Android Extensions by Default in Android Studio 4.1

Why kotlin-android-extensions not enabled by default in this latest version of Android Studio? In fact, as long as i know, i have to manually add the plugin in gradle files. Any workaround so that i can add the plugin and enable it automatically everytime i create new project? Thanks before.
ICYMI, this is the code that needs to be added to enable kotlin-android
plugins {
id 'com.android.application'
id 'kotlin-android'
// add kotlin-android-extensions
id 'kotlin-android-extensions'
}
In Kotlin 1.4.20-M2 JetBrains deprecated Kotlin Android Extensions compiler plugin.
kotlinx.android.synthetic is no longer a recommended practice. Removing in favour of explicit findViewById
Very sad news :(, i hate viewBinding
In this commit you can see the message:
Replaced kotlinx synthetic with findViewById
kotlinx.android.synthetic is no longer a recommended practice.
Removing in favour of explicit findViewById.
It says that it is no longer recommended, but that doesn't mean that you should not use it. They are providing guidelines, but you can choose what to use and what not.
Other alternatives:
findViewById
View Binding
kotlin-android-extensions is deprecated, you must use view binding
Android studio 4.1 do not support to import kotlin extension

How to disable the generating of synthetic view properties by the Kotlin Android extensions plugin

The Kotlin Android Extensions plugin generates static properties for each view with an ID from my app layout files, as it is described in the documentation.
I would like to disable this feature, because we use DataBinding instead of the Kotlin synthetic view properties, and sometimes I import them by accident; also it adds extra build overhead for something we don't use.
Disabling the Android extensions plugin is not possible, because we use the Parcelize feature which is done by the same plugin.
There is a features property in the androidExtensions DSL that allows you to provide a list of features to enable. Currently, there is only two available, parcelize and views. To disable synthetic view properties, add this to your build.gradle:
android {
// ...
}
androidExtensions {
features = ["parcelize"]
}
Source: https://github.com/JetBrains/kotlin/blob/6bef27e1e889b17ae84dd2ff99881727f13ac3e5/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt#L57
Nowadays, android-extensions plugin is discontinued so the best solution will be to just remove this plugin by removing apply plugin: 'kotlin-android-extensions' from your build.gradle.
Instead of synthetics we should use ViewBinding or DataBinding.
As explained in the first link, if you're also using parcelizer, you just need to change android-extensions plugin to kotlin-parcelize and change the import statement import kotlinx.android.parcel.Parcelize for import kotlinx.parcelize.Parcelize wherever is needed.
More info to migrate from android-extensions to JetPack here.

Difference between Kotlin-android and kotlin-android-extensions in android studio 3.0

In android studio 3.0 when we create a new project for kotlin there are two plugins automatically added in app build gradle file like below:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
Why do we need to apply the android-extensions plugin in addition with the kotlin-android plugin in android studio? Is there any difference or any reason behind this.
Every Android developer knows findViewById(). But findViewById is not required with Kotlin anymore.
And how this has happened? Its because of Kotlin Android Extension.
how to access TextView with Kotlin :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
textView.setText("Hello World"); // No need(findViewById) to create a reference variable explicitly
}
But how does all this work ?? This is the magic of 'Kotlin Android Extensions'.
You might have noticed below plugin in Gradle file and an import statement in our activity class.
apply plugin: 'kotlin-android-extensions'
import statement in Activity:
import kotlinx.android.synthetic.main.activity_login.*
Because of this plugin and import statement , we don't explicitly need to create View object in our activity. we can directly refer View from the name defined in layout file.
If the layout filename is activity_main.xml, we' should
import kotlinx.android.synthetic.main.activity_main.*.
See here for detail :http://kotlin-andro.blogspot.in/2017/07/kotlin-android-extensions.html
Is there any difference or any reason behind this
Yes, there is a difference.
The kotlin-android-extensions plugin is not a part of the kotlin-android plugin. One of the reasons you add the extensions plugin (either explicitly or automatically added for you by Android Studio) is to eliminate having to deal with the findViewById function because it helps you recover views seamlessly.
Internally the compiler creates a small hidden cache function which calls findViewById for every view, next time you use the same view it will fetch it from cache instead of calling findViewById again.
So, you add kotlin-android to setup Kotlin for Android Studio and kotlin-android-extensions for eliminate the need for calling findViewById.
Cheers and happy coding!
kotlin-android is use for kotlin support and kotlin-android-extensions is use for replace findViewById concept of java in android.by using kotlin-android-extensions in your project, you can access directly by Id which you have mentioned in your .xml file like : android:id="#+id/tvNextAbout"
you can use it in your activity example:
tvNextAbout.setOnClickLisnet{}
kotlin-android-plugin is what allows you to use kotlin on Android Studio;
kotlin-android-extensions provide a set if tools to make android development easier.
https://kotlinlang.org/docs/tutorials/android-plugin.html
https://kotlinlang.org/docs/tutorials/kotlin-android.html

Categories

Resources