无法以编程方式从另一个模块获取资源

时间:2018-04-11 16:40:58

标签: android android-resources android-module

我有一个带有几个gradle模块的android项目。模块之间的依赖关系如下:
app< - coremodule< - featuremodule

coremodule中有一些资源(字符串和颜色)。

当我在featuremodule的布局中使用它们时一切正常,它们是可用的并且按预期工作。 但是,当我尝试从featuremodule中的Activity中获取它们时,我得到一个异常:未解析的引用:R

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

有没有人知道它为什么会发生以及如何解决这个问题?

4 个答案:

答案 0 :(得分:0)

我认为R指向您应用的资源。检查文件开头的导入。

您应该在方法中明确指出Resource文件夹,如下所示:

myTextView.setText(com.coremodule.R.string.res_from_core_module)

答案 1 :(得分:0)

请像这样使用:

SELECT * FROM [REOEM] WHERE ID IN (SELECT ID FROM [REOEM] WHERE Manufacturer LIKE '*1155*' OR Type LIKE '*1155*' OR Make LIKE '*1155*' OR Lester LIKE '*1155*' OR OEM1 LIKE '*1155*' OR OEM2 LIKE '*1155*' OR OEM3 LIKE '*1155*' OR OEM4 LIKE '*1155*' OR OEM5 LIKE '*1155*' OR OEM6 LIKE '*1155*' OR OEM7 LIKE '*1155*' Or OEM8 Like '*1155*' Or OEM9 Like '*1155*'OR OEM10 LIKE '*1155*' OR OEM11 LIKE '*1155*' OR OEM12 LIKE '*1155*' OR OEM13 LIKE '*1155*' OR OEM14 LIKE '*1155*' OR OEM15 LIKE '*1155*' OR OEM16 LIKE '*1155*' OR OEM17 LIKE '*1155*' OR OEM18 LIKE '*1155*' OR OEM19 LIKE '*1155*' OR OEM20 LIKE '*1155*' OR OEM21 LIKE '*1155*' OR OEM22 LIKE '*1155*' OR OEM23 Like '*1155*' OR OEM24 LIKE '*1155*' OR OEM25 LIKE '*1155*' OR OEM26 LIKE '*1155*' OR OEM27 LIKE '*1155*' OR OEM28 LIKE '*1155*' OR OEM29 LIKE '*1155*' OR OEM30 LIKE '*1155*' OR OEM31 LIKE '*1155*' OR OEM32 LIKE '*1155*' OR OEM33 LIKE '*1155*' OR OEM34 LIKE '*1155*' OR OEM35 LIKE '*1155*' OR OEM36 LIKE '*1155*' OR OEM37 LIKE '*1155*' OR OEM38 LIKE '*1155*'
OR OEM39 LIKE '*1155*' OR OEM40 LIKE '*1155*' OR OEM41 LIKE '*1155*' OR OEM42 LIKE '*1155*' OR OEM43 LIKE '*1155*' OR OEM44 LIKE '*1155*' OR OEM45 LIKE '*1155*' OR OEM46 LIKE '*1155*' OR OEM47 LIKE '*1155*' OR OEM48 LIKE '*1155*' OR OEM49 LIKE '*1155*' OR OEM50 LIKE '*1155*' OR OEM51 LIKE '*1155*' OR OEM52 LIKE '*1155*' OR OEM53 LIKE '*1155*' OR OEM54 LIKE '*1155*' OR OEM55 LIKE '*1155*' OR OEM56 LIKE '*1155*' OR OEM57 LIKE '*1155*' OR OEM58 LIKE '*1155*' OR OEM59 LIKE '*1155*' OR OEM60 LIKE '*1155*' OR OEM61 LIKE '*1155*' OR OEM62 LIKE '*1155*' OR OEM63 LIKE '*1155*' OR OEM64 LIKE '*1155*' OR OEM65 LIKE '*1155*' OR OEM66 LIKE '*1155*' OR OEM67 LIKE '*1155*' OR OEM68 LIKE '*1155*' OR OEM69 LIKE '*1155*' OR OEM70 LIKE '*1155*' OR OEM71 LIKE '*1155*' OR OEM72 LIKE '*1155*' OR OEM73 LIKE '*1155*' OR OEM74 LIKE '*1155*' OR OEM75 LIKE '*1155*' ); 

答案 2 :(得分:0)

这种行为的原因是通过 compileOnly 添加了“ coremodule”依赖项。

应用程序模块的

build.gradle如下:

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

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

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

答案 3 :(得分:0)

将此添加到所有模块以共享资源、类、函数

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])

    ...
}