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

Android库模块Instrumented测试调试时进程崩溃求助

库模块Instrumented测试调试时崩溃问题排查与解决

问题描述

我正在公司项目中实施Instrumented测试以提升代码质量和部署速度,遵循TDD原则重写应用模块。但在为库模块的Instrumented测试附加调试器时,Debug编译始终报错:

Test run failed to complete. Instrumentation run failed due to Process crashed

通过二分法创建仅包含一个库模块的简易项目,运行示例Instrumented测试仍出现相同错误;但在app模块中运行相同测试却能成功附加调试器并触发断点。库模块的测试可以正常运行,仅在调试时崩溃。当前使用Android Studio Flamingo和Gradle 8.0,相关代码与配置如下:

示例测试代码

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See testing documentation.
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.example.mylibrary.test", appContext.packageName)
    }
}

库模块build.gradle

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.mylibrary'
    compileSdk 33

    defaultConfig {
        minSdk 24
        targetSdk 33

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

app模块build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.myapplication'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        vectorDrawables {
            useSupportLibrary true
        }
    }

    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 {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.3.2'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    /** remaining dependencies omitted to reduce noise **/
}

解决方案

库模块的Instrumented测试完全支持附加调试器,你遇到的问题是特定配置或环境冲突导致的,可按以下步骤排查修复:

  • 关闭Debug构建的测试覆盖率开关
    库模块debug构建类型中启用的testCoverageEnabled true会引入JaCoCo插桩逻辑,该逻辑与调试器的字节码插桩存在冲突,直接导致进程崩溃。将其注释或设为false:

    debug {
        // testCoverageEnabled true
    }
    
  • 升级AndroidX测试依赖版本
    你当前使用的测试依赖版本与Gradle 8.0、Android Studio Flamingo的兼容性不足,升级到适配版本:

    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    
  • 检查设备/模拟器调试配置
    确保测试设备/模拟器已开启USB调试,关闭"不保留活动"等可能影响进程存活的开发者选项;同时重启设备/模拟器,清除测试应用的缓存数据。

  • 调整调试VM参数
    在Android Studio的运行/调试配置中,为库模块的Instrumented测试添加VM参数:

    -noverify
    

    该参数可绕过字节码验证,避免插桩代码引发的验证失败。

  • 统一SDK版本配置
    确保库模块与app模块的compileSdk、minSdk、targetSdk版本完全一致,消除版本差异带来的兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 08:45:17