paint-brush
6 Ways to Run Kotlin Code in Android Studioby@leonidivankin
2,727 reads
2,727 reads

6 Ways to Run Kotlin Code in Android Studio

by Leonid IvankinOctober 9th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, I would like to tell you about 6 ways to run Kotlin code in Android Studio. The examples below allow you to repeatedly run individual, even large pieces of code. The limitation for all these methods is that the code of the method being tested must be independent of the rest code.

People Mentioned

Mention Thumbnail
featured image - 6 Ways to Run Kotlin Code in Android Studio
Leonid Ivankin HackerNoon profile picture

In this article, I would like to tell you about 6 ways to run Kotlin code in Android Studio.

For example, we will output the string Hello World.


println("Hello World!")


What can it be useful for

This can be useful for quickly checking the results of the code.

For example, to make sure that the method is working correctly. Sometimes, it takes a long time to restart the entire project in order to get a single number or string. The examples below allow you to repeatedly run individual, even large pieces of code.


The limitation for all these methods is that the code of the method being tested must be independent of the rest of the code of the project. Although the use of dependencies on libraries, for example, Gson, is allowed in some cases. However, it is worth remembering that the more dependencies are used, the longer the code will run. Therefore, the following methods are more suitable for testing an independent method.


I am sure that some will be known to you, but some you will find new.

1. Create an empty activity

In the usual way, we create a new activity. Insert the line println("Hello World!"):


class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       println("Hello World!")
   }
}


Do not forget to add it to AndroidManifest.xml so as not to crash. Run and get the result:


To find it faster in a huge list of logs, you can enter Hello World! into the filter.


2. Use Kotlin REPL

Go to the path Tools > Kotlin > Kotlin REPL


The Kotlin REPL window will open at the bottom of the screen. Insert the line println("Hello World!"). Start by clicking on the green arrow or by using the Command+Enter combination.


In the same window, just below, you will see the result.

You can read more about Kotlin REPL in this article.


3. Create a separate file

Create a Kotlin file in any part of the project, for example, Main.kt. Insert the following lines:


fun main() {
   println("Hello World!")
}


Android Studio has recognized that this is a Kotlin file and can run it separately from the entire project.


Click on the green arrow:


Below, you can see the results:


4. Use scratch + kotlin script

Press the key combination **Command+Shift+N \

Choose Kotlin.


The editor window will open, divided into two parts. On the left, you can enter the source code. On the right — see the result.


Insert the line println("Hello World!"). If Interactive mode is checked, then you will immediately see the result on the right:


You can read about how else to use scratch + Kotlin script in this article.

5. Use Breakpoints

Let's go back to the Main.kt file, created by us earlier (item 3).

Let's put a Break point opposite the line println("Hello World!"):



We will launch it in the debug mode by clicking on the green arrow or by using the combination Control+Shift+D:

Insert the line "Hello World!". Note that it is not println("Hello World!"), because the returned result of the method will be displayed. As you know, println("Hello World!") returns Unit, so we won't see anything. Press Enter and get the result:


6. Use the IDE scripting console

Press Command+Shift+A and type IDE Scripting Console. The following window will be displayed:


Choose Kotlin:



A new file will be created, and an editor window will open. Insert the line println("Hello World!") into it:


Next, right-click and press Run or the key combination Control+Shift+R:



The Kotlin script will be executed, and we will see the result in the window that appears at the bottom of Android Studio:


Also, the file with this script can be found in the path Scratches and Consoles > IDE Consoles > ide-scripting.kts


You can read more about the IDE scripting console in this article.


Conclusion

These simple examples show that Android Studio is a fairly flexible tool that allows you to solve the same task in several ways. This makes it possible to choose the optimal approach or combine several to solve your problems.


If you find an error in the article or you have an extension, you can always share it in the comments below.