3

I am trying to execute only unit test and genrate jacoco test report, but I see always a failure message saying

Execution failed for task ':App:connectedDebugAndroidTest'.
> com.android.builder.testing.api.DeviceException: No connected devices!

Gradle code:

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.1"
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testTpsDebugUnitTest', 'create<build-vairant>DebugCoverageReport']) { 
    group = "reporting"
    description = "Generate unified Jacoco code coverage report"

    reports {
        xml.enabled false
        csv.enabled false
        html.destination  file("${buildDir}/reports/jacocoHtml")
    }

    def fileFilter = [
            '**/*Test*',
            '**/*_MembersInjector.class',
            '**/*_Factory.class']
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/<build-variant>/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: "$buildDir", includes: [
            "jacoco/test<build-variant>DebugUnitTest.exec"
    ])

I execute with command:

 ./gradlew -Pcoverage clean jacocoTestReport

Please help to fix this issue, so it only executes unit test and doesnt ask for a device!

Sweety Bertilla
  • 972
  • 10
  • 35
  • You'll need to have an Android Emulator running or a physical device connected and in usb debugging mode. See https://facebook.github.io/react-native/docs/getting-started.html#preparing-the-android-device for more information. – AKS May 18 '18 at 21:59
  • Possible duplicate of [com.android.builder.testing.api.DeviceException: No connected devices](https://stackoverflow.com/questions/48039119/com-android-builder-testing-api-deviceexception-no-connected-devices) – AKS May 18 '18 at 21:59
  • Also see this https://stackoverflow.com/questions/46398159/com-android-builder-testing-api-deviceexception-no-connected-devices-error-on?rq=1 – AKS May 18 '18 at 22:03
  • 4
    Thanks for the reponse, it worked when I removed "'createDebugCoverageReport'" – Sweety Bertilla May 21 '18 at 15:31
  • 1
    If the project only has Unit Tests, then a device should not be required. To prove this you can easily run the Unit Tests with not device at all (JUnit 5). There's something wrong with Jacoco--it always seems to run AndroidTests, not Unit Tests. Any solutions? – SMBiggs Sep 22 '22 at 12:52

1 Answers1

0

I saw this issue while debugging my Jacoco installation, and fortunately for me it was the beginning of the solution.

If you are running Unit Tests (including Jacoco Unit tests), a device should not be needed. AndroidTests require a device, not Unit Tests.

After installing all the Jacoco changes to the gradle and re-synced:

  1. Open the gradle window of Android Studio (View -> Tool Windows -> Gradle) you will see an icon of an elephant (as of Android Studio Chipmunk)enter image description here.
  2. Click on this icon and a command window will appear. enter image description here
  3. Type in the box as shown: gradle app:createDebugUnitTestCoverageReport and hit .

This will start the Jacoco process running. Since it generally relies on Unit Tests (assuming your gradle is configured correctly), it will force the Unit Tests to run first. You'll find your report in the location you specified in the gradle (default is something like ./app/build/reports/coverage/test/debug/index.html).

My problem was I was entering the wrong unit test in step 3. I typed gradle app:createDebug... and accepted the first suggestion. This was wrong! It put createDebugAndroidTestUnitCoverageReport, which was not what I wanted (and caused the error requesting a device).

SMBiggs
  • 11,034
  • 6
  • 68
  • 83