Using colors defined in base module from feature m

2019-04-16 05:22发布

I have a base module and a feature module called query module in my Instant App project.

My QueryActivity inside query module uses colors that are in base module.

QueryActivity.kt:

@ColorInt
val textColor: Int = when (resultCode) {
    FetchAddressIntentService.RESULT_SUCCESS -> android.R.color.white
    FetchAddressIntentService.RESULT_FAILURE -> R.color.accent // this color is inside the base module
    else -> R.color.accent // this color is inside the base module
}

If I try to run the project, it works fine without any problem. But If I rebuild the project, it gives me the following error:

../net/epictimes/uvindex/query/QueryActivity.kt
Error:(133, 63) Unresolved reference: color
Error:(134, 27) Unresolved reference: color

Pointing to those color values.

I solved this by I adding another colors.xml file inside the query module and referencing the base colors from it. It worked fine. You can see the diff in this commit.

<color name="query_location_success_text">@android:color/white</color>
<color name="query_location_fail_text">@color/accent</color>

Right now it works but I am not sure why. Is this the right way to do it? My question is shouldn't be the resources inside base module accessible from the feature modules?

Versions:

Android target/compile SDK: 26

Kotlin: 1.1.50

Instant Apps: 1.1.0

That is a open source project of mine, you can see whole project here.

Thank you

1条回答
Lonely孤独者°
2楼-- · 2019-04-16 05:33

Yes, resource inside base module is accessible from the feature modules when you reference it with the fully qualified name (package_name.R.resource_name).

Base and child modules have different package names (your base feature package name is net.epictimes.uvindex, and your feature module package name is net.epictimes.uvindex.query).

Each package contains its own set of resources, and their resource IDs are collected in separate R packages during compilation:

  • net.epictimes.uvindex.R - for your base feature module
  • net.epictimes.uvindex.query.R - for your feature module

To access an ‘accent’ color resource of a base feature from your ‘query’ feature module, use net.epictimes.uvindex.R.color.accent identifier:

FetchAddressIntentService.RESULT_FAILURE -> net.epictimes.uvindex.R.color.accent

查看更多
登录 后发表回答