Run single kotlin class with main function in android studio

后端 未结 15 663
后悔当初
后悔当初 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:13

    The easiest way is to create a new Java library module like this, and configure it for Kotlin.

    You also have to add this in your build.gradle of the imported module:

    dependencies {
        runtimeClasspath files(compileKotlin.destinationDir)
    }
    
    0 讨论(0)
  • 2020-12-13 13:14

    In my case I add these:

    1. Add this apply plugin: 'kotlin-android'

    2. And also

    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$your-version' 
    }
    

    This fixes the problem.

    0 讨论(0)
  • 2020-12-13 13:15
    1. Create default project with gradle

      gradle init -> kotlin-application

    2. Import project into Android Studio
    3. Create new 'run configuration' with 'Application' template and set 'app.AppKt' as a 'Main class' (see build.gradel->mainClassName).

    4. Run!

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

    Tested on Android Studio 3.1.3

    Note that this is an edited version of my other answer.

    Using this method you can have Java/Kotlin modules and Android modules in the same project and also have the ability to compile and run Java/Kotlin modules as stand alone projects.

    1. Open your Android project in Android Studio. If you do not have one, create one.
    2. Click File > New Module. Select Java Library and click Next.
    3. Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project.
    4. Add your Java/Kotlin code to the Java module you've just created.
    5. Click on the drop down to the left of the run button. Click Edit Configurations...
    6. In the new window, click on the plus sign at the top left of the window and select Application
    7. A new application configuration should appear, enter in the details such as your main class and classpath of your module.
    8. Click OK.
    9. Next we need to add the Kotlin plugin. Add the following code to your project level build.gradle (lines to add are denoted by >>>):

      buildscript {
          >>> ext.kotlin_version = '1.2.51'
          repositories {
              google()
              jcenter()
          }
          dependencies {
              classpath 'com.android.tools.build:gradle:3.1.3'
              >>> classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
      
              // NOTE: Do not place your application dependencies here; they belong
              // in the individual module build.gradle files
          }
      }
      ...
      
    10. Add the following code to your module level build.gradle (lines to add are denoted by >>>):

      apply plugin: 'java-library'
      >>> apply plugin: 'kotlin'
      
      dependencies {
          implementation fileTree(dir: 'libs', include: ['*.jar'])
          >>> implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
          >>> runtimeClasspath files(compileKotlin.destinationDir)
      }
      ...
      

    Now if you click run, this should compile and run your Java/Kotlin module.

    If you get the error Error: Could not find or load main class..., just enter your main class (as you've done in step 7) again even if the field is already filled in. Click Apply and then click Ok.

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

    I faced the same problem and a workaround is run your code in a test class under test folder, then right-click on Run {your test class}

    It is enough if you only want to play with Kotlin.

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

    In my Android Studio, simply open the Kotlin file with your main function, then to the left of the main function there's a green "Run" triangle. Just click on that one to run this file.

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