Android: Importing resources on featureModule vs library module
Asked Answered
B

0

8

App Structure

featureModule = apply plugin: 'com.android.dynamic-feature'

libraryModule = apply plugin: 'com.android.library'

Problem - Using any library resources in feature module requires full package name qualifier which eventually creates a problem for databinding generated classes in feature module.

Description - I have this above app structure. When I am trying to use any library resource in any featureModule it requires to specify the resource with full package name qualifier because by default I have the feature resource R as the main import.

So for any kotlin class let say activity code looks like this

feature-1/TestActivity.kt

internal class TestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityTestBinding = DataBindingUtil.setContentView(this, R.layout.activity_test)
        binding.testName.text = getText(R.string.abc_test) // this resource is present in core-lib module
    }
}

The above will lead to an unresolved reference error which you can resolve by using full-name so instead of R.string.abc_test use com.blah.core-lib.R.string.abc_test.

R.layout.activity_test

<layout xmlns:android="http://schemas.android.com/apk/res/android">


    <TextView
        android:id="@+id/test_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/spacing_medium"
        android:text="@{@string/abc_test}" />
</layout>

But for XML-databinding it creates a bigger problem. Since R.layout.abc_test is in feature module it's not able to access the library resource. I can resolve this by using a utility class & let the utility class return the string with full-package name qualifier but this problem sort of restricts many feature of databinding where I can't use the resource directly in the XML itself.

PS: This all works fine if I convert my feature module to a library module. Even at the code level I don't end up using full package name qualifier if it's a library module.

Does anyone have any better suggestions to resolve this problem for feature modules?

Blumenfeld answered 31/7, 2020 at 23:0 Comment(1)
I faced the same issue when using @BindingAdapter from core module in feature module. I couldn't find a solution either, so created new binding adapter in feature module.Equanimous

© 2022 - 2024 — McMap. All rights reserved.