import kotlinx.android.synthetic.main.activity_main is not working

Viewed 89197

Import kotlinx greyed out

enter image description here

I think i try nearly everything. Reinstall Android Studio, Invalide Cache, new Project same Problem.

i just can't find the Solution

16 Answers

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

  • File | Invalidate Caches / Restart
  • Deleting .idea folder
  • Clean
  • Re-import the project

OR just remove apply plugin: 'kotlin-android-extensions' , sync gradle plugin and then I added it again.

Here is a step by step answer:

  • From right side of the Android studio click on Gradle
  • Right click on the app and click Open Gradle Config
  • New source opening in plugins part and then add this:

id 'kotlin-android-extensions'

  • Tap sync

Result: 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"
}

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 { ... }
        }
    }

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:

  1. open gradle app.module and add this line inside

    android{

     android{viewBinding.enabled = true
     ...
    
     }
    

(then sync)

  1. go to MainActivity.kt and do this:

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 :

  1. Put this into your app.iml

<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>

  1. Do gradle sync
Related