重建Android Instant App后,无法使用功能模块中基本模块中定义的颜色 [英] Using colors defined in base module from feature module fails after rebuilding the Android Instant App

查看:98
本文介绍了重建Android Instant App后,无法使用功能模块中基本模块中定义的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Instant App项目中有一个base模块和一个名为query模块的功能模块.

我在query模块中的QueryActivity使用的是base模块中的颜色.

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
}

如果我尝试run项目,它可以正常工作,没有任何问题.但是,如果我rebuild项目,它会给我以下错误:

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

指向这些颜色值.

我通过在query模块内添加另一个colors.xml文件并引用其中的base颜色来解决此问题.工作正常.您可以在此提交中看到差异.

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

现在它可以工作了,但我不确定为什么.这是正确的方法吗?我的问题是base模块中的资源是否可以从功能模块访问?

版本:

Android目标/编译SDK:26

科特琳:1.1.50

即时应用:1.1.0

这是我的一个开源项目,您可以在此处看到整个项目. /p>

谢谢

解决方案

是的,当您使用完全限定的名称(package_name.R.resource_name)引用基本模块中的资源时,可以从功能模块中访问它.

基本模块和子模块具有不同的软件包名称(您的基本功能软件包名称为net.epictimes.uvindex,而功能模块软件包名称为net.epictimes.uvindex.query).

每个包都包含自己的资源集,并且它们的资源ID在编译过程中收集在单独的R包中:

  • net.epictimes.uvindex.R-用于基本功能模块
  • net.epictimes.uvindex.query.R-适用于您的功能模块

要从查询"功能模块访问基本功能的重点"颜色资源,请使用net.epictimes.uvindex.R.color.accent标识符:

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

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

解决方案

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

这篇关于重建Android Instant App后,无法使用功能模块中基本模块中定义的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆