The docs are terrible around getting started with ...
# kotlin-native
a
The docs are terrible around getting started with a project in kotlin native btw 😞
5
👍 3
t
starting a "Kotlin (Mobile shared library)" in IntelliJ Idea EAP has given me the best results, so far
👍 1
o
a
Nope. I looked around the repo, tried various updates to various jetbrains IDEs, googled a bunch, and also looked at https://kotlinlang.org/docs/reference/native-overview.html#how-to-start where I expected a clear, reliable entrypoint into how to get started.
https://kotlinlang.org/docs/tutorials/native/basic-kotlin-native-app.html was relatively clear and I had already gotten that far just from the github repo, but it’s really different from how you would set up a project in IntelliJ (from what I can tell).
All I wanted to do was to try and build a simple CLI program and compile binaries for macos and linux it has been really difficult.
I understand and love Kotlin as a language, but the native tooling and documentation has been a nightmare.
The architecture seems great (with the LLVM backing and all), but we (as a community) are really missing that clear path for new users to get started, in my opinion.
o
Easiest way to create simple CLI app is using new project wizard in IDEA
a
That’s what I ended up doing and it seemed to work decently, but it was automatically configured to target macos. I’ve had trouble finding gradle documentation on how to add linux as a second target for compilation.
Copy code
plugins {
    id 'kotlin-multiplatform' version '1.3.10'
}
repositories {
    mavenCentral()
}
kotlin {
    targets {
        // For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
        // For Linux, preset should be changed to e.g. presets.linuxX64
        // For MacOS, preset should be changed to e.g. presets.macosX64
        fromPreset(presets.macosX64, 'macos')

        configure([macos]) {
            // Comment to generate Kotlin/Native library (KLIB) instead of executable file:
            compilations.main.outputKinds('EXECUTABLE')
            // Change to specify fully qualified name of your application's entry point:
            compilations.main.entryPoint = 'sample.main'
        }
    }
    sourceSets {
        // Note: To enable common source sets please comment out 'kotlin.import.noCommonSourceSets' property
        // in gradle.properties file and re-import your project in IDE.
        macosMain {
        }
        macosTest {
        }
    }
}

task runProgram {
    def buildType = 'release' // Change to 'debug' to run application with debug symbols.
    dependsOn "link${buildType.capitalize()}ExecutableMacos"
    doLast {
        def programFile = kotlin.targets.macos.compilations.main.getBinary('EXECUTABLE', buildType)
        exec {
            executable programFile
            args ''
        }
    }
}
I see options for how to change the primary target, but it isn’t clear how to target multiple platforms at once.
Similarly, a couple of other topics that I’m looking for information on are: • Platform APIs for functionality like reading files. The closet thing I could find was the CSV parser example. • Best practices for native projects. If I need to parse yaml, do I need to search for pure kotlin libraries? Is gradle the only way to add a dependency like that?
d
If you want to use any functionality that isn't in the standard library, like platform apis or libraries, at this point in time you'll have to look for C libraries and use the cinterop tool to use them, as there aren't many pure kotlin native libraries out there. You'll find more information here https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md and https://github.com/JetBrains/kotlin-native/blob/master/PLATFORM_LIBS.md