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

AndroidAndroid StudioKotlinImportAndroid Gradle-Plugin

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

Kotlin Android Extensions is depreciated. Migrate to Jetpack view binding. See below: https://developer.android.com/topic/libraries/view-binding/migration

Solution 10 - Android

Simple friend, you forgot the asterisk.

    import kotlinx.android.synthetic.main.activity_main.*

It just happened to me.

Solution 11 - Android

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.

Solution 12 - Android

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

Solution 13 - Android

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

Solution 14 - Android

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

Solution 15 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWurtzelzwerkView Question on Stackoverflow
Solution 1 - AndroidAmit BaderiaView Answer on Stackoverflow
Solution 2 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 3 - Androidalireza nikzadView Answer on Stackoverflow
Solution 4 - AndroidGanesh GaradView Answer on Stackoverflow
Solution 5 - AndroidmarkView Answer on Stackoverflow
Solution 6 - AndroidF.MysirView Answer on Stackoverflow
Solution 7 - AndroidThomas ZhangView Answer on Stackoverflow
Solution 8 - AndroidSaadurRehmanView Answer on Stackoverflow
Solution 9 - AndroidNozar MozakaView Answer on Stackoverflow
Solution 10 - AndroidJoaquín Luis Monleón IrisarriView Answer on Stackoverflow
Solution 11 - AndroidDiscoboyView Answer on Stackoverflow
Solution 12 - AndroidMarkTView Answer on Stackoverflow
Solution 13 - AndroidDavidView Answer on Stackoverflow
Solution 14 - AndroidAbbas FarajiView Answer on Stackoverflow
Solution 15 - AndroidsarnabtechView Answer on Stackoverflow