KotlinNo Comments

kot
162 views.

Kotlin is fully supported in Android Studio 3.0 (get the canary version here), so it’s easy to create new projects with Kotlin files, add Kotlin files to your 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.

Create a new project with Kotlin


Using Kotlin with a new project requires just one extra click in the New Project wizard:

  1. In Android Studio, click File > New > New Project. Or if you’ve just opened Android Studio and see the Welcome to Android Studio window, click Start a new Android Studio project.
  2. On the first screen, check Include Kotlin support. That’s the only difference.
  3. Click Next and continue through the wizard until you’re done.

kotlin1

When you select an activity template, it will be provided in Kotlin code. Currently, only some the templates for the Phone and Tablet form factor are provided with idiomatic Kotlin code. The rest are automatically converted to Kotlin from the Java templates—you still get Kotlin code throughout, but some of the code might not use Kotlin’s best language features yet.

Just like Java files, the Kotlin files are saved in the directorysrc/main/java/

Add Kotlin to an existing project


If you want to add Kotlin code to an existing project, simply 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.

kotlin2kotlin3

Continue through the wizard, and you’re done.

Alternatively, you can click File > New > Kotlin File/Class to create a basic file. The New Kotlin File/Class window provides several choices for the kind of file, 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:

Convert existing Java code to Kotlin code
In Android Studio 3.0, 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.

kotlin-convert_2x

Use Android APIs with Kotlin
Kotlin provides complete interoperability with the Java language, so calling the Android APIs often looks exactly like the matching Java code. Except now you can combine those method calls with Kotlin’s syntax features.

Here are a few examples of what it looks like to call Android APIs in Kotlin, compared to the same code in Java language:

Declare Activity in Kotlin

Declare Activity in Java

On-click listener in Kotlin

On-click listener in Java

Item click listener in Kotlin

Item click listener in Java

 

Be the first to post a comment.

Add a comment