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"

Leave a Comment