13

I have a simple Kotlin classes, as below

class MyClass {
    fun justSayHello(yes: Boolean): String {
        if (yes) {
            return "Hello"
        } else {
            return "Sorry"
        }
    }
}

I have my test (written in Java here, could be in Kotlin, also)

public class MyClassTest {
    private MyClass myClass = new MyClass();

    @Test
    public void testFirst() {
        myClass.justSayHello(true);
    }

    @Test
    public void testSecond() {
        myClass.justSayHello(false);
    }
}

When I run a test with Coverage in Android Studio 3.0 Beta-2 using classpath 'com.android.tools.build:gradle:3.0.0-beta2', no coverage is reported for it.

But when I run the test using classpath 'com.android.tools.build:gradle:2.3.3', 100% coverage reported.

When I change my source code to Java:

public class MyClass {
    public String justSayHello(boolean yes) {
        if (yes) {
            return "Hello";
        } else {
            return "Sorry";
        }
    }
}

It works fine for both gradle build tools

It seems to me that 'com.android.tools.build:gradle:3.0.0-beta2' has the broken test coverage measurement for Kotlin.

Did I miss anything? Is there a workaround for me to get the test coverage in Kotlin?

Elye
  • 53,639
  • 54
  • 212
  • 474
  • 3
    I reported the issue in https://issuetracker.google.com/issues/64929213 – Elye Aug 22 '17 at 14:25
  • Does this answer your question? [JaCoCo returning 0% Coverage with Kotlin and Android 3.0](https://stackoverflow.com/questions/45464138/jacoco-returning-0-coverage-with-kotlin-and-android-3-0) – Mahozad Nov 06 '21 at 14:15
  • Another similar question: [Android Studio 3/Kotlin code coverage](https://stackoverflow.com/q/45350561/8583692) – Mahozad Nov 06 '21 at 14:18

1 Answers1

6

In case anyone is still looking for a solution, adding in a gradle task to copy the cases from the tmp directory into the directory that the coverage output looks in helps with this issue as a workaround.

For example add copyTestClasses to your module gradle file

task copyTestClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debug"
    into "build/intermediates/classes/debug"
}

And then setting up your defaults to run the gradle task before running the tests Example of setting up gradle task

It can help to find both of the directories in your project manually before trying to point to them using gradle, to make sure that you're pointing to the right place (flavours will change the directories that you need to point to)

Marcus Hooper
  • 647
  • 7
  • 12
  • This is as per reported in https://stackoverflow.com/a/45354933/3286489. But if observe carefully, it is reporting the coverage of the test code, not the app code. :( – Elye Dec 12 '17 at 02:50
  • The reason that one is reporting the coverage of the test classes is because it's copying from /debugUnitTest and not /debug – Marcus Hooper Dec 12 '17 at 02:52
  • there is no debug folder if there is no kotlin classes – WenChao Jan 16 '18 at 05:39
  • Still the same with Android Studio 3.0.1, I filed a ticked: https://issuetracker.google.com/u/1/issues/73730680 – Chriss Feb 22 '18 at 10:18
  • Also, if you use the module just as plain kotlin lib, use this instead: task copyTestClasses(type: Copy) { from "build/classes/kotlin" into "build/classes/java" } – Deividas Strioga May 25 '18 at 11:23