Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

The editor cannot find R, so every R.string and R.id turns red

ErrorCannot resolve symbol 'R'

Short answer

R is a class that the build generates from your res folder, in the package named by the module's namespace. The editor shows it as missing when the build has not produced it, when your file imports a different R or none, or when the resource lives in a library and non-transitive R classes keep it out of your module's R. Check the build, namespace and imports in that order.

  • Applies to Android Studio current stable, Android Gradle Plugin 8.0 and later, Gradle 8.x and 9.x, Kotlin and Java sources
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Build the module from the command line. If it fails, the red R in the editor is only a result, and the first error in the output is the real problem. If it succeeds, the cause is in how your file refers to R.

    ./gradlew :app:assembleDebug 2>&1 | grep -n -m5 "error\|FAILED\|What went wrong"
  2. 2Match what you see, then jump to its fix

    1. 1Every R reference in the module is red, and the Build window shows an error in a resource file, the manifest or a Gradle fileThe build has not generated R yet because a build or sync failedFix
    2. 2Only some files fail; they sit in a sub-package, or they have import android.RThe file imports no R, or the wrong one, because it is not in the namespace packageFix
    3. 3Your own resources resolve, but R.string.something that is defined in a library or other module does not, since you moved to AGP 8 or created the project with a recent Android StudioA resource from a library module is looked up in the app's RFix
    4. 4The build or sync reports that no namespace is set, or R is generated in a package you do not expect, right after an AGP upgradenamespace was left in the manifest after upgrading to AGP 8Fix

What does the error look like?

  • Cannot resolve symbol 'R'
  • Unresolved reference: R
  • Unresolved reference: string (or id, layout, drawable) on R.string.app_name
  • error: cannot find symbol variable R or package R does not exist

Cause 1 of 4

The build has not generated R yet because a build or sync failed

1Check

Android's resources guide says the R class is generated when the app is compiled, with one ID for each resource in res/. If the build stops early, there is no R to resolve, and the editor cannot tell you which file is wrong. Run the build and read the first error. After you change a Gradle file, Android Studio also needs a sync.

./gradlew :app:assembleDebug
ls app/build/generated 2>/dev/null | head

2Fix

Fix the first error that the build prints, often an invalid resource name or a malformed XML file, then run Sync Project with Gradle Files from the File menu and build again.

./gradlew :app:assembleDebug

3Verify

The build passes and the red marks on R go away after the sync.

./gradlew :app:assembleDebug && echo build ok

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 4

The file imports no R, or the wrong one, because it is not in the namespace package

1Check

Android's build docs say each module has a namespace, used as the package of its generated R class (for example com.example.myapp.R). A file in that same package sees R without an import. Files in other packages must import R using the namespace, and a stray import android.R makes R.string.my_text refer to Android's own resources, which do not contain yours.

grep -n "namespace" app/build.gradle*
grep -rn "import android.R$\|import .*\\.R$" app/src/main/java | head

2Fix

Import the R of your namespace in each file that sits in another package, and delete import android.R unless you really mean a platform resource such as android.R.drawable.ic_menu_info_details. Do not change the namespace to match a file that is in the wrong place; move the file or add the import.

// Kotlin or Java file outside the namespace package
import com.example.myapp.R

3Verify

The import resolves, and the build still passes.

./gradlew :app:compileDebugKotlin   # use :app:compileDebugJavaWithJavac for Java-only modules

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 4

A resource from a library module is looked up in the app's R

1Check

Non-transitive R classes are the default from Android Gradle plugin 8.0: the R class of a module holds only that module's own resources, not those of its dependencies. Resources defined in a library must be read through the library's own R, which is in the library's namespace.

grep -n "nonTransitiveRClass" gradle.properties
grep -n "namespace" */build.gradle*

2Fix

Refer to the resource through the R of the module that defines it, by import or by full name. For an old project that still relied on transitive R classes, Android Studio offers Refactor > Migrate to Non-Transitive R Classes. Turning the flag off restores the old behaviour, but the default exists to speed up builds.

// module :feature uses a string defined in :core (namespace com.example.core)
import com.example.core.R as CoreR
val title = getString(CoreR.string.screen_title)
# only as a stopgap, gradle.properties
android.nonTransitiveRClass=false

3Verify

The reference resolves without the flag, and the build passes.

./gradlew assembleDebug

Confirmed by developer.android.com

Did this fix it?

Cause 4 of 4

namespace was left in the manifest after upgrading to AGP 8

1Check

Since AGP 8.0 the namespace must be set in the module's Gradle file, not through the package attribute of AndroidManifest.xml. The namespace decides the package of R and BuildConfig. Look at both places.

grep -n "namespace" app/build.gradle*
grep -n "package=" app/src/main/AndroidManifest.xml

2Fix

Move the package name from the manifest into the namespace property of the android block and remove the package attribute. Keep applicationId explicit so the app's installed identity does not change.

// app/build.gradle.kts
android {
    namespace = "com.example.myapp"
    defaultConfig {
        applicationId = "com.example.myapp"
    }
}

3Verify

R is generated as com.example.myapp.R and the build passes.

./gradlew :app:assembleDebug

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Treat a red R as a build problem first: run the Gradle build before you chase imports.
  • Keep namespace and applicationId the same unless you have a reason; set applicationId explicitly if they differ.
  • In multi-module projects read a module's resources through that module's own R and keep non-transitive R classes on.
  • Never import android.R as a habit; use the fully qualified android.R only where you need a platform resource.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.