How to Convert Java Code to Kotlin Code in Android Studio?
Last Updated :
31 Jan, 2021
In Google I/O 2017, Kotlin has been declared as an official language for Android app development. This language gains popularity among developers very quickly because of its similarities as well as interoperable with Java language. One can mix code of Java and Kotlin while designing an Android project. Some of the major challenges faced by developers like avoiding null pointer exceptions are easily handled by Kotlin. Because of all these reasons, it became essential to learn Kotlin. However, Android Studio takes care of this need. Developers can easily convert their Java code into Kotlin in Android Studio. There can be 2 scenarios in front of developers:
- Converting a complete Java File/Class into Kotlin File/Class
- Add a new Kotlin file/class in the project and convert a piece of Java code.
This article broadly describes the steps involved in performing code conversion in both cases.

Code Conversion
Method 1: Converting a Complete Class/File into Kotlin
Step 1: Open source code fileĀ
Open the java source code file that is to be converted. Consider the given code of the MainActivity file that is to be converted into Kotlin.
Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// declaring variable for TextView component
private TextView textView;
// declaring variable to store
// the number of button click
private int count;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// assigning ID of textView2 to the variable
textView = findViewById(R.id.textView2);
// initializing the value of count with 0
count = 0;
}
// function to perform operations
// when button is clicked
public void buttonOnClick( View view){
// increasing count by one on
// each tap on the button
count++;
// changing the value of the
// textView with the current
// value of count variable
textView.setText(Integer.toString(count));
}
}
Step 2: Select the option
From the left-hand side Option menu, select Android Project and right-click on the source code file. Select the option "Convert Java File to Kotlin File". One can also use the shortcut command "Ctrl+Alt+Shift+K" while the file is opened in Android Studio.Ā
A dialog box will appear that asks permission to configure Kotlin in Project. To carry out the code conversion it is necessary to grant this permission. Further, select the "All modules" option and choose the latest Kotlin compiler installed on the computer. After clicking "OK", Android Studio will make some changes in the app module build.gradle file to perform code conversion successfully.

Step 3: Produce Kotlin file
Again select the "Convert Java File to Kotlin File" option by right click on the java class/file. This time the required conversion will happen and the file extension will get changed from .java to .kt. Below is the equivalent Kotlin code produced on converting the above mentioned MainActivity.java file.
Kotlin
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// declaring variable for TextView component
private var textView: TextView? = null
// declaring variable to store
// the number of button click
private var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// assigning ID of textView2 to the variable
textView = findViewById(R.id.textView2)
// initializing the value of count with 0
count = 0
}
// function to perform operations
// when button is clicked
fun buttonOnClick(view: View?) {
// increasing count by one on
// each tap on the button
count++
// changing the value of the
// textView with the current
// value of count variable
textView!!.text = Integer.toString(count)
}
}
Method 2: Adding a Separate Kotlin File into the Project
Android Studio allows the mixing of Java and Kotlin code in a project. If there is a requirement to reuse a piece of Java code by writing it in Kotlin, one can follow the below-mentioned steps.
Step 1: Create a Kotlin Class/File
Create a new Class/File of Kotlin extension inside the java folder of the application's project file. Give it a desired name. Once the file is created, Android Studio will show an alert message that "Kotlin is not configured".
Step 2: Configure Kotlin into the Project
Click on the "configure" option present on the alert message. A dialog box will appear, select "All modules" and choose the latest Kotlin compiler installed on the computer. Click "OK" and let the changes take place in build.gradle files.
Step 3: Copy and Paste
Once Android Studio finished the build of project files, open the created Kotlin Class/File. Copy the Java code which is to be converted. Paste that code in the created file/class having Kotlin extension. That Java code will get converted automatically into Kotlin by the Android Studio.Ā
Advantages of Kotlin Over Java
- Kotlin codes are more concise than Java.
- It is interoperable(compatible) with Java.
- Kotlin resolves nullability issues by placing "Null" in its type system.
- Cleaner and safer code.
Similar Reads
How to Convert Kotlin Code to Java Code in Android Studio?
Java programming language is the oldest and most preferred language for Android app development. However, during Google I/O 2017, Kotlin has been declared as an official language for Android development by the Google Android Team. Kotlin has gained popularity among developers very quickly because of
3 min read
How to Convert a Kotlin Source File to a Java Source File in Android?
We use Android Studio to translate our Java code into Kotlin while transitioning from Java to Kotlin. But what if we need to convert a Kotlin file to its Java equivalent? We'll examine how to convert a Kotlin source file to a Java source file in this blog. Let's get this party started. Because of it
2 min read
How to Build a Roman Numeral Convertor in Android Studio?
Roman Numeral converter is an app through which we can convert a decimal number to its corresponding roman number or a roman number to its corresponding decimal number in the range of 1 to 3999. The user will enter a decimal number and on clicking the convert to roman numeral button, the entered num
5 min read
How to Convert Text to Speech in Android using Kotlin?
Text to Speech App converts the text written on the screen to speech like you have written âHello Worldâ on the screen and when you press the button it will speak âHello Worldâ. Text-to-speech is commonly used as an accessibility feature to help people who have trouble reading on-screen text, but it
3 min read
How to Convert Any Website to Android App in Android Studio?
Here, we are going to make an application for the "Wikipedia" website. By making this application we will be able to learn how we can convert a website to an Android Application just by following simple steps. You can use this concept for your personal website too and learn something new.Step by Ste
2 min read
How to Convert Pixel to DP in Android using Jetpack Compose?
We have seen the usage of many units while building android applications. These units are given different views such as image view, and text view to specify their height and width within the android application. Many times we also want to convert one unit to another unit. In this article, we will ta
5 min read
How to Clone Android Project from GitHub in Android Studio?
Android Studio provides a platform where one can build apps for Android phones, tablets, Android Wear, Android TV, and Android Auto. Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. One can develop Android Applications using Kotlin or Java as the Backen
3 min read
How to Add Footer to ListView in Android with Kotlin?
In Android, a ListView is a view used to display a list of items separated by a stroke. A ListView adapter is used to supply items from the main code to the ListView in real-time. A Footer is anything that gets added at the bottom of any element. So, a Footer in ListView is a layout that is present
3 min read
How to Build a Palindrome Checker App in Android Studio?
In this article, we will be building a Palindrome Checker android app in Android Studio using Java/Kotlin and XML. The app will check whether the entered word is Palindrome or not, if the entered word is a Palindrome then a toast will be displayed having the message "Yes, it's a palindrome" otherwis
3 min read
How to Create Classes in Android Studio?
In Android during the project development most of the time there is a need for classes in the projects. For example, in the case of CRUD operation, we need a model class to insert and retrieve data. Also to hold the info in our custom view we need to create a getter setter class. So basically in and
3 min read