Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a single .kt file in Android Studio

I created a Scratch Kotlin file in Android Studio. I simply want to run this scratch.kt file and get output.

I fiddled with Run Cofigurations but cannot understand what will go into Main Class.

enter image description here

like image 230
Ananth Avatar asked Nov 29 '22 08:11

Ananth


2 Answers

You can create a scratch kotlin file with File -> New -> Scratch File -> Kotlin. With this you should be able to create and run kotlin script files to try features, code and so on.

If your intention is to create a full standlone kotlin project. My recomendation is to use IntelliJ IDEA. Doing so with Android Studio may be possible but you won't stop facing issues and having to search for workarounds all the time.

like image 31
Eric Martori Avatar answered Dec 04 '22 00:12

Eric Martori


If you create a top-level function called main in any kotlin file, a green run button will appear next to it that allows you to run it as a program:
enter image description here

Note that this works in Android Studio as well as in IntelliJ IDEA.

like image 122
RobCo Avatar answered Dec 04 '22 00:12

RobCo