Kotlin has become very popular since Google announced that it supports the programming language at Google I / O 2017. The language used is fun, expressive and modern. Using Kotlin to develop your first Android application has many advantages.
Kotlin helps you avoid common errors in the programming language, such as NullPointerExceptions, because of built-in features that improve the quality of application development. Another advantage is that Kotlin is 100% interoperable with Java and requires less repetitive code writing, which saves time in testing and maintenance.
Kotlin runs on the Java virtual machine, widely used to develop Android applications. Here is a simple guide to building your first application with Kotlin.
1. Android Studio
To create applications using Kotlin, Android Studio must be installed on your computer. The platform works perfectly with Windows, Mac and Linux. The installation process is similar on all platforms.
2. Create Project
Once Android Studio is set up, you must click create a project. On the Welcome screen, click Start a new Android Studio project. In the New Project dialog box, enter the name of your first project. Accept the default sample domain. Check the box “include Kotlin support”.
Accept the default project location and proceed to the next step. In the Android target dialog, accept the defaults and click Next. Select the empty activity here and click Next. In the Customize Activity dialog box, you will find an option to accept the default value.
3. Create user interface
Let’s look at an example of creating a counter application with the plus button to increase the value and less to decrease it. You can easily create the user interface using existing components. The user interface of an application made with Kotlin will have the following three components:
• An additional button to increase the counter.
• One less button to decrease the counter.
• a TextView to display the counter value
4. Write code of the main activity
The Kotlin Android extension makes this job easier. You do not have to call findViewById () when writing the first application. You can easily reference the views in MainActivity with the plugin extensions. In the main activity, you must set a property to store the counter value. Once you have set OnClickListener on the plus and minus buttons to update the counter value, Android Studio generates the code.
5. Testing the app made with Kotlin
You can click the compile button to test the application. Android Studio will run the application in an emulator that will help you test it.