Unable to add Kotlin Android Extensions to my android studio - android

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

Related

disable kotlin-android-extensions deprecation warning

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.

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

How can I use newer release of android databinding library?

according to the bintray, newer version of the Android Databinding library has been released(1.0-rc5).
but if I'm correct, only compiler, compilerCommon and baseLibrary are updated, and dataBinder(which we actually include in build.gradle) is still 1.0-rc4.
Is there a way to use newer compiler, compilerCommon and baseLibrary while using dataBinder 1.0-rc4? Or is there already a 1.0-rc5 for dataBinder?
You can override the data-binding version in the build.gradle, like this:
dataBinding {
enabled true
version '1.1'
}
Never mind, in Android gradle plugin 1.5.0-beta1, dev team integrated data binding plugin into android plugin.
http://tools.android.com/tech-docs/new-build-system

Categories

Resources