Make Kotlin file Runnable/Executable on Android Studio
It’s a quick guide on how to run a Kotlin file i.e .kt extension file when using Android Studio as an IDE.
In brief: If you have already a project and just want to run the Kotlin file then you must have fun main(args: Array<String>){} function.
Steps to run the Kotlin file(.kt):-
- Create a new Android Project.
2. Choose “Add No Activity” option from the template and Finish.
3. Right-click on the directory and create a new Kotlin File.
4. Give a name to file which will have .kt extension.
5. Once you will define the main function, then you will see a run icon. Just tap it and you are done. :)
It must have a “main” function in the Kotlin file to make it runnable. If you have main() function then this project can be a pure Kotlin project without using any android libraries.
fun main(args: Array<String>){
println(printHello())
}
fun printHello(): String {
return "Hello Kotlin World!"
}
6. Edit Config will have this class as Main class. You can change if have multiple runnable Kotlin files.
Have happy coding and a clap. :-)