无法以编程方式从另一个模块获取资源 [英] Can't programmatically get resource from another module

查看:60
本文介绍了无法以编程方式从另一个模块获取资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个gradle模块的android项目.模块之间的依存关系如下所示:
应用<-核心模块<-功能模块

I have an android project with several gradle modules. Dependencies beetween modules look like this:
app <-- coremodule <-- featuremodule

coremodule中有资源(字符串和颜色).

There are a resources in coremodule (strings and colors).

当我在Featuremodule的布局中使用它们时,一切正常,它们可用并且可以按预期工作.但是,当我尝试通过功能模块从Activity中以编程方式获取它们时,出现异常:未解析的引用:R

When I use them in layout from featuremodule everything is OK, they are avaliable and work as expected. But when I try to get them programmaticully in Activity from featuremodule I get an exception: Unresolved reference: R

所以 android:text ="@ string/res_from_core_module" 有效 myTextView.setText(R.string.res_from_core_module) 不起作用.

有人知道为什么会发生这种情况以及如何解决吗?

Have anyone ideas why it happens and how to solve this?

推荐答案

这种行为的原因是通过 compileOnly 添加了"coremodule"依赖项.

The reason for such behavior was adding 'coremodule' dependency with compileOnly.

build.gradle如下:

build.gradle for app module looked like:

dependencies {
    ...
    compileOnly project(path: ':coremodule')
    ...
}

如果使用实现(或API)更改仅编译,一切正常

if change compileOnly with implementation (or api) everything is OK

dependencies {
    ...
    implementation project(path: ':coremodule')
    ...
}

这篇关于无法以编程方式从另一个模块获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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