Import kotlinx greyed out
I think i try nearly everything. Reinstall Android Studio, Invalide Cache, new Project same Problem.
i just can't find the Solution
In November 2020, we announced that this plugin has been deprecated in favor of better solutions, and we recommended removing the plugin from your projects. We know many developers still depend on this plugin's features, and we've extended the support timeframe so you have more time to complete your migrations.
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.
Check "build.gradle(:app)" file,
plugins {
id 'com.android.application'
id 'kotlin-android'
}
if kotlin extension is missing, add kotlin-android-extensions
as shown below and click on "Sync now"
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
Can you try
OR just remove apply plugin: 'kotlin-android-extensions'
, sync gradle plugin and then I added it again.
Just add below line in your build.gradle(Module:YourProjectName.app) inside the plugins section on top:
plugins{
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
Mostly first two lines are already there just need to add 3rd one and sync project
Here is a step by step answer:
Gradle
Open Gradle Config
plugins
part and then add this:id 'kotlin-android-extensions'
sync
Result: now you can import kotlinx.android.synthetic.main.activity_main.*
module gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
project gradle
buildscript{
ext.kotlin_version = '1.3.11'
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
Synthetics are now deprecated from Google. Try to avoid using them as it might lead to null pointer exceptions and unexpected behaviour on your app.
Read more on:
Migrate from Kotlin synthetics to Jetpack view binding from official developers site.
In build.gradle (:app)
, add:
buildFeatures {
viewBinding true
}
In MainActivity
:
private lateinit var binding: ActivityMainBinding
Modify onCreate
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setListeners()
}
To set listeners:
/**
* Attaches listeners to all the views.
*/
private fun setListeners() {
val clickableViews: List<View> =
listOf(
binding.view1,
binding.view2,
// ...
)
for (item in clickableViews) {
item.setOnClickListener { ... }
}
}
Kotlin Android Extensions is depreciated. Migrate to Jetpack view binding. See below: https://developer.android.com/topic/libraries/view-binding/migration
For me it was just adding the apply plugin: 'kotlin-android-extensions'
to app's build.gradle, press sync gradle files and i was able to get synthetics
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With