Kotlin M5.2: IntelliJ IDEA 12.1 and Gradle

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!

About Andrey Breslav

Andrey is the lead language designer working on Project Kotlin at JetBrains. He also works on making the Java language better, serving as a Java Community Process expert in a group for JSR-335 ("Project Lambda"), and occasionally speak at software conferences such as Devoxx or JavaOne.
This entry was posted in General and tagged . Bookmark the permalink.

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

  1. Mario Arias says:

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

    interface Foo{
    int bar(String baz);
    }

  2. Mario Arias says:

    Cool, now half of my project class are useless… :D

  3. he guangyu says:

    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:

    Any plans to introduce auto import for unambiguous imports?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>