4

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

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

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

So android:text="@string/res_from_core_module" works and myTextView.setText(R.string.res_from_core_module) doesn't work.

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

Mikhail Sidorov
  • 699
  • 2
  • 9
  • 18

5 Answers5

2

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

build.gradle for app module looked like:

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

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

dependencies {
    ...
    implementation project(path: ':coremodule')
    ...
}
Mikhail Sidorov
  • 699
  • 2
  • 9
  • 18
1

I think the R points to the Resources of your app. Check the imports at the beginning of the file.

You should explicitly point the Resource folder in the method like this:

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

  • If I'm not mistaken R file is a common for project and modules. In any case it was the first thing which I tried - no changes. The error was the same : "Unresolved reference: R" – Mikhail Sidorov Apr 12 '18 at 08:10
1

Add this to all modules to share resources, classes, functions

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

    ...
}
hungtran273
  • 1,180
  • 9
  • 11
-1

Please use like this:

myTextView.setText(getString(R.string.res_from_core_module));
Raman Sharma
  • 159
  • 1
  • 5
  • setText(@StringRes) call setText(getString(@StringRes)) , so in fact there is no difference between calling this 2 methods and as a result i got the same error "Unresolved reference: R" – Mikhail Sidorov Apr 12 '18 at 08:15
-1

Latest answer !!! It will make everything accessible to your app module.

dependencies {
    ...
    api project(': coremodule')
    ...
}
Shahbaz Hashmi
  • 2,631
  • 2
  • 26
  • 49