José González Gómez
05/03/2021, 11:35 AMsrc/commonTest/resources
but they don't seem to get copied to the output directory when running Android tests. Putting them under src/androidTest/resources
did the trick for Android tests, but I don't want to have two copies of these files, as they are common for both Android and iOS tests.Derek Ellis
05/03/2021, 11:47 AMcommonTest/resources
directory, but in a lot of cases that's good enough.José González Gómez
05/03/2021, 12:00 PMNicolas Verinaud
05/03/2021, 12:19 PMJosé González Gómez
05/03/2021, 12:43 PMMichal Klimczak
05/03/2021, 2:31 PMandroidTest/resources
. In case anyone has the same: https://kotlinlang.slack.com/archives/C3PQML5NU/p1620201399394700?thread_ts=1620195839.390000&cid=C3PQML5NUJosé González Gómez
05/05/2021, 10:28 AMcommonTest/resources
, and then add the following snippet in the shared project's `build.gradle.kts`:
afterEvaluate {
listOf("debug", "release").forEach { variant ->
val copyTaskName = "copyResources${variant.capitalize()}UnitTest"
tasks.register<Copy>(copyTaskName) {
from("$projectDir/src/commonTest/resources")
into("$buildDir/tmp/kotlin-classes/${variant}UnitTest/")
}
tasks.getByName("test${variant.capitalize()}UnitTest") {
dependsOn(copyTaskName)
}
}
tasks.register<Copy>("copyResourcesIosX64DebugTest") {
from("$projectDir/src/commonTest/resources")
into("$buildDir/bin/iosX64/debugTest/")
}
tasks.getByName("iosX64Test") {
dependsOn("copyResourcesIosX64DebugTest")
}
}
and then I have a `expect`/`actual` class to load resources:
expect class TestResource(path: String) {
val content: String?
}
actual class TestResource actual constructor(path: String) {
actual val content: String? = NSString.stringWithContentsOfFile(NSBundle.mainBundle.pathForResource(path, null)!!) as String?
}
actual class TestResource actual constructor(path: String) {
actual val content: String? = this::class.java.classLoader?.getResource(path)?.readText()
}
Be careful! The into
directories seem to change depending on the Android Studio / Gradle version./gradlew testDebugUnitTest
after a ./gradlew clean
the build fails because the files aren't copied to the destination. The second time the files are copied and the build is successful. I thought that maybe the copy task doesn't create the target directory, so I tried to manually create it before executing the tests, but this didn't work// Top of build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
// Inside the task definition
tasks.register<Copy>(copyTaskName) {
...
mustRunAfter(tasks.withType<KotlinCompile>())
}
goncalossilva
12/13/2021, 2:53 PM