memory-leaksandroid-instrumentationleakcanaryandroidjunitrunner

AndroidJUnitRunner cannot find extra class com.squareup.leakcanary.FailTestOnLeakRunListener


I am trying to run leak canary along with my Android Instrumentation tests. I followed the LeakCanary documentation https://square.github.io/leakcanary/upgrading-to-leakcanary-2.0/#running-leakcanary-in-instrumentation-tests

However, the instrumentation test app fails to start with the following error:

java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{<app component>/androidx.test.runner.AndroidJUnitRunner}: java.lang.IllegalArgumentException: Could not find extra class com.squareup.leakcanary.FailTestOnLeakRunListener
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6709)
        at android.app.ActivityThread.access$1300(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I have the following in my build.gradle:

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.12'
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.annotation:annotation:1.0.0'
    implementation 'androidx.core:core:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.github.bumptech.glide:glide:4.6.1'
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:4.2'
    testImplementation 'org.robolectric:shadows-httpclient:4.2'
    testImplementation 'androidx.test:core:1.1.0'
    testImplementation 'org.mockito:mockito-core:2.25.0'
    androidTestImplementation 'org.mockito:mockito-core:2.25.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.annotation:annotation:1.0.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'com.linkedin.dexmaker:dexmaker:2.25.0'
    androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.25.0'
    testImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
    androidTestImplementation 'com.squareup.leakcanary:leakcanary-android-instrumentation:2.9.1'
    androidTestUtil 'androidx.test:orchestrator:1.1.1'
    api "com.google.auto.value:auto-value-annotations:1.7"
    api 'com.google.guava:guava:27.0.1-jre'
    annotationProcessor "com.google.auto.value:auto-value:1.7"
    annotationProcessor 'org.projectlombok:lombok:1.18.12'
}

android {
    compileSdkVersion 31
    ndkVersion "23.0.7599858"
    buildToolsVersion "30.0.2"
    defaultConfig {
        minSdkVersion 22
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments clearPackageData: 'true'
        testInstrumentationRunnerArgument "listener", "com.squareup.leakcanary.FailTestOnLeakRunListener"
        renderscriptTargetApi 22
    }
}

Any help or pointers is greatly appreciated.


Solution

  • This was a documentation problem which was fixed.

    https://github.com/square/leakcanary/issues/2409