Skip to content Skip to sidebar Skip to footer

Unresolved Reference For Synthetic View When Layout Is In Library Module

using Kotlin 1.20.20 (not that it matters, older versions behaves the same) When layout is in separate library module Android Studio has no problem finding and referencing the view

Solution 1:

Update:

Synthetic view references have been deprecated and will not be supported in the future.

The current recommended solution is to use ViewBinding (or wait for Jetpack Compose)

Original Answer:

One way to solve this is to create an "alias" property that can be consumed from other modules:

// SyntheticExports.ktpackage com.example.synthetic.exported

import kotlinx.android.synthetic.main.layout_in_library.*

inlineval Activity.exported_text_view get() = text_view

Then on the other module:

// MainActivity.ktimport com.example.synthetic.exported.exported_text_view

exported_text_view.text = "example"

That works for us. Have to create different extensions for view, fragment, etc. It's a bit tedious to have to do them manually but this is the simplest workaround we found.

Extra: This is also a decent method to export synthetic extensions as part of a public library too, not just in an internal module.

Solution 2:

None of the above answers actually helped me . Add both these plugins in your app level build.gradle file on top :

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

This should solve the issue .

OR

plugins {
    id'kotlin-android-extensions'id'kotlin-android'
}

Solution 3:

I also got this issue and my solution is:

  • Don't use import kotlinx.android.synthetic.main....*

  • Use findViewById()

For example I changed from

textview_id.text = "abc"

to

findViewById(R.id.textview_id).text = "abc"

Solution 4:

The solution is far simpler than imagined, Kotlin synthetic sugars are a part of Kotlin Android Extensions and you just have to apply the plugin manually (Android Studio applies this automatically only to application modules):

Add the following to your library / feature module's build Gradle:

apply plugin: 'kotlin-android-extensions'

Now enjoy Kotlin synthetic sugars in your Kotlin code! ;)

Solution 5:

you could try switching to 1.2.30-eap-16 and adding

androidExtensions {
    experimental = true
}

to your build.gradle.

Post a Comment for "Unresolved Reference For Synthetic View When Layout Is In Library Module"