簡體   English   中英

為 Kotlin 多平台項目設置 gradle 和項目結構

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

我想使用 Linux、Macos 和 Windows 上運行的 Kotlin Multiplatform 構建一個 CLI 工具。

但我正在努力設置我的build.gradle和我的項目結構。 我正在使用IntelliJ IDEA 2020.1並使用File -> New -> Project -> Kotlin / Native | Gradle創建了我的基本項目File -> New -> Project -> Kotlin / Native | Gradle

目前我正在瀏覽來自kotlinlang.org的指南,但我更多的是跌倒而不是取得一些成就。

到目前為止,我的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"
} 

而且我的項目結構還是基本的:

項目結構

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.

我知道我需要創建像linuxMainmingwMain這樣的包,但是我在哪里放置公共源集? 我試圖創建一個名為 commonMain 的commonMain ,但它甚至不允許我在該 package 中創建 Kotlin 文件。

完成后,我希望(在最好的情況下)擁有一個公共源集和一個用於所有目標的入口點。 這甚至可能嗎?

據我所知,您將 commonMain 源集的源位置指定為/src/main/ 默認情況下,它通常設置為/src/commonMain/kotlin/ 因此,如果您將刪除這些srcDir設置並在/src/commonMain/kotlin/文件夾中創建一個 .kt 文件,那么一切都應該正常工作。 另外,我希望您已按照腳本推薦的方式從gradle.properties中刪除了“kotlin.import.noCommonSourceSets”屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM