Error: No resource found that matches the given name
(at 'label' with value '@string/app_name')
在 AndroidManifest.xml 的 application 和 activity 標籤中引用 @string/app_name 報錯。
res/values/strings.xml 中缺少 app_name 字串資源定義,或文件結構錯誤導致資源編譯失敗。
<!-- res/values/strings.xml -->
<resources>
<string name="app_name">My Application</string>
<!-- 其他字串 -->
</resources>
res/values/ 而非 res/values-v21/ 等限定資源目錄(除非故意按版本區分)。strings.xml 是否有未閉合標籤、特殊字符未轉義(& → &)、編碼非 UTF-8。values,不能是 value、vals 等。./gradlew clean 或 AS File → Invalidate Caches / Restart。app_name,庫不應強依賴。tools:replace="android:label" 可能導致資源引用丟失。# 終端執行
./gradlew clean assembleDebug
# 或 AS: Build → Clean Project → Rebuild Project
若編譯通過,說明資源已正確識別。
<!-- strings.xml -->
<string name="app_name" translatable="false">MyApp</string>
<!-- translatable="false" 避免翻譯工具誤改 -->
在 build.gradle 中同步:
android {
defaultConfig {
applicationId "com.example.myapp"
// 可選:從 strings.xml 讀取 (需自定義任務)
}
}
@string/app_name 找不到資源 99% 是 **strings.xml 缺少對應定義** 或 **XML 格式錯誤導致資源編譯失敗**。補全定義、Clean Rebuild 即可解決。養成在 strings.xml 集中管理所有可見字串的習慣,利於國際化與維護。