Unresolved Reference - Activity Does Not Recognize Synthetic Imports in Android Studio V4

Unresolved reference - activity does not recognize synthetic imports in android studio v4

Kotlin Synthetic imports not working ?

Well, there's always the age-old alternative:

val foo: TextView = findViewById(R.id.your_id)

I believe synthetics have been deprecated and I guess support for it has just now been completely removed


Alternatively, you can make use of ViewBinding, which is another alternative.

Enable it in your build.gradle:

android {
...
buildFeatures {
viewBinding true
}
}

This generates a Binding object for your layout, so you can make use of it like this:

private lateinit var binding: YourLayoutNameBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = YourLayoutNameBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}

then you have access to all views on the layout, through the binding:

binding.name.text = "foo"

After changing developing OS, it shows Unresolved reference: synthetic

You need to build your code at least once for the build-time generated synthetic code to be available. Just importing the project does not build it.

Note that synthetic view binding with kotlin-android-extensions is deprecated. You can use e.g. view binding instead.

Unresolved reference: kotlinx

Add kotlin-android-extensions in our buildscript's dependencies:

1. In your project-level build.gradle

buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}

and apply the kotlin-android-extensions plugin:

2. In your module-level build.gradle

apply plugin: 'kotlin-android-extensions'

Unresolved reference in Android Studio problem

Unresolved reference means you're referring to something that doesn't exist, like an undeclared variable name - or at least the compiler doesn't know about it, from the code that you have. nextActivity isn't declared in your code anywhere, so the compiler is saying "ok, what's this?"

What Stanislav is showing you is the plugin that enables "Kotlin synthetic binding", which is basically magic code that turns your declared IDs in the layout XML file (like android:id="@+id/nextActivity" in your Button) into a variable in your Activity class with the same name as the ID. So even though you haven't declared a val nextActivity: Button anywhere, the synthetics have added it, so you can just refer to it like in your example. Without synthetics, it won't work.

Don't worry about not understanding all that, this kind of magic is great when you know it's there and how it works, it's incredibly confusing for a beginner. The normal way to look up a Button (or any View) is to do

val nextActivity = findViewById<Button>(R.id.nextActivity)

Kotlin synthetics are deprecated now, you're meant to use the ViewBinding library instead. Honestly I'd use findViewById if you're new, just so you get used to the general idea of things - you'll learn a lot! You can use the fancy automation later

Unresolved reference when writting btnDatePicker on Android studio

The answer by @himel is correct. Using kotlin synthetics is discouraged.

However I did notice that the import line for the synthetic view was not in your code. I suggest typing the view manually and importing with Android studio if you want a quick fix

Unresolved reference in kotlin with id and activity_main

In my case removing import android.R solved the issue.



Related Topics



Leave a reply



Submit