As mentioned in the comment by @cesards above this is an on going issue with the Kotlin Android Extentions. And it is not solved as of today.
Good: Use Custom Views
My primary suggestion is encapsulating the view and the relevant behavior as a custom view and instead of including it via <include>
tag, use it directly in your layout as <com.example.app.YourCustomView>
.
This will work no matter your view class is in another module or not.
Bad and Ugly: Id Collision Workaround
However I found a hacky workaround if you only want to get one reference from the included view.
Follow these steps to use kotlin synthetic imports for the layouts included from other modules:
- Give an id to the view you want to get reference of in the included xml file.
- Give the same id to the include tag in the including xml file.
- Now synthetic import the view (id) from the including layout (not from the included)
I'm not really sure how and why this works but it's a very fragile and dirty way of reusing layouts.
Example:
Your including layout (fragment_example.xml)
<include
android:id="@+id/exampleView"
layout="@layout/example_layout" />
Your included layout (example_layout.xml)
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/exampleView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</merge>
Your fragment class (ExampleFragment.kt)
import kotlinx.android.synthetic.main.fragment_example.exampleView
// Do not import the exampleView through example_layout.exampleView
class ExampleFragment : Fragment() {
// Do something with imported exampleView
}