You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Jetpack Compose instrumentation测试Manifest合并错误如何解决

Jetpack Compose Navigation Codelab 测试Manifest合并错误解决方案

根因说明

就算主Manifest已经配置了android:exported属性,运行Android 12+(targetSdk≥31)的仪器化测试时仍触发该报错,本质是测试依赖的第三方库(尤其是androidx.test相关库)内置的组件未声明android:exported,合并到测试Manifest时触发校验失败。

解决步骤

  • 方案1:新增测试专用Manifest覆盖配置
    在src/androidTest/目录下创建AndroidManifest.xml文件,强制为冲突的测试组件配置android:exported属性:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
        <application>
            <!-- 适配androidx.test库内置组件的exported要求 -->
            <activity
                android:name="androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity"
                android:exported="false"
                tools:replace="android:exported" />
            <activity
                android:name="androidx.test.core.app.InstrumentationActivityInvoker$EmptyActivity"
                android:exported="false"
                tools:replace="android:exported" />
            <activity
                android:name="androidx.test.core.app.InstrumentationActivityInvoker$EmptyFloatingActivity"
                android:exported="false"
                tools:replace="android:exported" />
        </application>
    </manifest>
    
  • 方案2:升级测试依赖到适配Android 12的版本
    修改模块级build.gradle中的测试依赖版本,高版本测试库已经默认给内置组件添加了android:exported属性,无需额外配置:
    dependencies {
        androidTestImplementation 'androidx.test:core:1.5.0'
        androidTestImplementation 'androidx.test.ext:junit:1.1.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    }
    
  • 方案3:清理构建缓存
    配置完成后依次执行Android Studio的Clean Project、Invalidate Caches / Restart操作,再重新运行测试,排除旧缓存干扰。

冲突定位技巧

如果上述通用方案不生效,可以在项目根目录执行命令./gradlew processDebugAndroidTestManifest --stacktrace,输出完整的Manifest合并日志,精准定位到未声明android:exported的具体组件,再针对性在测试Manifest中补充配置即可。


内容的提问来源于stack exchange,提问作者Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 15:45:05