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
Can you try
OR just remove apply plugin: 'kotlin-android-extensions'
, sync gradle plugin and then I added it again.
kotlin-android-extensions
-> sync, and added it again and it worked again –
Micrometry 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.
kotlin-android-extensions
-> sync, and added it again and it worked again –
Micrometry 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
For newcomers to Android world who use JetPack Library:
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. If your app doesn't already use Kotlin synthetics for view binding, see View binding for basic usage information.
From Migrate from Kotlin synthetics to Jetpack view binding documentation
To insert plugins in app:build.gradle
as
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
isn't necessary anymore and does not work.
Instead you must insert this implementation in the app:build.gradle
file:
android {
...
buildFeatures {
viewBinding = true
}
}
Examples: Setup instruction by developer.android.com, Sean McQuillan's Blog setup instruction in Android 3.6 and Android 4.0, Github ViewBindingSample given by developer.android.com
To bind the .xml
layout to the main activity program (ex: MainActivity.kt
) you must proceed like the following in the acitivity .kt
file :
private lateinit var binding: ResultProfileBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ResultProfileBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.labelWeight.text = "weight (lbs)" // a TextView in the layout
}
If you have a layout called activity_awesome.xml, which contains a button and two text views, view binding generates a small class called ActivityAwesomeBinding that contains a property for every view with an ID in the layout.
From McQuillan's blog
So here ResultProfileBinding
is the id binding of the layout result_profile_binfind.xml
. Snake case is converted to Pascal case for .xml
layout name, and Camel case for views present in the .xml
layout (ex: TextView android:id="@+id/label_weight
in the layout will give binding.labelWeight
in the activity program) .
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. Migrate from Kotlin synthetics to Jetpack view binding
In my case I was missing in module's gradle file:
androidExtensions {
experimental = true
}
in my case i applied all above solutions but didn't work and then i just added following line in menifest and it is working fine.
package="com.your.package"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.your.package"
>
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>
© 2022 - 2024 — McMap. All rights reserved.