Unresolved reference for synthetic view when layout is in library module

前端 未结 10 2012
醉梦人生
醉梦人生 2020-12-12 21:37

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 refer

相关标签:
10条回答
  • 2020-12-12 21:56

    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! ;)

    0 讨论(0)
  • 2020-12-12 21:57

    My scenario was : I needed base class (from lib) view references in child classes (app module) and had minimum number of view references from Library Module's base classes.

    I would say, it's kind a fix and not a solution.

    Solution #1 Get a view reference from Base class

    //lib module snippet
    import kotlinx.android.synthetic.main.view_user_input.*
    
    class LibModuleBaseActivity : AppCompatActivity() {
    
       override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.view_user_input)
       }
    
        protected val vFirstName: TextView by lazy { tvFirstName }
        ...........
    }
    
    
    //app module snippet
    class AppModuleActivity : LibModuleBaseActivity() {
    
        fun setUserDetails(name: String) {
            vFirstName.text = name
        }
    }
    

    Solution #2 Get the task done by Base Class

    //lib module snippet
    import kotlinx.android.synthetic.main.view_user_input.*
    
    class LibModuleBaseActivity : AppCompatActivity() {
        protected fun setUserDetails(name: String) {
            tvFirstName.text = name
        }
        ...........
    }
    
    
    //app module snippet
    class AppModuleActivity : LibModuleBaseActivity() {
        ............
        setUserDetails("user_name")
    }
    

    Note: This works only if you are inheriting from lib module and where your Base class is inflating views.

    0 讨论(0)
  • 2020-12-12 22:02

    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.kt
    package com.example.synthetic.exported
    
    import kotlinx.android.synthetic.main.layout_in_library.*
    
    inline val Activity.exported_text_view get() = text_view
    

    Then on the other module:

    // MainActivity.kt
    import 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.

    0 讨论(0)
  • 2020-12-12 22:03

    None of the above answers actually helped me . Add both these plugins in your library module's build.gradle file on top :

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

    This should solve the issue .

    0 讨论(0)
提交回复
热议问题