https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Alexander Larsson

06/16/2020, 8:27 AM
Hey, anyone know of a way to detect if you are running your tests on Android device or JVM?
a

aleksey.tomin

06/16/2020, 8:32 AM
As I know tests can’t run on android - only mac/lin/win You can use
org.jetbrains.kotlin.konan.target.HostManager.hostIsLinux
etc properties in
build.gradle.kts
.
a

Alexander Larsson

06/16/2020, 8:37 AM
I'm running them on an Android emulator with the
connectedAndroidTest
task from Gradle. I guess it runs on the emulator because it fails if you haven't started an emulator manually first. What do you mean with properties in
build.gradle.kts
. Can I define properties in the Gradle file that is available at runtime?
a

aleksey.tomin

06/16/2020, 8:39 AM
What the result of
HostManager.host_os()
in the build file?
a

Alexander Larsson

06/16/2020, 8:45 AM
It is always
osx
no matter which platform I build for. (JVM, Android, iOS)
Found a super simple solution:
Copy code
internal actual fun testSqlDriver(): SqlDriver {
    try {
        val context = InstrumentationRegistry.getInstrumentation().context
        return AndroidSqliteDriver(Database.Schema, context, "androidtestdb")
    }
    catch (e: IllegalStateException) {
        return JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY).apply {
            Database.Schema.create(this)
        }
    }
}
How did I not think of this before? 😅 Thanks for the help though! Did not know of host manager before. 🙂