How to get code coverage using Android Studio?

前端 未结 9 1950
囚心锁ツ
囚心锁ツ 2020-12-02 07:00

I am developing apps using Android Studio.
I was able to run the test code.
But, I do not know how to get code coverage in android studio.

I have a

9条回答
  •  半阙折子戏
    2020-12-02 07:56

    There are so much answers showing how to apply jacoco plugin to Android studio project, which is outdated, and wasted me so much time to figure out the solution for recently Android studio(My Android Studio is version 2.1.2).

    • Jacoco plugin is built in for Android Studio gradle, what you need to do is just enable it like following:
      buildTypes {
        ...
        debug {
          testCoverageEnabled true
        }
      }
    
    • After you do above, run unit test task ./gradlew testDebugUnitTest

    • Then create coverage files: ./gradlew createDebugCoverageReport

    • Coverage files will be created under /build/reports/coverage/debug folder,include index.html, which you can open it with browser, and report.xml which you can use to get a report by jenkins jacoco plugin or other continues integration tools.

    For those who got 0% coverage with jenkins jacoco plugin, be sure to use the right version. quote from their site:

    Unfortunately JaCoCo 0.7.5 breaks compatibility to previous binary formats of the jacoco.exec files. The JaCoCo plugin up to version 1.0.19 is based on JaCoCo 0.7.4, thus you cannot use this version with projects which already use JaCoCo 0.7.5 or newer. JaCoCo plugin starting with version 2.0.0 uses JaCoCo 0.7.5 and thus requires also this version to be used in your projects. Please stick to JaCoCo plugin 1.0.19 or lower if you still use JaCoCo 0.7.4 or lower

提交回复
热议问题