Kapt generatestubs - cannot initialize an object using the internal constructor from unit-test

Say we have a kotlin class defined as:

package foo

class Bar internal constructor() {
//...
}

When I try to initialize this object from a test method, for example:

package foo


class TestBar {
    @Test
    fun testingBar() {
        Bar()  //<----- error
    }
}

I get the following error:

Cannot access '<init>': it is internal in 'Bar'

Both bars and TestBar are in the same AndroidStudio module (android library). Both source paths were defined in gradle:

...
sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
    test.java.srcDirs += 'src/test/kotlin'
}
...

The tests seem to work fine at all, but I can't create any object with an internal constructor.

I use:

ext.kotlin_version = '1.1.3-2'

EDIT

Thanks for the feedback. I decided to make a simple android multi-module application from scratch, and it did a great job.

Then I tried to get rid of the dependencies of my module one by one, and it turns out that the problem is caused by the dagger2 gradle dependency.

dependencies {
    ...

    //dagger 2
    implementation 'com.google.dagger:dagger:2.8'
    kapt 'com.google.dagger:dagger-compiler:2.8'
}

, . , ?


2 ()

, kapt generatestubs = true. , . , kapt:

http://kotlinlang.org/docs/reference/kapt.html

kapt {
    generateStubs = true
}

apply plugin: kotlin-kapt'

gradle.

+4
1

internal , . Kotlin::

, Kotlin, :

  • IntelliJ IDEA;
  • Maven;
  • a Gradle ;
  • , Ant.

, main test 2 .

main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
+2

Source: https://habr.com/ru/post/1681096/


All Articles