Let's walk through the following steps to create an Android project with Kotlin support:
- First, we need to open Android Studio on our development machine. If you don't already have Android Studio installed, you can find the download and installation instructions at https://developer.android.com/studio/install.
- Select Start a new Android Studio project.
- Select Empty Activity, and then click Next, shown as follows:
- Update your project Name, Package name, and Save location, as shown in the following screenshot:
- Ensure that Kotlin is selected in the Language drop-down menu, and then click Finish. This will ensure that Kotlin is the default language for the project and that the IDE generates new code using Kotlin rather than Java:
At this point, you will have a buildable Android project with Kotlin support, something similar to what is shown in the following screenshot:
To see how Kotlin is configured for the project, open the root-level build.gradle file. In this case, it's the build.gradle file labeled Project: HelloKotlin. See the following screenshot:
This is the project-level build.gradle file.
There are two key things to be aware of in this build.gradle file:
- The ext.kotlin_version variable defines the version of Kotlin used in the project, in this case, 1.3.31.
- The Kotlin Gradle plugin has been added as a classpath dependency, allowing us to apply the plugin to our other Gradle modules, thereby making them Kotlin-aware.
Finally, if you open the build.gradle file labeled as Module: app, you'll notice three things:
- The kotlin-android plugin has been applied.
- The kotlin-android-extensions plugin has been applied.
- The Kotlin standard library has been added as a dependency.
The following screenshot shows these elements:
At this point, you're now ready to start writing your Android app with Kotlin. In the next section, we'll explore how Kotlin can make that process easier.