Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Setting Up the Kotlin Android Environment

Setting Up the Kotlin Android Environment

Install the Kotlin plugin

Starting from version 3.0 (preview), Android Studio comes with the Kotlin plugin installed by default.

Open the Settings panel (Preferences on Mac), find the Plugins option on the right (shortcut Ctrl+, or command+ on Mac), type "Kotlin" in the search box, click Search in repositories, then install it; after installation, restart Android Studio.


Create a new project

Select Start a new Android Studio project or File | New project; most options have default values, so you just need to press "Enter" a few times.

Android Studio 3.0 offers an option to enable Kotlin support in the current dialog; checking it lets you skip the "Configuring Kotlin in the project" step.

Select the Android version:

Select the Activity template to create:

Name the Activity:

In Android Studio 3.0, you can choose to create the activity in Kotlin, so the step "Converting Java code to Kotlin" is not needed either.

In earlier versions, you create the activity in Java first and then convert it using the automatic conversion tool.


Convert Java code to Kotlin

Reopen Android Studio, create a new Android project, and add a default MainActivity

Open the MainActivity.java file, then choose Code | Convert Java File to Kotlin File from the menu bar:

Once the conversion is complete, you can see the activity written in Kotlin.

Configure Kotlin in the project

When you start editing this file, Android Studio will prompt that Kotlin is not yet configured in the current project; just follow the prompt, or you can select Tools from the menu bar

When you choose to configure, the following dialog appears; just select the latest installed version.

After Kotlin is configured, the application's build.gradle file is updated. You can see the newly added apply plugin: 'kotlin-android' and its dependencies.

Sync the project: click "Sync Now" in the prompt box or use the Sync Project with Gradle Files command.