Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

  • Run my production app (filled with Kotlin files)
  • Unit test in Java

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

like image 452
Oblitzitate Avatar asked Dec 06 '17 22:12

Oblitzitate


2 Answers

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.

like image 61
bio007 Avatar answered Oct 23 '22 06:10

bio007


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.

like image 22
Harry Han Avatar answered Oct 23 '22 05:10

Harry Han