I'm writing a simple console app which should to generate some kotlin code on execution. I faced a strange problem I can't add KotlinPoen dependency. My build.gradle:
plugins {
id 'java-library'
id 'kotlin'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation group: 'com.squareup', name: 'kotlinpoet', version: '1.7.2'
}
But in PoetApp.kt import failed with message Unresolved reference: squareup
:
import com.squareup.kotlinpoet.FunSpec
object PoetApp {
@JvmStatic
fun main(vararg param: String) {
val main = FunSpec.builder("main")
.addCode("""
|var total = 0
|for (i in 0 until 10) {
| total += i
|}
|""".trimMargin())
.build()
}
}
You have declared Java compatibility with version 1.7, but this version of kotlinpoet will work only with 1.8 and higher, see the library gradle file and also some compatibility issue solved here.
So your build.gradle should look like this:
plugins {
id 'java-library'
id 'kotlin'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation group: 'com.squareup', name: 'kotlinpoet', version: '1.7.2'
}