Unresolved reference for synthetic view when layout is in library module

前端 未结 10 2091
醉梦人生
醉梦人生 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:40

    In an incremental improvement on @pablisco's solution above, in my library I have a file:

    // SyntheticExports.kt
    @file:Suppress("ClassName")
    
    package com.example.library.synthetic.main
    
    import android.view.TextView
    import android.view.View
    import android.view.ViewGroup
    import kotlinx.android.synthetic.main.common_layout.view.rootLayout as syntheticRootLayout
    import kotlinx.android.synthetic.main.common_layout.view.retryButton as syntheticRetryButton
    
    object common_layout {
        object view {
            inline val View.rootLayout: ViewGroup get() = syntheticRootLayout
            inline val View.retryButton: TextView get() = syntheticRetryButton
        }
    }
    

    Then on the other module:

    // MainActivity.kt
    import com.example.library.synthetic.main.common_layout.view.rootLayout
    import com.example.library.synthetic.main.common_layout.view.retryButton
    
    rootView.rootLayout.isVisible = true
    rootView.retryButton.setOnClickListener { doIt() }
    

    If this issue is ever fixed, I just need to change imports to start with "kotlinx.android" instead of "comp.example.library".

提交回复
热议问题