Android Instant Apps - Cannot Find Symbol From Bas

2019-02-15 00:30发布

问题:

I have a base feature module, and a feature module (you could call it the "child"). The base feature module has a strings.xml file asset containing:

<resources>
   <string name="app_string">Test String</string>
</resources>

I attempt to reference this string resource in the "child" feature's activity, as below:

int resId = R.string.app_string;

Android Studio appears to respect this reference, and will even direct me to the app_string resource when I click it. However, during compilation, I am met with the following error message:

Error:(13, 25) error: cannot find symbol variable app_string

The build Gradle file for my "child" feature has the dependency too:

dependencies {
   ...
   implementation project(':base')
}

I also tried compile project(':base'), but no success.

Is there something blatant that I am missing?

回答1:

Your base and child modules have different package names - let's say they are com.example.base and com.example.child. Each contains its own set of resources, and their identifiers will be collected in separate R packages:

  • com.example.base.R
  • com.example.child.R

Because you're trying to access a resource defined in base module, you need to reference it with the fully qualified name of the variable, which is com.example.base.R.string.app_string.