I feel like this should have been very easy, but I’ve been banging my head against the wall about this for some time. All that I want is to create a Kotlin project in IntelliJ IDEA using Gradle as my build system. I will admit that I’m not savvy with Gradle, but understand it’s importance. I am trying to do this in order to get better at coroutines in Kotlin.
My first question is, if I want a Kotlin + Gradle project, do I start by:
- Create a Kotlin project
- Create a Gradle project
With the former, I don’t see any build.gradle
files in my project directory. With the latter, I do see a build.gradle
, although in a format that I’m not as familiar with (I am an Android developer). Here is what my build.gradle
looks like so far:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3-M2'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
With this set up, I have the following very basic coroutine implementation:
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
fun main(args: Array<String>) {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello,") // main thread continues while coroutine is delayed
Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}
However, I am seeing some compilation errors that are very vague that I can’t seem to pin down:
Would anyone be able to assist me with this? I’ve only started using Kotlin and really don’t want to be discouraged by such simple issues. Any sort of help on this would be greatly appreciated