0

Basically, I have a class, ClassThatUsesFoo, which uses class Foo. However, I have two different versions of Foo, each in a separate build variant. In variantB, Foo needs to access a resource, whereas variantA does not.

The issue is that, when I activate variantB, Android Studio thinks it can't resolve R.someRawResource inside variantB's version of Foo, even though when I actually compile and run the app, everything works as expected. (The resource is resolved just fine).

Here's what the structure looks like:

MyProject
    |- MyModule
        |- src
            |- main
            |   |- java
            |   |   |- ClassThatUsesFoo.java
            |   |
            |   |- res (Empty)
            |   
            |- variantA
            |   |- java
            |   |   |- Foo.java (Version of Foo that does NOT use resources)
            |   |
            |   |- res (Empty)
            |
            |- variantB
            |   |- java
            |   |   |- Foo.java (Version of Foo that DOES use resources)
            |   |
            |   |- res
            |   |   |- raw
            |   |   |   |- someRawResource.file

And here's the relaveant snippet from the Gradle file:

flavorDimensions 'type'
productFlavors {
    variantA {
        dimension 'type'
    }
    variantB {
        dimension 'type'
    }
}

Just to be clear here, both build variants compile and run as expected. The issue is Android Studio is confused.

Things I have tried:

  • Resync with Gradle files
  • Rebuild project
  • Clean project

Any idea what could be wrong here? It seems like there might be a bug in Android Studio, since everything compiles and runs as expected...


System information:

  • Ubuntu 16.04
  • Android Studio v3.4.2
  • if it builds and runs the app, then there's no real issue ? – a_local_nobody Aug 09 '19 at 18:48
  • "Android Studio thinks it can't resolve R.someRawResource inside variantB's version of Foo" -- if you look at the code-generated `R` class for your variant, does your resource appear there? – CommonsWare Aug 09 '19 at 18:49
  • @a_local_nobody I mean *technically* no, but it's very annoying to have Android Studio underlining the file with a squiggly red line when there's nothing wrong with it – You'reAGitForNotUsingGit Aug 09 '19 at 18:51
  • @CommonsWare hmm if I right click on `R` and say `Go To Declaration`, it says `Cannot find declaration to go to` – You'reAGitForNotUsingGit Aug 09 '19 at 19:00
  • @CommonsWare ok I found it manually. Yes it **does** contain the resource ID for it, however, the icon for the file shows a page with a small "J", rather than the normal large "C" icon... – You'reAGitForNotUsingGit Aug 09 '19 at 19:03
  • Hmmmm... usually that means Android Studio does not think that directory is part of the active source set. You are getting the "J" effect when you have variantB selected in the Build Variants tool? – CommonsWare Aug 09 '19 at 19:05
  • @CommonsWare yes, correct. I'm only talking about the `R` class though - the `Foo` source file has the "C" icon. – You'reAGitForNotUsingGit Aug 09 '19 at 19:08
  • In general, I agree with your assertion: if everything runs fine, but Android Studio is confused, it's probably an Android Studio bug. If you can create a small project that reproduces the problem, file a bug report and include that project as evidence. If you think of it, pass the issue link along. – CommonsWare Aug 09 '19 at 19:18
  • @CommonsWare so I was unable to reproduce in a minimal project. I thought maybe it was a problem with the gradle plugin version or the gradle version. So I changed those versions in the minimal project to match the versions in my real project, but I was still unable to repro. For the heck of it, I updated my real project to plugin `v3.4.2` and Gradle `v5.1.1`. Upon trying to build I got some crazy error about "apkdata" can't be null or something. But then, after an Invalidate Caches and Restart, everything started working properly.... – You'reAGitForNotUsingGit Aug 09 '19 at 20:07
  • Ah, the old "beat on it with a $5 wrench until it starts behaving" trick... :-) Anyway, I am glad to hear that you have at least temporarily cleared up the problem! – CommonsWare Aug 09 '19 at 20:11
  • @CommonsWare do you think I should add it as an answer, or wait to see if the problem comes back? Also, on an unrelated note, do you have any ideas on [this other problem I'm having](https://stackoverflow.com/questions/57245064/android-studio-library-source-does-not-match-the-bytecode-for-class)? – You'reAGitForNotUsingGit Aug 10 '19 at 16:14
  • "do you think I should add it as an answer" -- your solution will be hard to reproduce, so my instinct would be to not post an answer. "do you have any ideas on this other problem I'm having?" -- no, sorry. – CommonsWare Aug 10 '19 at 16:18

0 Answers0