Icon

The documentation for Kotlin is now located on the new site. This page is kept for historical reasons

Skip to end of metadata
Go to start of metadata
Icon

The documentation for Kotlin is now located on the new site. This page is kept for historical reasons

Ant

Defining <kotlinc> task using local Kotlin setup

Where kotlin.home is the path to Kotlin distribution (usually, kotlinc directory).

<kotlinc> attributes

Name
Description
Required
Default Value
src

Kotlin source file or directory to compile

"src" or "module" needs to be specified

 

module

Kotlin module to compile

"src" or "module" needs to be specified

 

output

Destination directory

If "src" is used - "output" or "jar" needs to be specified

 

jar

Destination jar file

If "src" is used - "output" or "jar" needs to be specified
If "module" is used - only "jar" can be specified or it can be omitted

"moduleName.jar"
classpath

Compilation class path

false

 

classpathref

Compilation class path reference

false

 

stdlib

Path to "kotlin-runtime.jar"

false
""
includeRuntime

If "jar" is used - whether Kotlin runtime library is included

false
true
inline

Enable inlining

false
true

<kotlinc> accepts a nested <classpath> element, similarly to <javac>.

Examples

"Smoke.kts":

"Smoke.kt":

Classpath examples

Maven

"kotlin-maven-plugin" compiles Kotlin sources and modules.
Note: only Maven 3 is supported for now.

Versions

Define Kotlin version in kotlin.version. Possible values are:

  • X.Y-SNAPSHOT - snapshot versions for X.Y release, updated with every successful build on our continuous integration server.
    Highly unstable, recommended for testing new compiler features.
    Currently, all builds are published as 0.1-SNAPSHOT. To use a snapshot version, configure a snapshot repository in your pom (see below).
  • X.Y.Z - release or milestone version X.Y.Z, updated manually, stable. Release versions are published to Maven Central Repository, no extra configuration is needed.

Here's the table of versions corresponding to milestone codes:

Milestone

Version

M5.3'

0.5.998

M6

0.6.69

M6.1

0.6.602

M6.2

0.6.1673

M7

0.7.270

M8

0.8.11

Using the Snapshot Repository

If you want to use 0.1-SNAPSHOT versions of Kotlin, you need to add the following repository definitions to your pom:

Dependencies

Kotlin has an extensive standard library, use it in your program:

Compile Kotlin sources

Specify source directories in <build> tag:

Reference kotlin-maven-plugin to compile sources:

Compile mixed Kotlin/Java sources

To compile mixed code applications Kotlin compiler should be invoked before Java compiler.
In maven terms that means kotlin-maven-plugin should be run before maven-compiler-plugin.

It could be done by moving Kotlin compilation to previous phase, process-sources (feel free to suggest a better solution if you have one):

External annotations

Kotlin uses external annotation to have precise information about types in Java libraries. To specify these annotations, use annotationPaths in <configuration>:

Other options

To disable inlining, specify

for kotlin-maven-plugin.

Example

Download a simple hello world application here.

Gradle

"kotlin-gradle-plugin" (available since Kotlin M5.2) compiles Kotlin sources and modules.

Repositories and versions are similar to kotlin-maven-plugin.

Project layout

Put your Kotlin sources under kotlin directory, next to java directory that holds Java sources.

Dependencies

You need to add dependencies on kotlin-gradle-plugin and Kotlin standard library:

See versions above.
More examples (including mixed Java/Kotlin project) here (download here).

Snapshot Versions

If you want to use a snapshot version (nightly build), first add out snapshot repository and change the version to 0.1-SNAPSHOT:

Android

Android's Gradle model is a little different from ordinary Gradle, so if you want to build an Android project written in Kotlin, you need kotlin-android plugin instead of kotlin:

Also, if you are in Android Studio, add the following under android:

This lets Android Studio know that your kotlin directory is a source root, so when the project model is loaded into the IDE it will be properly recognized.

See an example here (or download it here).

Compatibility with Gradle versions

Icon

If you use com.android.tools.build:gradle:0.9.+ (for example, in Android Studio 0.5.+), then Kotlin 0.7.115 or higher is required

External Annotations

External annotations for JDK and Android SDK will be configured automatically. If you want to add more annotations for some of your libraries, add the following line to your Gradle script:

Other options

To disable inlining, specify

in your script.

Griffon

See "griffon-kotlin-plugin".

  • No labels

26 Comments

  1. Can a single module have both Java/Kotlin files in it?  We would like to start using Kotlin but have a large Java base we cannot transform all to Kotlin.  It would be ideal to just start writing new code with Kotlin allowing Java to reference the Kotlin and Kotlin to reference Java.  We build our application with Ant.  

    1. Yes, this is certainly possible.
      When building such a sourcebase with ANT, call a kotlinc first (it can read java source files too) and then javac, adding kotlinc output as additional classpath entry.

  2. What about building JavaScript, particularly with Ant?  I'm most interested in Kotlin as a shared code base between JVM code (Java/Scala) and JavaScript.

    1. Currently the capability of sharing the same code across two platforms is limited (because only small parts of the standard library are available on JS so far), but this will be improved.

    2. Ant task for JavaScript is not implemented yet. Watch/vote for http://youtrack.jetbrains.com/issue/KT-3436

  3. About the new (as of M5.2) gradle plugin: it seems the tests are not executed at all.

    I downloaded the provided kotlin-examples-master.zip and ran {{./gradlew clean test}} but there is no test report generated.
    Adding a fail() does not result in a build failure.

    Am I missing something?

    1. Indeed, we have an issue, sorry for that.

      The workaround is to add a single line:

      The issue itself is described here: http://youtrack.jetbrains.com/issue/KT-3490 please watch/vote for it

  4. How do I specify a directory containing KAnnotator-generated Annotations using the gradle plugin?
    Thx

    1. You can find an example in hello-world project

      1. In Gradle, this works great with the kotlin plugin but doesn't with the kotlin-android plugin as it fails with the error 

        > Could not find method compileKotlin() for arguments ...

        Are external annotations supported with the kotlin-android plugin, for android libraries/applications ? With what syntax ?  

        1. OIivier,

          You should set kotlinOptions inside android section. E.g.:

          1. Many thanks ! I love it.

            One more question : Is there a way to annotate the classes in an android library idea (gradle backed) module/aar ? (like the holoeverywhere android library for example)

  5. Synchronicity of Kotlins Maven artifacts?

    When are the kotlin-stdlib and the kotlin-maven-plugin updated in the repositories after an new version of the plugin is available? As of today Kotlin plugin is 0.6.312, std-lib in maven central is 0.6.179 and kotlin-maven-plugin is 0.5.748?

    However the pom in std-lib has a dependency to kotlin-maven-plugin with a matching version. How do I have to configure Idea and Maven to make this work?

    1. Everything is 0.6.179 in Central: http://search.maven.org/#search%7Cga%7C1%7Ckotlin, including kotlin-maven-plugin

      The IDE plugin is newer, indeed, but do you experience problems with it?

      1. Actually I have more problems with it, than with earlier plugins. The snapshot build, only says 0.1-SNAPSHOT. Is it newer than the numbered builds?

        1. It is the latest successful nightly build. By default, Maven updates it once a day.

          What problems do you have with the IDE plugin?

          1. So at the moment I am on 0.6.317 for everything. Maven build are ok. The Plugin throws lots of internal errors and messages errors during typing, but does not clear them. I'll have a shot at the snapshot version. Thanks for your fast reaction and help.

            1. If you give us your code, we could investigate the problems you are having. If all the versions match there shouldn't be any exceptions/uncleared massages

              1. Ok, upgrading to 0.6.319 and/or recreating the project from scratch helped. If you want to test with my code feel free to grab it from: git clone git://git.assembla.com/funql-ri.gitProject info for the curious: www.funql.org

                1. Thanks. We'll look into it

    2. Also, the IDEA plugin you are using is a nightly build, so it would make sense to use SNAPSHOT versions of Kotlin as described above.

  6. Is the plugin for mixed Kotlin/Java versions still necessary? My maven can't find the plugin (version tag is red) and it seems to compile just fine without it.

    1. Does Maven not complain on the build output about the missing plugin?

      1. No*

        I noticed something was wrong when my maven and intellij could run and package my project, but the TeamCity agent couldn't. The original project works without the plugin as I described.I then cloned a new version of git and tried to compile it as-is. Now it throws a bunch of "Symbol not found" errors- all of them my kotlin classes. Presumably, this is the issue you mentioned, that kotlin must compile first.

        First, the plugin apparently runs if I substitute the version 0.1-SNAPSHOT with 0.6.1617. However, it crashes for whatever reason on my fourth Project with a MojoException. I haven't ruled out human error yet (I screwed something up), but the project .pom doesn't have any specifics not found in the other four successful projects.

        1. When something magically works without being configured, I usually suspect some kind of cache silently holding the necessary things. Feel free to ask a question in the forum in case you don't figure the versioning out.