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.
<
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.
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
}
}
...
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.