9

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?

1
  • share complete code and error details Commented Apr 11, 2018 at 17:35

5 Answers 5

4

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') ... } 
Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

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"
1

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

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

1 Comment

how does it work if I don't use dependencies from disk?
-1

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

dependencies { ... api project(': coremodule') ... } 

Comments

-2

Please use like this:

myTextView.setText(getString(R.string.res_from_core_module)); 

1 Comment

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"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.