You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目编译报错:R.java文件语法错误求助

Fixing R.java Compilation Errors: ';' Expected & Identifier Expected

Hey there, let's tackle this frustrating compilation error you're hitting. First off, never manually edit the R.java file—it's auto-generated by the Android build tools, so any changes you make will get overwritten, and the root of the problem is always in your project's resource files, not R.java itself.

What's Causing This?

The space between font and size in that R.java line tells me there's a syntax mistake in one of your XML resource files (like layouts, styles, or value files). For example, you might have accidentally split a single attribute into two words—something like writing android:font size="14sp" instead of the correct android:textSize="14sp", or a custom attribute named incorrectly with a space. When the build tool (aapt) tries to parse this invalid XML, it generates broken code in R.java that throws those compilation errors.

Step-by-Step Fix:

  • Locate the problematic resource
    Look at the exact line in R.java—this line will be tied to a specific resource (like a layout ID, style, or dimension). Note the resource name, then find the corresponding XML file in your res/ directory.

  • Fix the XML syntax error
    Scan the XML file for attributes with spaces in their names. Common culprits include:

    • Miswritten system attributes (e.g., android:text Size instead of android:textSize)
    • Custom attributes with invalid spaces (e.g., app:font size instead of app:fontSize)
    • Typos in resource names that accidentally introduce spaces
  • Clean and rebuild your project
    Go to Build > Clean Project to wipe old build artifacts, then Build > Rebuild Project to regenerate a correct R.java file from your fixed resources. This ensures no cached bad code is hanging around.

  • Double-check R imports
    Make sure none of your Java/Kotlin files are importing android.R instead of your project's own com.yourpackage.R—this can cause unexpected conflicts even after fixing resources.

Example Scenario:

If your R.java error links to a TextView in activity_main.xml, you might find this broken line:

<TextView
    android:id="@+id/my_text"
    android:font size="16sp"  <!-- Wrong! -->
    android:text="Hello World"/>

Fix it to:

<TextView
    android:id="@+id/my_text"
    android:textSize="16sp"  <!-- Correct -->
    android:text="Hello World"/>

Once you fix the XML issue and rebuild, those R.java compilation errors should disappear.

内容的提问来源于stack exchange,提问作者pamo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:21:38