Oblitzitate
Oblitzitate

Reputation: 61

Cannot unit test in Kotlin (ExecutionException, Aapt2Exception)

A simple test...

import org.junit.Test

class KotlinUnitTest {

    @Test
    fun test() {
        assert(true)
    }
}

... results in the following...

Information:Gradle: Executing tasks: [:app:assembleDebug, :app:assembleDebugUnitTest]
Information:Kotlin: Kotlin JPS plugin is disabled
Information:Module "app" was fully rebuilt due to project configuration/dependencies changes
Information:06/12/2017 5:08 PM - Compilation completed with 3 errors and 0 warnings in 19s 962ms
Error:Gradle: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Gradle: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Gradle: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

Adding android.enableAapt2=false to gradle.properties (or also gradle-wrapper.properties) made Android Studio halt indefinitely every time I ran the test.

This is weird because I'm able to...

But, for whatever reason, I can't test in Kotlin

Upvotes: 6

Views: 1581

Answers (2)

bio007
bio007

Reputation: 982

I've had similar problems with meaningless AAPT2 errors while running kotlin tests. The source of the problem is in Android Studio generating wrong Run Configuration for tests in Kotlin. I'm not sure what causes this bug because it looks it's not happening to everyone.

Running unit tests via command line worked without problems.

Creating test configuration by hand resolved the issue: create configuration manually

But still if I try to run the test by clicking the side note button I got the compilation error. run test button

BTW: If you try to edit the generated configuration you are out of luck because that configuration isn't listed in the configurations list at all.

Upvotes: 4

Harry Han
Harry Han

Reputation: 281

I got same problem. for a simple test:

@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)
class MainActivityTest {
    private lateinit var activityController: ActivityController<MainActivity>

    @Before
    @Throws(Exception::class)
    fun setUp() {
        activityController = Robolectric.buildActivity(MainActivity::class.java)

        activityController.create().start().resume()

    }

    @Test
    @Throws(Exception::class)
    fun testNotBeNull() {
        assertNotNull(activityController.get())
    }

}

Update to latest Kotlin version to 1.1.60 and Gradle to 3.0.1 solve it.

Upvotes: 2

Related Questions