androidandroid-testingandroid-instrumentation

Test run failed to complete. Instrumentation run failed due to Process crashed. with debugger inside a module


I'm starting to implement instrumentation testing for my company to increase our code quality and speed of deployment. I'm rewriting a module of our application and I'm trying to implement TDD principles. My code does not work at the moment and I therefore wanted to attach a debugger to my instrumentation tests to verify its execution. Except that at each Debug compilation I end up with the error Test run failed to complete. Instrumentation run failed due to Process crashed.

By dichotomy I created a simple project containing just one module and I launched the instrumental tests given as an example.

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@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)
    }
}

Same result. Then I launched the same instrumental test in the app module and here surprise, the debugger was attached and a breakpoint triggered.

It is not possible to attach a debugger to an instrumental test in a module? The test works and is validated when I run it

You will find here the build.gradle file of my library

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'
}

Nothing fancy, this is the file created when the project was created. Same for the build.gradle of app module

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 ommitted to reduce noice **
}

I'm on Android Studio Flamingo and Gradle 8.0


Solution

  • UPDATE:

    Just found a workaround for this issue:

    Go to the run configuration and change the debug type to “Java Only”

    enter image description here

    You can find more information about this issue on the following links

    https://issuetracker.google.com/issues/268213434 https://issuetracker.google.com/issues/264388220

    OLD ANSWER:

    I'm also facing this particular error, but it only started when I upgraded my Android Studio to Flamingo.

    By downgrading the version back to Android Studio Electric Eel solved the issue for me.

    You can find previous Android Studio versions here