import kotlinx.android.synthetic.main.activity_main is not working
Asked Answered
H

19

76

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

Humiliating answered 11/9, 2018 at 8:13 Comment(3)
github.com/gradle/kotlin-dsl/issues/377Crucible
did you convert your project to androidx? To me the issue happened after that.Hostetler
kotlin-android-extensions is not supported anymore use viewbindingCanter
C
61

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.

Cloistral answered 11/9, 2018 at 8:14 Comment(6)
i add it to my file but its greyed out and its says "Unused import directive"Humiliating
Did you try running your app? unused simply mean, it is not used yet, it will be used when you use some id in your class.Cloistral
that verry weird the import line Disappears when i try to add a id to my classHumiliating
Thank you so much, i dont know how but now its workingHumiliating
Deleting .idea folder worked for me, I think it started happening because I switch branched and AS didn't know how to go from thereMurrumbidgee
My project compiled, but an IDEA showed an error, I just removed the kotlin-android-extensions -> sync, and added it again and it worked againMicrometry
C
153

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'
}
Champlain answered 13/11, 2020 at 11:11 Comment(4)
This worked. I think there is a bug on the new Android Studio 4.1 version that excludes extension at project creation.Macaque
It works for me. ThanksCsc
This was my answer. Thanks.Gangrel
Simple solution, thanks.Justiciar
C
61

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.

Cloistral answered 11/9, 2018 at 8:14 Comment(6)
i add it to my file but its greyed out and its says "Unused import directive"Humiliating
Did you try running your app? unused simply mean, it is not used yet, it will be used when you use some id in your class.Cloistral
that verry weird the import line Disappears when i try to add a id to my classHumiliating
Thank you so much, i dont know how but now its workingHumiliating
Deleting .idea folder worked for me, I think it started happening because I switch branched and AS didn't know how to go from thereMurrumbidgee
My project compiled, but an IDEA showed an error, I just removed the kotlin-android-extensions -> sync, and added it again and it worked againMicrometry
W
16

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

West answered 9/10, 2021 at 17:45 Comment(0)
O
15

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

Obedience answered 9/3, 2021 at 11:23 Comment(0)
B
10
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"
}
Barrus answered 25/1, 2019 at 6:36 Comment(1)
The answer is worked for me (apply plugin: 'kotlin-android-extensions') & after Invalidate Caches / RestartExoergic
G
9

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.

Graiggrail answered 15/12, 2020 at 14:11 Comment(0)
H
5

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 { ... }
        }
    }
Hairball answered 9/5, 2021 at 0:1 Comment(0)
W
3

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

Witless answered 5/6, 2021 at 5:19 Comment(0)
M
1

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

Manoff answered 10/12, 2020 at 12:35 Comment(0)
M
1

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.

STEP 1.

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

STEP 2

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

Madder answered 28/2, 2023 at 11:26 Comment(0)
H
0

Simple friend, you forgot the asterisk.

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

It just happened to me.

Hew answered 6/3, 2020 at 8:40 Comment(0)
I
0
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.

Intercession answered 15/12, 2020 at 12:23 Comment(0)
P
0

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

Peskoff answered 4/2, 2021 at 22:34 Comment(0)
E
0
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
Edyth answered 8/10, 2021 at 18:38 Comment(0)
P
0

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

Piddock answered 3/1, 2022 at 9:24 Comment(0)
P
0

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

Pallaten answered 2/7, 2022 at 9:21 Comment(0)
S
0

In my case I was missing in module's gradle file:

androidExtensions {
  experimental = true 
}
Summerwood answered 10/7, 2023 at 15:56 Comment(0)
C
0

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"
    >
Congo answered 23/12, 2023 at 5:43 Comment(0)
P
-1

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
Purgatorial answered 16/11, 2020 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.