简体   繁体   English

适用于应用程序和单元测试的单独的Android Gradle Jvm版本

[英]Seperate Android Gradle Jvm version for app and unit tests

Is it possible to set jvmTarget = "1.8" only for unit tests in gradle for an Android app? 是否可以仅在Android应用程序的gradle中为单元测试设置jvmTarget = "1.8" I was using: 我正在使用:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

But then I got crashes on older 4.4 devices... 但是后来我在较旧的4.4设备上崩溃了...

Yes, you can do that simply by setting jvmTarget for a certain Kotlin compilation task. 是的,您只需为某个Kotlin编译任务设置jvmTarget就可以做到这一点。

In an Android project, the unit tests are normally compiled by the tasks compileDebugUnitTestKotlin , compileReleaseUnitTestKotlin etc. You may find the complete list of Gradle tasks in the IDE's Gradle view or by running: 在一个Android项目中,单元测试通常由任务compileDebugUnitTestKotlincompileReleaseUnitTestKotlin等编译。您可以在IDE的Gradle视图中或通过运行以下命令找到Gradle任务的完整列表:

./gradlew tasks --all

Search for the names following the pattern compile*Kotlin . 在模式compile*Kotlin之后搜索名称。

Then just configure the single task that you need, for example: 然后只需配置所需的单个任务,例如:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile)
    .matching { it.name == "compileDebugUnitTestKotlin" }
    .all { 
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM