4

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

Ananth
  • 2,597
  • 1
  • 29
  • 39
  • 1
    Can't you just create a new project with an empty Activity using the project wizard, and then add your own code? – Michael Jan 16 '19 at 13:10
  • @Michael I just want to run - fun printHello () { println ("Hello World") } – Ananth Jan 16 '19 at 13:28
  • 1
    I believe it's impossible in AndroidStudio you should use IntelliJ Idea instead – anber Jan 16 '19 at 13:31
  • 1
    @AnanthRajSingh: Android apps are based around activities. If you want to build some sort of command-line application then it sounds like you should develop for a desktop OS instead. – Michael Jan 16 '19 at 13:33
  • @anber I don't think so. Just know I figured out how to run Kotlin script file (.kts) there must be a way for .kt file also. – Ananth Jan 16 '19 at 13:33
  • @Michael You're getting me wrong here. I am not building any application. Just want to use Android Studio as an IDE for Kotlin files. – Ananth Jan 16 '19 at 13:36
  • 2
    AndroidStudio is intended for creating Android applications via Gradle. You may found some workaround how to run simple Kotlin file but its better to use appropriate tools – anber Jan 16 '19 at 14:08
  • Go through this https://youtu.be/u2I3g1hBv3A – Yogesh Nikam Patil Feb 16 '23 at 19:00

4 Answers4

15

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.

RobCo
  • 6,240
  • 2
  • 19
  • 26
  • 2
    It is giving me "Exception in thread "main" java.lang.ClassNotFoundException: KotlinClass2Kt" – Ananth Jan 16 '19 at 14:49
  • 3
    @AnanthRajSingh That depends entirely on your code and configuration. In Android Studio this still uses gradle to build your code as application. For example when you use Android system classes, it will compile fine but not run since these classes are not available at runtime. As others have suggested, you may try your luck using IntelliJ IDEA (community edition is also free). It uses a different environment and build system. Otherwise post your code and errors as separate question. – RobCo Jan 16 '19 at 14:57
  • I was trying it on Kotlin File. Now when I created Kotlin Class, that Play button is only able to Debug the class not Run it. When I debug it, it says "Error: Could not find or load main class KotlinClass3". Well anyway, I am grateful for your answer. I will use Kotlin REPL provided in Android Studio or use .kts scratch files. – Ananth Jan 16 '19 at 15:15
  • I run into the same issues as OP but with it having worked once but without changing the code it just stopped working. Claims to not find the class. – Giszmo Mar 22 '19 at 00:25
  • try [this](https://stackoverflow.com/questions/44197521/gradle-project-java-lang-noclassdeffounderror-kotlin-jvm-internal-intrinsics) – Siddhivinayak Dec 20 '19 at 19:39
  • For me this worked in IntelliJ IDEA but didn't in Android Studio. – Michal Vician Dec 31 '21 at 11:46
  • when we are checking for a piece of code. this also stars building the whole project and if there is error in project, this can't run this code. – Vikas Pandey Aug 31 '22 at 14:14
6

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.

Eric Martori
  • 2,885
  • 19
  • 26
2

Android Studio allow to run Kotlin code, but the green triangle (to Run) appears just if you don't use a class file or if you add a fun main(args: Array<String>){} out of the class's brackets. So, the way I am doing this (in AS 4.0) is:

  1. Creating a Kotlin FILE (not choose a CLASS) and putting inside just:

    fun main(args: Array) {}

  2. Then, inside this method you are allowed to use Kotlin classes. You can do for example:

    fun main(args: Array){ var oneMan: Man = Man() oneMan.method() }

being Man a regular Kotlin class.

lm2a
  • 835
  • 1
  • 10
  • 19
1

Use this code, You will see a run icon near function.

class Test {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            println("Hello test!")
        }
    }
}
Yamini
  • 732
  • 7
  • 11