android - 重建 Android Instant App 后,使用功能模块中基本模块中定义的颜色失败

标签 android kotlin android-resources android-instant-apps

我的 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
}

如果我尝试运行 项目,它工作正常,没有任何问题。但是如果我重建项目,它会给我以下错误:

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

指向那些颜色值。

我通过在 query 模块中添加另一个 colors.xml 文件并从中引用 base 颜色来解决这个问题。它运作良好。您可以在 this commit 中看到差异.

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

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

版本:

Android target/compile SDK: 26

Kotlin: 1.1.50

Instant Apps: 1.1.0

这是我的一个开源项目,你可以看到整个项目here .

谢谢

最佳答案

是的,当您使用完全限定名称 (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

关于android - 重建 Android Instant App 后,使用功能模块中基本模块中定义的颜色失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46452367/

相关文章:

android - 使用intellij开发Android应用程序

java - 尝试在空对象引用上调用虚拟方法 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)'

java - IllegalStateException textview 在 Kotlin 中不能为 null

android - 无法从 Android 中的 dataSnapShot 获取数据

android - 我们如何使用数据绑定(bind)从字符串资源中获取 SpannedString 对象?

android - 从 shape drawable 获取渐变资源

java - 比较 byte[] 的各个部分以确定两个 byte[] 是否相同

android - 如何在 Kotlin 中处理后按

android - 如何解决android studio中的那个错误?

android - 如何用前导零格式化资源字符串?