7

I have Android Studio installed, I know Android Studio has Kotlin support for android applications, however I am still learning the basics (Hello World, printing, basic things) and I would just like to run some basic programs.

Is there a way I can run simple (preferably single file) Kotlin scripts from Android Studio (maybe something the online tester) for testing, without going straight into the Android API and going straight on Android?

Installing the command line version is not a desirable option for me as I don't have the Java SDK (I am hoping for a way around intalling it). I also do not have Intellij Idea .

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 1
    Why don't you use IntelliJ IDEA? It has similar (rather identical) workflow to Android Studio. Its community edition is free. It handles the SDK installation part. [reference](https://kotlinlang.org/docs/tutorials/getting-started.html) – rushi Jul 03 '18 at 19:59
  • @rushi Thank you, I have seen that page (but didn't realise it dealt with the SDK for me), while I understand it's the easiest option I don't really want to keep installing extra tools, to remove them later. I was hoping I could just run in Android Studio until I moved on from complete basics and to developing Android properly but if I have no other option that is what I will have to do. – Xantium Jul 03 '18 at 20:03
  • 2
    Also, [this question](https://stackoverflow.com/questions/16626810/can-android-studio-be-used-to-run-standard-java-projects) might be helpful. I guess running "pure" kotlin script would be same as for "pure" java. However, I have not done this myself. – rushi Jul 03 '18 at 20:03
  • @rushi Thank you, that is exactly what I am hoping to acieve only in Kotlin, I might be able to get it working ;) – Xantium Jul 03 '18 at 20:07

3 Answers3

4

This is how I got it working:

First create a normal project. Then go to:

File menu -> New -> New Module -> Java Library

After that set your build.gradle as:

apply plugin: 'kotlin'

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

Then create a new kotlin file with the main method

kotlin code

To execute just click on the play/run green button on the left of the main method as seen in the image above.

And you are good to go!

crgarridos
  • 8,758
  • 3
  • 49
  • 61
1

I think Kotlin single file doesn't run as a module like java. The easiest way is to create a new Java library module like this, and configure it for Kotlin. After that make 2 classes, a Java class and a Kotlin class. Call your Kotlin methods from the Java main function. Check my complete answer here..

Shahzad Akram
  • 4,586
  • 6
  • 32
  • 65
0

I can execute Kotlin class by simply doing right-click to the Kotlin class, and click on "Run '[classname]' (Ctrl+Shift+F10) " and it works.

I don't see the reason why you should create a new module, or do workarounds...

Cicco
  • 1