Run single kotlin class with main function in android studio

后端 未结 15 662
后悔当初
后悔当初 2020-12-13 13:04

I am trying to get familiar with Kotlin to use in my android apps. So first I want to try out some simple kotlin examples, just to get familiar with syntax of kotlin.

<
相关标签:
15条回答
  • 2020-12-13 13:29

    It is supported now (V 3.2.1 )

    I just finished upgrading my Android studio, created A new project I then waited until all the building finished ( if you are advised to upgrade something please accept )

    After that I created a new Kotlin file and added your code, right click and choose the Run option and that's it .

    I can see the following in the console

    Hello World

    Process finished with exit code 0

    0 讨论(0)
  • 2020-12-13 13:30

    Update:

    Approach 1:

    Now you can create a kotlin file with empty main() method and then you can run the code directly using run icon on left(of 7th line) in android studio (AS version: 3.5.3 ) editor like

    This will internally create the TestKt(name of file) class with PSVM method(and required structure) to execute the code.

    Demo configuration(automatically generated) to run this file will look like

    Approach 2(with Scratch file, tested on AS 3.6):

    1. Select the Project view in the project navigation panel.
    2. Create a Kotlin scratch file from New -> Scratch File -> Kotlin
    3. Now add your code and see the result on the right-side panel

    Android studio (intellij) provides REPL(Real Eval Print Loop) tool to write and execute kotlin code.

    1. Open kotlin REPL as Tool -> kotlin -> kotlin REPL

    1. Write your code

    1. Press command + enter (on mac) to execute your code(pay attention to the key combo on different platform)

    Either write code or import the class

    Tips:

    • Rebuilt the project once you change the source code
    • Use arrow key to go back in history
    0 讨论(0)
  • 2020-12-13 13:31

    This is simply not possible as of now in Android Studio 3.0.

    There is a bug filed for this already: https://issuetracker.google.com/issues/68021152

    0 讨论(0)
提交回复
热议问题