Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

372
Visualizações
How to compile Kotlin code to jvmTarget="1.8" for tests

In our Android project we code in Kotlin and target java 1.6. However we are forced to java 1.8 in our test since some JUnit5 features requires it (static methods in interfaces).

Is it possibe to compile the tests differently than the production code?

We tried to raise the jvmTarget to 1.8 by adding this to our build.gradle:

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

This raises the jvmTarget of the production code too, but we only want it for our tests. The Docs indicate that it can be specified for the test like this:

compileReleaseUnitTestKotlin{
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Unfortunatly the build.gradle doesn't compile.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The Kotlin docs mention how to do it for tests only.

Groovy DSL:

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

Kotlin DSL:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val compileTestKotlin: KotlinCompile by tasks

compileTestKotlin.kotlinOptions.jvmTarget = "1.8"
over 4 years ago · Santiago Trujillo Relatório

0

This snippet does the job for me:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    if (it.name.contains("UnitTest")) {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

I have to say, It's not the best solution to filter the test tasks based on their names, so think of this as a quick and dirty aid.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda