Skip to content

Most visited

Recently visited

navigation

Add Kotlin Code

Android Studio 3.0 and higher provides full support for Kotlin, so it's easy to add Kotlin files to you existing project and convert Java language code to Kotlin. You can then use of all 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 {
       main.java.srcDirs += '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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)