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

无法在colors.xml中创建新颜色项,报错未在基础values文件夹声明

Fixing "The color 'colorRed' has no declaration in the base values folder" Error

Hey Krishna, let’s work through this Android resource error you’re hitting—this is a common gotcha with how Android handles resource qualifiers, and we can get it sorted out.

First, let’s break down what the error means: Android requires that any resource defined in a qualifier-specific folder (like values-night/, values-v21/, or any folder with a configuration suffix) must have a matching declaration in the base values/ folder. Without that base definition, devices running on configurations that don’t match the qualifier will fail to find the resource and crash.

Here are step-by-step fixes to try, even after you’ve cleaned/rebuilt and invalidated caches:

1. Verify the base values/colors.xml has colorRed

First and foremost, make sure your res/values/colors.xml (the base, unqualified folder) includes a definition for colorRed. It should look something like this:

<resources>
    <!-- Other colors here -->
    <color name="colorRed">#FF0000</color>
</resources>

Double-check for typos—even a small difference (like color_red vs colorRed) will trigger this error.

2. Check qualifier-specific colors.xml files

Your qualifier folders (e.g., values-night/colors.xml) should only override the base colorRed value, not define it for the first time. For example, in a night mode folder, it should just have:

<resources>
    <color name="colorRed">#CC0000</color> <!-- Only overrides the base value -->
</resources>

Don’t add extra resource declarations here—stick to just the value override.

3. Use Android Studio’s Resource Manager to confirm

Open the Resource Manager (left sidebar, looks like a square with a paintbrush) and search for colorRed. You’ll see which folders contain the resource. If the base values/ folder is missing, right-click the resource and select Add to base values to auto-create the base declaration.

4. Perform a deep cache reset (beyond Invalidate Cache)

Sometimes Android Studio’s cached data sticks around even after Invalidate Cache & Restart. Try this:

  • Close Android Studio
  • Delete the .gradle folder at your project’s root, and the app/build folder inside your app module
  • Reopen Android Studio and sync the project (File > Sync Project with Gradle Files)

5. Rule out resource compression or plugin issues

If you’re using resource shrinking (shrinkResources true in your build.gradle), ensure colorRed isn’t being marked as unused. Add this to res/raw/keep.xml to force retention:

<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@color/colorRed" />

Also, check if any third-party plugins (like theme or resource management plugins) are interfering with Android’s resource validation. Temporarily disable them to see if the error goes away.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:03:31