The editor cannot find R, so every R.string and R.id turns red
Cannot 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
Every 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
Only 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
Your 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
The 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
- 1
What does the error look like?
Cannot resolve symbol 'R'Unresolved reference: RUnresolved reference: string (or id, layout, drawable) on R.string.app_nameerror: 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 | head2Fix
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:assembleDebug3Verify
The build passes and the red marks on R go away after the sync.
./gradlew :app:assembleDebug && echo build okConfirmed by developer.android.com
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 | head2Fix
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.R3Verify
The import resolves, and the build still passes.
./gradlew :app:compileDebugKotlin # use :app:compileDebugJavaWithJavac for Java-only modulesConfirmed by developer.android.com
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=false3Verify
The reference resolves without the flag, and the build passes.
./gradlew assembleDebugConfirmed by developer.android.com
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.xml2Fix
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:assembleDebugConfirmed by developer.android.com
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
Flutter Android build stops with Execution failed for task ':app:…'
Execution failed for task ':app:compileFlutterBuildRelease'.
The line only tells you which Gradle step broke; the task name in the quotes tells you why.
5 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fixMobile apps
Java and Kotlin compile to different JVM targets in an Android module
Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17)
Java and Kotlin tasks in the same module are compiling for different JVM targets, for example 1.8 and 17.
2 causes, each with a fixMobile apps
Manifest.permission.POST_NOTIFICATIONS shows red or does not resolve
Cannot resolve symbol 'POST_NOTIFICATIONS'
POST_NOTIFICATIONS exists only from API level 33.
4 causes, each with a fix