Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 360
Wurtzelzwerk Avatar asked Sep 11 '18 08:09

Wurtzelzwerk


People also ask

Why is Kotlin synthetic deprecated?

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.

How does kotlin synthetic work?

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.


9 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'
}
like image 54
Amit Baderia Avatar answered Oct 06 '22 02:10

Amit Baderia


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.

like image 23
Khemraj Sharma Avatar answered Oct 06 '22 03:10

Khemraj Sharma


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

like image 41
Ganesh Garad Avatar answered Oct 06 '22 02:10

Ganesh Garad


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

like image 35
alireza nikzad Avatar answered Oct 06 '22 01:10

alireza nikzad


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"
}
like image 35
mark Avatar answered Oct 06 '22 01:10

mark


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.

like image 34
F.Mysir Avatar answered Oct 06 '22 03:10

F.Mysir


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 { ... }
        }
    }
like image 32
Thomas Zhang Avatar answered Oct 06 '22 02:10

Thomas Zhang


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

like image 43
Nozar Mozaka Avatar answered Oct 06 '22 01:10

Nozar Mozaka


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

like image 37
SaadurRehman Avatar answered Oct 06 '22 02:10

SaadurRehman