Espresso测试启动失败报NoClassDefFoundError: PlatformTestStorageRegistry如何解决?
调试方法
- 统一依赖版本:所有androidx.test相关依赖(core、runner、rules、espresso系列)必须使用同一大版本,不能混合不同主版本号(比如1.5.x和1.6.x混用)。执行
./gradlew androidDependencies命令生成依赖树,找出版本冲突的依赖,通过exclude排除旧版本,或者用gradle的force属性强制统一版本。 - 补全Test Storage核心依赖:
PlatformTestStorageRegistry属于androidx.test.platform:platform-core包,确保这个依赖被正确引入,且版本和其他test依赖匹配。如果transitive依赖被意外排除,直接显式添加:androidTestImplementation "androidx.test.platform:platform-core:对应版本号" - 匹配AGP与Gradle版本:AndroidX Test版本和Android Gradle Plugin(AGP)、Gradle版本强相关:
- AGP 7.0-7.3 对应 Gradle 7.0-7.4,建议用AndroidX Test 1.5.x系列
- AGP 7.4+ 对应 Gradle 7.4+,建议用AndroidX Test 1.6.x系列
- AGP 8.0+ 对应 Gradle 8.0+,建议用AndroidX Test 1.6.1及以上版本
版本不匹配会导致内部API兼容问题,出现类找不到的错误。
- 排查混淆规则:如果开启了R8混淆,需确保androidx.test相关类未被混淆,在proguard-rules.pro中添加:
同时检查自定义混淆规则是否误删了必要类。-keep class androidx.test.** { *; } - 最小化依赖排查:暂时只保留核心测试依赖(core、runner、espresso-core),移除其他非必要的test依赖,确认测试能正常运行后,再逐个添加依赖,定位冲突来源。
兼容的AndroidX Test依赖集合
AGP 7.0-7.3(Gradle 7.0-7.4)
androidTestImplementation "androidx.test:core:1.5.0" androidTestImplementation "androidx.test:runner:1.5.2" androidTestImplementation "androidx.test:rules:1.5.0" androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1" androidTestImplementation "androidx.test.platform:platform-core:1.5.0" // 可选扩展依赖 androidTestImplementation "androidx.test.espresso:espresso-contrib:3.5.1" androidTestImplementation "androidx.test.espresso:espresso-intents:3.5.1"
AGP 7.4+(Gradle 7.4+)
androidTestImplementation "androidx.test:core:1.6.0" androidTestImplementation "androidx.test:runner:1.6.0" androidTestImplementation "androidx.test:rules:1.6.0" androidTestImplementation "androidx.test.espresso:espresso-core:3.6.0" androidTestImplementation "androidx.test.platform:platform-core:1.6.0" // 可选扩展依赖 androidTestImplementation "androidx.test.espresso:espresso-contrib:3.6.0" androidTestImplementation "androidx.test.espresso:espresso-intents:3.6.0"
AGP 8.0+(Gradle 8.0+)
androidTestImplementation "androidx.test:core:1.6.1" androidTestImplementation "androidx.test:runner:1.6.1" androidTestImplementation "androidx.test:rules:1.6.1" androidTestImplementation "androidx.test.espresso:espresso-core:3.6.1" androidTestImplementation "androidx.test.platform:platform-core:1.6.1" // 可选扩展依赖 androidTestImplementation "androidx.test.espresso:espresso-contrib:3.6.1" androidTestImplementation "androidx.test.espresso:espresso-intents:3.6.1"
内容的提问来源于stack exchange,提问作者Ben
相关产品推荐
相关产品推荐

