Android Studio中Theme.AppCompat无法找到,添加依赖仍未解决求助
我需要使用Theme.AppCompat.Light.NoActionBar主题,但在Android Studio中无法找到Theme.AppCompat。已经在Gradle中添加了implementation 'com.android.support:appcompat-v7:28.0.0'依赖,但问题仍未解决。
我的Gradle依赖内容如下:
dependencies { implementation 'androidx.core:core-ktx:1.10.1' // implementation 'androidx.room:room-ktx:2.5.1' kapt "androidx.room:room-compiler:2.5.1" implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1' implementation 'androidx.preference:preference-ktx:1.2.0' implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0")) implementation 'com.android.support:design:28.0.0' implementation 'com.google.android.material:material:1.9.0' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.google.android.material:material:1.10.0-alpha03' // implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1' implementation 'androidx.activity:activity-compose:1.7.1' implementation platform('androidx.compose:compose-bom:2022.10.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' }
1. 核心问题:混合AndroidX与旧Support库
你的项目同时引入了AndroidX库(如androidx.core:core-ktx、androidx.room)和旧的Support库(com.android.support:appcompat-v7:28.0.0、com.android.support:design:28.0.0),二者无法共存,会引发依赖冲突,导致Android Studio无法正确识别Theme.AppCompat。
2. 修复步骤
(1)替换旧Support库为AndroidX版本
移除所有com.android.support开头的依赖,改用对应的AndroidX库:
- 把
com.android.support:appcompat-v7:28.0.0替换为androidx.appcompat:appcompat:1.6.1(或最新稳定版) - 把
com.android.support:design:28.0.0替换为com.google.android.material:material:1.9.0(项目已引入该库,保留即可)
(2)清理重复依赖
依赖中重复引入了3次com.google.android.material:material,只保留一个最新稳定版本(比如1.9.0,删除1.10.0-alpha03和重复的1.9.0)。
(3)修改主题引用
使用AndroidX库时,推荐继承Material Components主题,替换后的主题示例:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <!-- 自定义主题属性 --> </style>
若坚持使用AppCompat主题,AndroidX版本的Theme.AppCompat.Light.NoActionBar也可正常使用,只要依赖配置正确。
(4)整理后的依赖示例
dependencies { implementation 'androidx.core:core-ktx:1.10.1' implementation 'androidx.room:room-ktx:2.5.1' kapt "androidx.room:room-compiler:2.5.1" implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1' implementation 'androidx.preference:preference-ktx:1.2.0' implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0")) implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1' implementation 'androidx.activity:activity-compose:1.7.1' implementation platform('androidx.compose:compose-bom:2022.10.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' implementation 'androidx.compose.material3:material3' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' }
(5)同步并清理项目
修改依赖后,点击Android Studio右上角的Sync Project with Gradle Files按钮同步,再执行Build > Clean Project和Build > Rebuild Project,确保依赖正常加载。
内容的提问来源于stack exchange,提问作者Alena Vakh

