I have an Android app where application module depends on a few library modules. Each of the library modules have: -
- Different package names defined in their
AndroidManifest.xml
- strings defined in their
res/values/strings.xml
Application module has a different package name defined in its AndroidManifest.xml
I am trying to access the strings from the library modules in my application module via
textView.setText(R.string.stringLibA);
textView1.setText(R.string.stringLibB);
However, I am getting compilation error when accessing R.string.stringLibB
saying
cannot find symbol R.string.stringLibB
But it works fine if I access stringLibB
using library's package name
textView.setText(R.string.stringLibA);
textView1.setText(com.package.libraryB.R.string.stringLibB);
Can someone please guide me what am I doing here that I cannot access libraryB
's strings from application module's R class directly?
I apologize I could not share actual code from the project because of privacy concerns.
EDIT
My application module's build.gradle
's dependency block
dependencies {
implementation project('libraryA')
implementation project('libraryB')
}
libraryA
and libraryB
are applying library plugin in their build.gradle
apply plugin: 'com.android.library'
I am trying to access strings declared in libraryA
and libraryB
inside of my application via application's R class
CodePudding user response:
Please make sure your A module is a Library Module. Then all A module's resources will be built into its R class and merged to B module's R class
. That means you can get R.string.stringLibB
by both com.myproject.libraryA.R.string.stringLibA
and com.myproject.libraryB.R.string.stringLibB.
I suggest you do "File->Invalidate Caches" or "Build->Rebuild Project"
, it should work.
You can also refer : https://developer.android.com/studio/projects to see more detail.
CodePudding user response:
Check your dependency
first, make sure all of your modules
are declared properly. If you implement module
properly then you should have access to the modules component
including resources
.
Sometimes, IDE errors happen. Simply, Invalidate & Restart
will solve your problems.