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
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
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.
Here is a step by step answer:
GradleOpen Gradle Configplugins part and then add this:id 'kotlin-android-extensions'
syncResult: now you can import kotlinx.android.synthetic.main.activity_main.*
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
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
Simple friend, you forgot the asterisk.
import kotlinx.android.synthetic.main.activity_main.*
It just happened to me.
id 'kotlin-android-extensions'
id 'kotlin-android'
remove plugins and added them two of them. id 'kotlin-android-extensions' id 'kotlin-android'
should be added. restart the project.
So the problem as I have found is in gradle plugins, then you need to restart, rebuild your project.
Hope this help... maybe is related with the new way to get views from layouts
Kotlin:
open gradle app.module and add this line inside
android{
android{viewBinding.enabled = true
...
}
(then sync)
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater) // 2.1
setContentView(binding.root) // 2.2 instead of (R.layout.activity_main)
now views are called this way
binding.btn1.setOnClickListener{...}
or
binding.txtviewTitle.text = "Welcome to the jungle" // or any R.string
note: after you sync the gradle module.app with the line you will find any activity with the same name+Binding
Look 2.1 reference
buildscript {
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
I solved my problem in this manner: in build.gradle and in plugins add id 'kotlin-android-extensions' after some seconds when I write the name of button, automatically import kotlinx.android.synthetic.main.activity_main.* imported to code
as android document says Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported. If your app uses Kotlin synthetics for view binding, use this guide to migrate to Jetpack view binding. enter link description here
this fixed it for me :
<facet type="kotlin-language" name="Kotlin"> <configuration version="3" platform="JVM 1.8" allPlatforms="JVM [1.8]" useProjectSettings="false"> <compilerSettings /> <compilerArguments> <option name="jvmTarget" value="1.8" /> <option name="pluginOptions"> <array> <option value="plugin:org.jetbrains.kotlin.android:enabled=true" /> <option value="plugin:org.jetbrains.kotlin.android:defaultCacheImplementation=hashMap" /> </array> </option> </compilerArguments> </configuration> </facet>