React Native安卓编译报错:找不到android:attr/fontVariationSettings等资源
fontVariationSettings/ttcIndex) Without Upgrading CompileSdk or AndroidX Hey Leo, let's break down why this happened and fix it without touching your CompileSdk version or moving to AndroidX.
Why This Error Popped Up
First, the android:attr/fontVariationSettings and android:attr/ttcIndex attributes are introduced in Android 9.0 (API level 28)—but your project uses CompileSdkVersion 27, which doesn't recognize these attributes.
The reason it worked yesterday but broke today? Chances are Android Studio triggered a dependency refresh or pulled an updated version of a library (like appcompat-v7) that now references these API 28 attributes. Even though you didn't modify code, the gradle cache or dependency resolution might have changed when you opened the project in Android Studio.
Fixes to Try (No CompileSdk/AndroidX Upgrade Needed)
1. Force All Dependencies to Use Your Target appcompat-v7 Version
Some third-party libraries might be pulling in a newer version of appcompat-v7 that includes API 28 references. Add this to your project-level build.gradle to lock all dependencies to your 27.0.2 version:
configurations.all { resolutionStrategy { force 'com.android.support:appcompat-v7:27.0.2' } }
2. Define Missing Attributes Manually
Since your CompileSdk doesn't know these attributes, you can define dummy values for them in your app's resources to satisfy AAPT. Add these lines to your app/src/main/res/values/values.xml file:
<attr name="fontVariationSettings" format="string" /> <attr name="ttcIndex" format="integer" />
Alternatively, you can add them via gradle in your app-level build.gradle (inside the defaultConfig block):
defaultConfig { // ... your existing config (minSdk, targetSdk, etc.) resValue "attr", "fontVariationSettings", "string" resValue "attr", "ttcIndex", "integer" }
3. Clean Gradle Cache and Rebuild
Corrupted cache is often the culprit here. Run these commands to clear old cache and rebuild:
# Clean project ./gradlew clean # Delete global gradle cache (optional but recommended) rm -rf ~/.gradle/caches/ # Rebuild the Android project ./gradlew assembleDebug
4. Check for Accidental Android Studio Changes
Double-check if Android Studio auto-upgraded your build tools or gradle plugin. Make sure your app-level build.gradle still has:
buildToolsVersion "27.0.3" classpath 'com.android.tools.build:gradle:3.1.0'
Verify the Fix
After trying these steps, run react-native run-android again. The AAPT linking errors should be gone since we've either locked the dependencies to compatible versions or provided the missing attribute definitions.
内容的提问来源于stack exchange,提问作者Leo

