Kotlin Multiplatform - how to build .class file


#1

considering Kotlin 1.2 introduced kotlin-platform-common I’m trying to build my first common .class file, so I did the below:

main.kt:

package hello

fun main() {
    println("kotlin!")
} 

gradle.build:

group 'h'
version 'prn'

buildscript {
    ext.kotlin_version = '1.2.0'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin-platform-common'

repositories {
mavenCentral()
}

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

sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}

jar {
manifest {
    attributes 'Main-Class': 'hello.MainKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

Then I run gradle build

Q1- I coud not find any .class file generated? how to get it generated and where should I find it?

Q2- I got a .jar file generated, but once I tried to run it, I got an error:

Error: Could not find or load main class hello.MainKt

I tried to run it using the below 2 option, but got the same error for bot:

Option 1:

kotlin -cp <filename>.jar hello.MainKt
Option 2:

java -jar <filename>.jar
Project structure, and errors are as in this pic below:


#2

A common project does not produce .class files, because it’s - well - common, and .class files are binaries for JVM platform.

In order to build runnable binaries you need to create another subproject, apply kotlin-platform-jvm plugin there and declare an expectedBy dependency on your common project (looks like it’s the root project in your case).