Creating an IntelliJ IDEA Project w/ Kotlin + Gradle


#1

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 :slight_smile:


#2

Try to change it to 1.3.0. You don’t need to use the early access build as 1.3 was released some weeks ago.

Also I don’t think coroutines 1.0.1 works with the eap versions of kotlin. I think this is the reason for your errors, but I’m not 100% sure.
As a last point, I don’t think you need the java plugin, but AFAIK this should not affect your build in any negative way.


#3

That did it! I was still getting errors after checking Kotlin to 1.3.0, but remove the eap repo did the trick. I would never have figured that out… Thank you!

Also, just to confirm, am I going down the right path by creating a Gradle project in IntelliJ IDEA if I want Gradle to manage my build process? I mostly use Gradle to import any libraries that I’m using (such as coroutines) as a dependency. Would I be able to achieve the same functionality if I started with just a Kotlin project and decided later on that I wanted to use Gradle for my builds?


#4

Yes, if you want gradle to manage builds and dependencies you should create a gradle and not a kotlin project. There might be some way in the settings to for a kotlin project to start using gradle but I don’t know how to do it.
In my experience the easiest way is just to always use gradle and just ignore IDE’s build system.