引入Google Material Design后项目同步出现appComponentFactory资源错误
Hey, let's tackle that No resource identifier found for attribute 'appComponentFactory' in package 'android' error you're facing. This is a super common issue when mixing old Android Support libraries with the newer AndroidX-based Material Design components. Let's break down why it's happening and how to fix it.
为什么会出现这个错误?
The root cause boils down to two key conflicts:
- SDK Version Mismatch: The
com.google.android.material:material:1.0.0-alpha1library you're using requires at least Android SDK 28 to function properly, but your project is set to compile against SDK 26. - Support vs AndroidX Conflict: You're still relying on old Support libraries (like
com.android.support:appcompat-v7:26.0.1) alongside the AndroidX-based Material library. These two ecosystems aren't compatible, andappComponentFactoryis an AndroidX-specific attribute that your old SDK/Support setup doesn't recognize.
解决方案一:迁移到AndroidX(推荐)
AndroidX is Google's modern replacement for the old Support libraries, and it's where all future Material Design improvements will live. Here's how to make the switch smoothly:
Upgrade your SDK versions in your module-level
build.gradle:android { compileSdkVersion 28 // Minimum required for AndroidX Material; use 33+ for latest stability buildToolsVersion "28.0.3" // Match the compileSdkVersion defaultConfig { minSdkVersion 17 targetSdkVersion 28 // Sync this with compileSdkVersion // Keep your other default config settings } }Replace all Support library dependencies with AndroidX equivalents in your
dependenciesblock:
Swap out old Support libraries for their AndroidX counterparts. For example:dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) // Replace old Support libs with AndroidX implementation 'androidx.appcompat:appcompat:1.0.0' // Replaces com.android.support:appcompat-v7 implementation 'com.google.android.material:material:1.0.0' // Use stable version, not alpha implementation 'androidx.cardview:cardview:1.0.0' // Replaces com.android.support:cardview-v7 // Keep your other non-Support dependencies (Volley, Retrofit, Picasso, etc.) as-is implementation 'org.apache.httpcomponents:httpclient:4.5' implementation 'com.mcxiaoke.volley:library-aar:1.0.0' implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4' implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta3' implementation 'com.wang.avi:library:2.1.3' implementation 'com.mikhaellopez:circularimageview:3.0.2' implementation 'com.droidninja:filepicker:1.0.0' implementation 'com.squareup.picasso:picasso:2.5.2' implementation 'com.github.ganfra:material-spinner:1.1.1' implementation 'com.google.android.gms:play-services-appindexing:8.1.0' }Enable AndroidX migration in your project-level
gradle.propertiesfile:
Add these lines to automatically convert third-party libraries to use AndroidX:android.useAndroidX=true android.enableJetifier=trueUpdate your app theme (optional but recommended):
To fully leverage Material Design components, switch your theme instyles.xmlto inherit from a MaterialComponents theme:<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Your existing theme attributes --> </style>
解决方案二:继续使用旧Support库(不推荐)
If you can't migrate to AndroidX right now, ditch the AndroidX Material library and stick with the old Support Design library (which you already have in your dependencies):
- Remove this line from your
dependencies:compile 'com.google.android.material:material:1.0.0-alpha1' - Keep the existing Support Design library:
compile 'com.android.support:design:26.0.1'
Note: This will limit you to older Material components without access to new AndroidX features.
额外注意事项
- Never mix AndroidX and Support library dependencies—this is the #1 cause of this type of error.
- Avoid using alpha/beta versions of libraries unless you're testing new features; stable versions are far more reliable.
- Double-check your
AndroidManifest.xmlfor any AndroidX-specific attributes or classes that might have snuck in during migration.
内容的提问来源于stack exchange,提问作者Chamindu

