How can I create a Kotlin project that builds with

2019-04-23 05:59发布

问题:

I'm trying to create a new Kotlin project that builds with Gradle using IntelliJ IDEA (2016.2.5 on Ubuntu 16.04). When I do this I immediately get an error message.

Here's what I'm trying:

  1. Select "Create New Project" from Welcome Screen.

  2. Select "Gradle" from left hand pane, "Kotlin (Java)" from right. Click "Next".

  3. Enter "hello-world" as ArtifactId. Click Next.

  4. Ensure "Create separate module from source set" and "Use default Gradle wrapper" are selected, nothing else is. Click "Next".

  5. Use defaults for project name and location. Click "Finish".

I then immediately get this error:

Gradle 'hello-world' project refresh failed

Error: Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.1-M02-12.
       Searched in the following locations:
           https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.pom
           https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.1-M02-12/kotlin-gradle-plugin-1.1-M02-12.jar
       Required by:
           :hello-world:unspecified

The generated build.gradle looks like this:

version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1-M02-12'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

How can I create a Kotlin project that builds with Gradle correctly?

回答1:

In your build.gradle, change ext.kotlin_version to be:

 ext.kotlin_version = '1.1-M02'

It's a minor bug that the IDE plugin puts its own version into build scripts, not the Kotlin version.

And also add the 1.1 EAP repository to the repositories in both buildscript and the root scope:

repositories {
    // ...
    maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" }
}

Kotlin artifacts related to EAP versions are not put into Maven Central like those of public releases, and this repository is not added automatically into the generated build script.

Then refresh the Gradle project, and the build should pass.

Feel free to check with this build.gradle file.