How to add Kotlin to an existing Java Android project – Guide

Android Studio supports Kotlin, making it easy to add Kotlin files to your project and convert Java language code to Kotlin. You can use all of Android Studio’s existing tools with your Kotlin code, including autocompletion, lint checking, refactoring, debugging, and more.

Add Kotlin to an Existing Project

  1. Add the following line to your project’s build.gradle file: dependencies { compile ‘com.android.tools.build:gradle:3.0.0’ }
  2. Add the Kotlin file to your project’s root directory: src/main/kotlin/app/src

In the New Android Fragment dialog, select one of several Android templates. If you don’t see the list of templates in this menu, first open the Project window and select your application module.

When you create a new activity in Android Studio, the New Android Activity dialog appears. Figure 2 shows the dialog, and Kotlin is the source language. ..

Please continue with the wizard. ..

If you don’t see this option, open the Project window and select the java directory. The New File / Kotlin Class window allows you to define the file name and offers several options for the file type: File, Class, Interface, Enum Class or Object. The choice you make determines the basic structure created for you in the new Kotlin file. If you choose Class, Android Studio will create a new Kotlin source file with the given name and a corresponding class definition. If you choose Interface, an interface will be declared in the file and so on.

If you have not used Kotlin before, Android Studio will warn you that it is not configured in the project. Configure Kotlin by clicking Configure in the upper right corner of the editor or the event log alert that appears up in the lower right corner.

When prompted, choose the option to configure Kotlin for all modules that contain Kotlin files.

After clicking OK, Android Studio adds Kotlin to your project and applies the Kotlin Android plugin to each module that contains Kotlin files.

home organization

If you want to keep your Kotlin and Java files separate, you can place the Kotlin files in src / main / kotlin / and include this directory in the sourceSets configuration. ..

Convert existing Java code to Kotlin code

To convert Java code to Kotlin, open the Java file in Android Studio and select File > Convert Java File to Kotlin File. Alternatively, create a new Kotlin file (File > New > File / Kotlin class) and paste the Java code into that file. Android Studio displays a prompt and offers to convert your code to Kotlin. Click Yes to convert. You can optionally check Don’t show this dialog next time, which makes future conversions automatic.

Code conversion and nullability

Android Studio produces functionally equivalent Kotlin code that is compiled and executed. However, it is likely that you will need to make additional optimizations to the converted code. For example, you might want to refine how your converted code handles nullable types.

On Android, it’s common to delay the initialization of View objects and other components until the fragment or activity they’re attached to reaches the appropriate lifecycle state. For example, you might have a reference to a button in one of its fragments but want it initialized as soon as possible so that you can use it in an activity.

When using the button variable in this example, it should never be null. However, because the value is not assigned at the build point, the generated Kotlin code handles Button as a nullable type and uses the non-null assertion operator to unwrap the button when adding a click listener. ..

This conversion is less ideal than using lateinit for this case because you are forced to unwrap the button reference with a non-null assertion or safe call operator at each place that is accessed.

In some cases, where null is a valid variable assignment based on your application’s use case, using a secure call operator (?.) with an elvis termination operator (? 🙂 might be a more appropriate way to unwrap safely nullable object or coerce to a non-null sensitive default value. However, Android Studio does not have enough information to make this determination during the conversion process. Although the default is the non-null statement, you must follow up and adjust the converted code as needed. ..

Final note

This guide will show you how to add Kotlin to an existing Java Android project. If you have any questions about this article, please feel free to ask us in the comments below. Additionally, please share this article with your friends if you enjoyed it! ..