Kotlin M5.2: IntelliJ IDEA 12.1 and Gradle

Posted on by Andrey Breslav

Another update of Kotlin comes out today. Welcome Kotlin M5.2. 

Support for New IntelliJ IDEA

Koltin M5.2 supports (in fact, requires) the recently released IntelliJ IDEA 12.1.

The Kotlin IDE improvements include:

  • Kotlin classes in the Class Hierarchy view. Just hit Ctrl+H on a class name to see its descendants and/or parents.
  • Folding for imports (you don’t have to scroll through imports to get to your code any more).
  • New UI for Kotlin library configuration: you can now control library names and location, and kotlin-runtime.jar is not copied to your project by default. It just works.
  • Support for JavaScript-targeted modules is significantly improved.
  • Optimizing imports on the fly: IDE Settings -> Editor -> Auto Import -> Optimize imports on the fly.
  • New quick fixes: place the cursor on an error, and hit Alt+Enter.

SAM Constructors

When using Java libraries, you can now create an instance of a SAM interface (one with a Single Abstract Method) by calling its name and passing a function literal. For example:

SwingUtilities.invokeLater(Runnable { doItNow() })

This works only for Java classes. In fact, it is not a part of the language, but a feature of how Java classes are loaded into Kotlin: we define a synthesized function

fun Runnable(body: () -> Unit) = object : Runnable {
    override fun run() {
        body()
    }
}

So whenever you import Runnable, the function is there too, and you can use it.

This is the first chunk of SAM-conversion support for Java. Real SAM conversions will come soon, and you’ll be able to say simply

SwingUtilities.invokeLater { doItNow(); }

New Gradle Plugin

Starting with M5.2, in addition to the long-available Maven plugin, there’s a Gradle plugin for Kotlin from JetBrains.

Here’s an example for building a Kotlin module with Gradle:

buildscript {
  repositories {
    mavenCentral()
    maven {
      url 'http://repository.jetbrains.com/all'
    }
  }
  dependencies {
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.1-SNAPSHOT'
  }
}

apply plugin: "kotlin"

repositories {
  mavenCentral()
  maven {
    url 'http://repository.jetbrains.com/all'
  }
}

dependencies {
  compile 'org.jetbrains.kotlin:kotlin-stdlib:0.1-SNAPSHOT'
}

More examples & docs can be found here.

Reminder: KAnnotator

Don’t forget that you now have KAnnotator at your service.

Have a nice Kotlin!

Comments below can no longer be edited.

8 Responses to Kotlin M5.2: IntelliJ IDEA 12.1 and Gradle

  1. Mario Arias says:

    April 4, 2013

    How the SAM will work with methods that take parameters?, for example:

    interface Foo{
    int bar(String baz);
    }

  2. Mario Arias says:

    April 4, 2013

    Cool, now half of my project class are useless… 😀

  3. he guangyu says:

    May 24, 2013

    Is kotlin plugin compatible with Android Studio?
    I have found scala plugin in Android Studio, but I don’t find kotlin plugin there.

  4. Stephen Samuel says:

    January 28, 2014

    Any plans to introduce auto import for unambiguous imports?

    • Andrey Breslav says:

      January 28, 2014

      Yes, sure it’s on the roadmap

  5. Roadmap de #kotlin | Kotlin.es says:

    August 22, 2015

    […] oficial en inglés […]