简体   繁体   English

为 Kotlin 多平台项目设置 gradle 和项目结构

[英]Setting up gradle and project structure for Kotlin Multiplatform project

I want to build a CLI tool with Kotlin Multiplatform which runs on Linux, Macos and Windows.我想使用 Linux、Macos 和 Windows 上运行的 Kotlin Multiplatform 构建一个 CLI 工具。

But I am struggling with setting up my build.gradle and my project structure.但我正在努力设置我的build.gradle和我的项目结构。 I am using IntelliJ IDEA 2020.1 and created my basic project with File -> New -> Project -> Kotlin / Native | Gradle我正在使用IntelliJ IDEA 2020.1并使用File -> New -> Project -> Kotlin / Native | Gradle创建了我的基本项目File -> New -> Project -> Kotlin / Native | Gradle

Currently I am looking through guides from kotlinlang.org but I am more falling then achieving something.目前我正在浏览来自kotlinlang.org的指南,但我更多的是跌倒而不是取得一些成就。

So far my build.gradle looks as follows:到目前为止,我的build.gradle如下所示:

plugins {
   id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
}
repositories {
    mavenCentral()
}
kotlin {
// For ARM, should be changed to iosArm32 or iosArm64
// For Linux, should be changed to e.g. linuxX64
// For MacOS, should be changed to e.g. macosX64
// For Windows, should be changed to e.g. mingwX64

     linuxX64("linux") {

     }
     mingwX64("mingw") {

     }
     macosX64("macos") {
         binaries {
             executable {
            // Change to specify fully qualified name of your application's entry point:
                entryPoint = 'sample.main'
            // Specify command-line arguments, if necessary:
            runTask?.args('')
        }
    }
}

sourceSets {
    commonMain {
        kotlin.srcDir('src/main')
        resources.srcDir('src/res')
        dependencies {
            implementation kotlin('stdlib-common')
            implementation "com.github.ajalt:clikt-multiplatform:2.7.0"
        }
    }
    commonTest {
        dependencies {
            implementation kotlin('test-common')
            implementation kotlin('test-annotations-common')
        }
    }

    macosX64().compilations.test.defaultSourceSet {
        dependsOn commonMain
    }
    // 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 {
    }
}
}

 wrapper {
    gradleVersion = "6.4.1"
    distributionType = "ALL"
} 

And my project structure is still basic:而且我的项目结构还是基本的:

Project structure项目结构

Formerly I only worked on Android Projects with Kotlin, and I guess I am spoiled with gradle as Android generates the most basic stuff and everything is working without doing that much. Formerly I only worked on Android Projects with Kotlin, and I guess I am spoiled with gradle as Android generates the most basic stuff and everything is working without doing that much.

I understand that I need to create packages like linuxMain and mingwMain , but where to I put common sourcesets?我知道我需要创建像linuxMainmingwMain这样的包,但是我在哪里放置公共源集? I tried to create a package called commonMain , but it won't even let me create Kotlin files in that package.我试图创建一个名为 commonMain 的commonMain ,但它甚至不允许我在该 package 中创建 Kotlin 文件。

When I am finished I want to have (in the best case) one common source set and one entry point for all my targets.完成后,我希望(在最好的情况下)拥有一个公共源集和一个用于所有目标的入口点。 Is this even possible?这甚至可能吗?

As far as I can see, you specify your commonMain source set's source locations as /src/main/ .据我所知,您将 commonMain 源集的源位置指定为/src/main/ By default, it's usually set onto /src/commonMain/kotlin/ .默认情况下,它通常设置为/src/commonMain/kotlin/ So if you will remove those srcDir settings and create a.kt file in your /src/commonMain/kotlin/ folder, everything should work fine.因此,如果您将删除这些srcDir设置并在/src/commonMain/kotlin/文件夹中创建一个 .kt 文件,那么一切都应该正常工作。 Also, I hope you have removed 'kotlin.import.noCommonSourceSets' property from your gradle.properties as your script recommended.另外,我希望您已按照脚本推荐的方式从gradle.properties中删除了“kotlin.import.noCommonSourceSets”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM