Setting Up the Kotlin Android Environment
Installing the Kotlin Plugin
Starting from version 3.0 (preview), Android Studio will have 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), enter "Kotlin" in the search box, click Search in repositories (search in the repository), and then install it. After the installation is complete, you will need to restart Android Studio.
Creating a New Project
Select Start a new Android Studio project or File | New project. Most options have default values, and you only need to press the "Enter" key a few times.
Android Studio 3.0 provides an option to enable Kotlin support in the current dialog box. After checking, you can skip the step of "Configuring Kotlin in the project."
Select the Android version:
Choose the style of Activity you want to create:
Name the Activity:
In Android Studio 3.0, you can choose to create an activity using Kotlin, so there is no need for the step of "Converting Java code to Kotlin."
In earlier versions, the activity would first be created using Java, and then it would be converted using an automatic conversion tool.
Converting Java Code to Kotlin
Reopen Android Studio and create a new Android project, adding a default MainActivity.
Open the MainActivity.java file, and through the menu, navigate to Code | Convert Java File to Kotlin File:
After the conversion is complete, you can see the activity written in Kotlin.
Configuring Kotlin in the Project
When you start editing this file, Android Studio will prompt that the current project has not yet been configured for Kotlin. Complete the operation according to the prompt, or you can choose Tools from the menu bar.
When choosing the configuration, there is the following dialog box, select the latest installed version.
After the Kotlin configuration is complete, the application's build.gradle file will be updated. You can see the addition of apply plugin: 'kotlin-android' and its dependencies.
Synchronize the project, click "Sync Now" in the prompt box or use the Sync Project with Gradle Files command.