Nathan Bird
Nathan Bird

Reputation: 932

package R does not exist....or does it?

A few days ago, I accepted a contract from a returning client to make a few changes to his Android app over the course of the next few weeks. After getting the project source code from Github, I imported it into Android Studio (3.2) and noticed an error that I usually see pretty frequently but only for a short time during a Gradle sync: error: package R does not exist. Naturally, I shrugged my shoulders and ran 'Clean Project' and then 'Make Project.'

Nothing changed

Did a quick Google search and tried every 'solution' I could find. I tried every combination of "Make Project", "Make Module", "Clean Project", "Rebuild Project", "Sync Project with Gradle Files", "Sync with File System", "Invalidate Caches/Restart" imaginable. I restarted my laptop, updated SDK files, updated build tools, singled out each and every Gradle 'Implementation' line, and finally ended up uninstalling/reinstalling Android Studio as well as deleting all my IDE preferences just in case.

Still nothing

Next, I created a new project and noticed that the generated setContentView() line had the same error referencing the generated layout file. Wait. An untouched new project is having the same error?!? I went ahead and pressed 'run' and the project compiled without any errors contrary to what the real-time inspections are saying.

Switching back over to the original imported project, I tried running it and the compiler gives the same R package error. WHY?!?! There aren't any other errors, Android Lint runs cleanly, my Gradle implementations are recognized in the files, package names are all correct, and my AndroidManifest.xml file is perfectly fine.

If you need to see my build.gradle files, AndoridManifest, or anything else, comment and let me know.

Any ideas?

EDIT

Not a duplicate because as I mentioned, I've tried all of those solutions and none worked. I explained what I've tried, and why it's a unique situation.

Compare the contents of your build folder to mine and you should notice the difference: enter image description here

Notice that the R folder in the upper image is marked differently? That's a working one. The lower image shows the broken project. These were taken moments apart.

enter image description here

Upvotes: 4

Views: 10301

Answers (2)

Nathan Bird
Nathan Bird

Reputation: 932

So it turns out this painfully specific undocumented error had to do with having multiple build flavors all with different google-services.json files. In one of them, the package name was incorrect but because it was in a different source directory, Android Lint didn't have any context for which to compare the package name against and the R.java file wasn't being generated because there was technically an error when Gradle Sync was running.

I discovered this after many hours of tearing through the source files and re-constructing the project one file at a time in an empty project. There's nothing like the feeling you get when you finally resolve an error you've been struggling against for days!!

Upvotes: 4

Ben P.
Ben P.

Reputation: 54214

I can cause a similar error to occur if I edit AndroidManifest.xml to have a different package name than my source structure:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stackoverflow.garbage">

All of my code inside the com.example.stackoverflow package gives me the "package R does not exist" error. That's because these files don't import R.java, since normally the generated R.java file would be in the same package as these files. But it's not: it's in a com.example.stackoverflow.garbage package.

I can solve the problem by adding imports. But I can also solve the problem by changing my manifest's package attribute back to what I expect.

Upvotes: 2

Related Questions