Unable to use Kotlin android extension in adapter class

I’m currently having this strange issue. I was able to use the extension in adapter earlier, but after moving the adapter into another package, the android extension suddenly stopped working. Here’s the output log:

Error:(42, 17) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val View.fullNameTv: TextView! defined in kotlinx.android.synthetic.main.item_repo.view
Error:(43, 17) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val View.starCountTv: TextView! defined in kotlinx.android.synthetic.main.item_repo.view
Error:(44, 17) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val View.item: LinearLayout! defined in kotlinx.android.synthetic.main.item_repo.view

I have tried another layout but the problem was still there. Though the extension still works in activity, it just doesn’t in adapter. Moving the buildscript in project gradle file into my module doesn’t help as well. I’m pretty lost, anyone having this issue?

I’m using kotlin 1.1.2-3, this happens in Android Studio 2.3.1 as well as 3.0.

1 Like

I am having the same problem. I have a Presenter class where I would like to reference the view Id’s.

Android Extensions plugin now generates synthetic properties for Activity, Fragment and View receiver types. I think you can use the base view in your custom components:

class MyComponent(val baseView: View) {
    val firstName = baseView.firstName
    val lastName = baseView.lastName
}

Note to import the kotlinx.android.synthetic.<variantName>.<layoutName>.view package contents.

Also, there is a KEEP for supporting user holder classes, and the prototype is almost done. See Android Extensions: View holder pattern support and caching options by yanex · Pull Request #33 · Kotlin/KEEP · GitHub for more details.

I’m using

ext.kotlin_version = ‘1.2.0-rc-39’

and i still have a problem, that kotlin extension doesnt see my views, what strange is it see com.example.R.id.xxxx, but not kotlinx.android.synthetic although i’ve imported it

The answer was already given Unable to use Kotlin android extension in adapter class - #3 by yanex

You still need to inflate a base view in adapter:

private val baseView = itemView.findViewById<View>(R.id.itemWrapper)

but then you can use baseView.subView1, baseView.subView2 and so on