升级compileSdk至33及appcompat至1.6.0后设计模式属性丢失问题
问题描述
将androidx.appcompat:appcompat从1.5.1版本升级至1.6.0后,Android Studio弹出报错提示;随后把compileSdk与targetSdk从32版本升级至33版本,发现布局设计模式下控件的大部分属性不再显示。尝试将Gradle版本改为7.4.2、Android Gradle Plugin版本改为7.2.2,问题仍未解决。
以下是app模块的build.gradle代码:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'com.google.gms.google-services' } android { compileSdk 33 defaultConfig { applicationId "com.gramzin.kilo" minSdk 21 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { viewBinding = true } } dependencies { implementation platform('com.google.firebase:firebase-bom:31.2.0') implementation 'androidx.appcompat:appcompat:1.6.0' implementation 'androidx.core:core-ktx:1.7.0' implementation 'com.google.android.material:material:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.google.firebase:firebase-database-ktx:20.1.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'com.google.firebase:firebase-auth-ktx' implementation 'androidx.activity:activity-ktx:1.5.1' }
解决方案尝试
1. 同步依赖版本
appcompat 1.6.0与activity-ktx当前1.5.1版本不匹配,可能引发联动问题,建议将androidx.activity:activity-ktx升级至1.6.0:
implementation 'androidx.activity:activity-ktx:1.6.0'
修改后同步项目并重新构建。
2. 清理Android Studio缓存
这是解决布局预览异常的常用手段:
- 菜单栏选择
File -> Invalidate Caches... - 勾选
Clear file system cache and local history,点击Invalidate and Restart - 重启后重新打开布局文件查看预览状态
3. 检查预览主题设置
在布局设计模式右上角,确认预览使用的主题是App自定义的AppCompat/Material主题(而非No Theme或不兼容主题),主题不匹配会导致属性加载异常。
4. 排查依赖冲突
执行命令查看完整依赖树,确认无旧版本AppCompat或Support库残留:
./gradlew app:dependencies
若发现冲突,可通过exclude强制排除冗余依赖,例如:
implementation('androidx.appcompat:appcompat:1.6.0') { exclude group: 'com.android.support' }
5. 切换预览API版本
在设计模式右上角的API选择器中,切换至与targetSdk一致的API 33版本,或尝试其他API版本,验证属性是否恢复显示。
内容的提问来源于stack exchange,提问作者ramzes1_1
相关产品推荐
相关产品推荐

