Resolving "Can't determine type for tag '?attr/colorSurface'" in Android
Problem: Android Build Error with Material Components
The Android build error Can't determine type for tag '?attr/colorSurface'
typically occurs when using Material Components for Android version 1.7.0 or newer on an outdated build environment. This error indicates the build system can't process new XML macros introduced in Material Components v1.7.0.
Common triggers:
- Using Material Components library ≥1.7.0 with Android Gradle Plugin (AGP) <7.2.0
- Outdated Gradle versions (<7.3.3)
compileSdkVersion
lower than 31- Older Android Studio versions (pre-2021.2.1)
Recommended Solutions
Fix 1: Update Build Dependencies (Recommended)
The official solution is updating your tools to meet Material Components' requirements:
buildscript {
dependencies {
// Update Android Gradle Plugin to 7.2.2+
classpath 'com.android.tools.build:gradle:7.2.2'
}
}
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
android {
// Set SDK versions to 33 or newer
compileSdk 33
defaultConfig {
targetSdk 33
}
}
dependencies {
// Keep Material Components updated
implementation 'com.google.android.material:material:1.9.0'
}
WARNING
Minimum requirements for Material Components ≥1.7.0:
- AGP ≥7.2.0
- Gradle ≥7.3.3
- Android Studio Chipmunk 2021.2.1+
- Java 8
Fix 2: Downgrade Material Components (Temporary Fix)
If immediate tool updates aren't feasible, use Material Components v1.6.x or earlier:
dependencies {
implementation 'com.google.android.material:material:1.6.0'
}
Solution Rationale
Material Components 1.7.0+ introduced XML macros requiring updated build tools:
- AGP ≥7.2.0 supports macro processing
- Gradle ≥7.3.3 enables required dependency features
- Android Studio Chipmunk/Dolphin+ provides Gradle compatibility
Additional Configuration Changes
For projects requiring Manifest declarations (common with newer SDKs):
<activity
android:name=".MainActivity"
android:exported="true"> <!-- Add this attribute -->
...
</activity>
Verifying The Fix
- Update Android Studio via
Help > Check for Updates
- Verify tool versions in
File > Project Structure
- Clean project with
Build > Clean Project
- Rebuild via
Build > Rebuild Project
Avoid Using Incompatible Versions
Material Components 1.7.0+ won't work with these combinations:
- AGP ≤7.1.0
- Gradle ≤7.3.2
- Android Studio Arctic Fox (2020.3.1) or older
Best Practices
- Keep all dependencies updated:
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}
- Test with newest stable tooling before updating Material Components
- Upgrade Material Components only after verifying compatibility
TIP
Always sync your Gradle files after dependency changes using the "Elephant" icon in Android Studio or via File > Sync Project with Gradle Files
.
By meeting the build environment requirements and maintaining updated dependencies, you'll resolve the XML macro processing error and avoid similar issues in future updates.