Mus*_*tlu 1 android kotlin android-resources android-instant-apps
我的Instant App项目中有一个base名为querymodule 的模块和功能模块.
我的QueryActivity内部query模块使用模块中的颜色base.
@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 Code Online (Sandbox Code Playgroud)
如果我尝试run该项目,它可以正常工作没有任何问题.但如果我rebuild是项目,它会给我以下错误:
../net/epictimes/uvindex/query/QueryActivity.kt
Error:(133, 63) Unresolved reference: color
Error:(134, 27) Unresolved reference: color
Run Code Online (Sandbox Code Playgroud)
指向那些颜色值.
我通过colors.xml在query模块中添加另一个文件并引用base它的颜色来解决这个问题.它工作正常.您可以在此提交中看到差异.
<color name="query_location_success_text">@android:color/white</color>
<color name="query_location_fail_text">@color/accent</color>
Run Code Online (Sandbox Code Playgroud)
现在它有效,但我不知道为什么.这是正确的方法吗?我的问题不应该是base功能模块中可访问的模块内的资源吗?
版本:
Android目标/编译SDK:26
科特林:1.1.50
即时应用:1.1.0
这是我的一个开源项目,你可以在这里看到整个项目.
谢谢
小智 9
是的,当您使用完全限定名称(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