Add Kotlin code

Android Studio 3.0 and higher provides full support for Kotlin, so it's easy to add Kotlin files to your existing project and convert Java language code to Kotlin. You can then use all of Android Studio's existing tools with your Kotlin code, such as autocomplete, lint checker, refactoring, debugging, and more.

If you're starting a new project and want to use Kotlin, see Create a project. Also check out our Kotlin code samples.

Add Kotlin to an existing project

  1. Click File > New and select one of the various Android templates. If you don't see the list of templates in this menu, first open the Project window and select your app module.

  2. In the wizard that appears, select Kotlin for the Source language.

  3. Continue through the wizard, and you're done.

Alternatively, you can click File > New > Kotlin File/Class to create a basic file. If you don't see this option, open the Project window and select the java directory. The New Kotlin File/Class window provides several choices for the file type, but it doesn't matter which one you choose because Kotlin switches the file type automatically if you later change the declaration type.

By default, new Kotlin files are saved into src/main/java/. You might find it's easier to see both Kotlin and Java files in one location. But if you'd prefer to separate your Kotlin files from your Java files, you can put Kotlin files under src/main/kotlin/ instead. If you do, then you need to include this directory in your sourceSets configuration:

android {
   sourceSets { += 'src/main/kotlin'

Convert existing Java code to Kotlin code

Open a Java file and select Code > Convert Java File to Kotlin File.

Or, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file—when prompted, click Yes to convert the code to Kotlin. You can check Don't show this dialog next time, which makes it easy to dump Java code snippets into your Kotlin files.

For more information about using both Kotlin and Java code in your project, read about Kotlin's interoperability with the Java language.