visit
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!")
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.
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.
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
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.
Press the key combination **Command+Shift+N \
Choose Kotlin.
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
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:
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
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.