Android Studio新手求助:无法解析符号'R'问题如何解决?
Hey there! I’ve been right where you are—staring at that annoying red squiggle under R when you’re just getting started with Android development, and the usual fixes (importing, cleaning/building) aren’t cutting it. Let’s dive into some deeper troubleshooting steps that often resolve this:
First, hunt down XML errors (they’re the #1 culprit)
TheRclass is auto-generated from your project’s XML resources, so even a tiny syntax mistake in any XML file will break it. Check every XML file:- Layouts (
res/layout/): Look for unclosed tags, typos in attribute names (likeandroid:layout_widthmisspelled), or invalid resource references. res/values/files (strings, colors, styles): Ensure resource names only use lowercase letters, underscores, and numbers—no spaces, uppercase, or special characters.AndroidManifest.xml: Confirm your package name matches the one in your Java files (e.g., if your Java file starts withpackage com.example.andro.myapp;, the manifest’spackageattribute should be the same).
- Layouts (
Double-check your module-level
build.gradle
Open thebuild.gradlefile for your app module (not the project-level one) and verify:compileSdkVersionandtargetSdkVersionmatch an SDK Platform you’ve installed (check in SDK Manager). For example, if you’re targeting Android 14, both should be set to34.- There are no typos or invalid dependencies. A broken dependency can prevent the
Rclass from generating.
Invalidate Caches & Restart (this is magic sometimes)
Android Studio’s cache can get corrupted, which messes up auto-generated files likeR. Do this:- Go to
File → Invalidate Caches... - Check the box for "Invalidate and Restart"
- Let Studio restart and rebuild—this clears out stale cache files that clean/build doesn’t touch.
- Go to
Make sure you’re importing the right
R
If you accidentally importedandroid.R(the system’s default resource class), that’ll cause this error. Delete any line that saysimport android.R;, then let Android Studio auto-import your project’sR(or manually addimport com.example.andro.youractualpackagename.R;replacing with your real package name).Verify your project structure is correct
- Your Java files should live in
src/main/java/com/example/andro/...—not intestor other non-source directories. - All resource files (layouts, drawables, etc.) must be in
src/main/res/with correct folder names (e.g.,drawable,layout—no typos likedrawablev24instead ofdrawable-v24).
- Your Java files should live in
Check your SDK setup
OpenTools → SDK Managerand confirm:- You have the SDK Platform installed for your
compileSdkVersion. - Your Build Tools version is compatible (usually, Studio will suggest the latest compatible one if there’s an issue).
- You have the SDK Platform installed for your
If none of these work, try creating a brand new blank Android project. If the new project generates R correctly, compare its configuration files (build.gradle, AndroidManifest.xml) with your original project—you’ll likely spot a misconfiguration that’s breaking things.
内容的提问来源于stack exchange,提问作者Adi Harel

