Author profile picture

How to Enable Kotlin 1.3 Inline Classes Today

Are you ready to start exploring inline classes? Since they’re an experimental feature in Kotlin 1.3, you’ll need to take a few extra steps to enable them in your project. It’s not difficult to set up, but there are a few things to watch out for. This guide will help you navigate those issues so that you can start playing with Kotlin 1.3 and inline classes today.

For these instructions, I’m assuming you’re using IntelliJ IDEA or Android Studio.

1. Set the Kotlin Plugin Channel

The Kotlin plugins for your IDE are published in different update channels. By default, the update channel is going to be set to Stable, which is what you probably want most of the time. But to use inline classes and other 1.3 features, you’ll want to set it to use an early access version of 1.3.

To set this, open the Tools menu, and then go to Kotlin > Configure Kotlin Plugin Updates.

Configure Kotlin Plugin Updates menu item

Set the update channel to Early Access Preview 1.3, and click the Install button. Once it’s done installing, it’ll tell you that the plugin will be activated after you restart, so restart the IDE at that point.

Choosing the channel

2. Create a New Project

Create a new project, and of course select:

  • Kotlin (Java) if you’re using Gradle in IDEA
  • kotlin-archetype-jvm if you’re using Maven in IDEA
  • Include Kotlin support if you’re in Android Studio

Walk through the rest of the setup wizard.

3. Add the repository

The Kotlin build tool plugins and standard library for 1.3 aren’t published to the same repositories as the stable versions are, so you’ll need to update your build script so that it knows where to find them.

Gradle in IDEA

If you’re using a Gradle build in IDEA, and you created the project with version 1.3 of the Kotlin plugin installed, your settings.gradle and build.gradle files should already include the right repository. If that’s the case you can skip this step.

Gradle in Android Studio

For Android Studio, on the other hand, the repository doesn’t appear to be added automatically. You’ll know it because you’ll get a message like this:

Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.3-M1.

… or this …

Failed to resolve: org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3-M1

To fix this, add a repository that points to http://dl.bintray.com/kotlin/kotlin-eap. For example:

repositories {
    google()
    jcenter()
    maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
}

Be sure to add this to both the regular repository section (e.g., under allprojects, depending on your build) and the buildscript section. Here’s an example of a top-level build.gradle file for an Android project that includes the correct repository:

buildscript {
    ext.kotlin_version = '1.3-M1'
    repositories {
        google()
        jcenter()
        maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Maven

For Maven, be sure your kotlin.version is set to a 1.3 version, like 1.3-M1. (Assuming you created the project from a 1.2 archetype, you’ll have to make sure you set that version number yourself).

Add the Kotlin Early Access repository to your <repositories> and <pluginRepositories> blocks:

<repositories>
    <repository>
        <id>kotlin-eap</id>
        <url>http://dl.bintray.com/kotlin/kotlin-eap</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>kotlin-eap</id>
        <url>http://dl.bintray.com/kotlin/kotlin-eap</url>
    </pluginRepository>
</pluginRepositories>

4. Turn Warnings Off

With the changes we’ve made so far, the language level should be set to 1.3, so you should be able to start coding with inline classes, but you’d also get a warning in the IDE that says:

The feature “inline classes” is experimental

(If for some reason you’ve still got a language version of 1.2, you’ll get an error - see Language Version below to resolve this).

We can turn off that warning by adding a command-line flag for the compiler. This is the flag we want:

-XXLanguage:+InlineClasses

Gradle for JVM

If you’re using Gradle, it’s easy to set with freeCompilerArgs, like this for a regular JVM-based project:

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = ['-XXLanguage:+InlineClasses']
    }
}

Gradle in Android Studio

For Android, you’ll want this setting to apply to all build variants, so it would look a little somethin’ like this:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = '1.8'
        freeCompilerArgs = ['-XXLanguage:+InlineClasses']
    }
}

Maven

For Maven projects, you can set this in the <configuration> block of the kotlin-maven-plugin, like so:

<configuration>
    <args>
        <arg>-XXLanguage:+InlineClasses</arg>
    </args>
</configuration>

In case you’re not sure exactly where this goes, here’s an example <build> that includes the compiler argument with more context:

<build>
    <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
    <plugins>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>

            <executions>
                <execution>
                    <id>compile</id>
                    <goals> <goal>compile</goal> </goals>
                </execution>

                <execution>
                    <id>test-compile</id>
                    <goals> <goal>test-compile</goal> </goals>
                </execution>
            </executions>

            <configuration>
                <args>
                    <arg>-XXLanguage:+InlineClasses</arg>
                </args>
            </configuration>

        </plugin>
    </plugins>
</build>

Just in the IDE

You can also set this just in the IDE with the Additional command line parameters field of the Kotlin Compiler settings page, or on a per-module basis by opening the Project Structure menu, and expanding the module tree until you get to Kotlin.

Setting the command line parameter to allow inline classes.

Other Stuff that Might Happen

Language Version

If you’re converting a 1.2 project to use 1.3, you might end up with an error message like this:

The feature “inline classes” is only available since language version 1.3

In this case, you probably still have the Kotlin plugin set to a 1.2 version. You can update it to a 1.3 build (such as 1.3-M1). Or if you really want to keep it on 1.2, you could set the languageVersion, like this:

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
        languageVersion="1.3"
        freeCompilerArgs = ['-XXLanguage:+InlineClasses']
    }
}

For Maven, you can set this with a property:

<kotlin.compiler.languageVersion>1.3</kotlin.compiler.languageVersion>

Coroutines

If you’re using coroutines, you’ll want to make sure you’re using a version that’s compatible, or else you’ll get this message:

Library should be updated to be compatible with Kotlin 1.3

It’s easily fixed by updating the dependency:

compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0-eap13'

There’s even a handy-dandy intention action (i.e., quick fix) for both Gradle and Maven - just press Alt+Enter.

Professor cartoon

And You’re Done!

After following these steps, you should be able to start using inline classes - and explore other Kotlin 1.3 features! If you have any trouble with these instructions, or discover anything else that’s required for your installation, let me know!

Happy coding!

Share this article:

  • Share on Twitter
  • Share on Facebook
  • Share on Reddit