如何使用单个build.gradle配置向Kotlin多平台项目添加依赖项

时间:2018-11-04 01:55:21

标签: gradle kotlin

我有以下gradle模块“计算器”,其build.gradle文件的外观如下所示。效果很好

buildscript {
    ext {
        kotlin_version = "1.2.71"
        coroutines_version = "0.30.1"
    }

    repositories {
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version "1.2.71"
}

group 'mpp2-calc'
version '1.1'


kotlin {
    targets {
        fromPreset(presets.jvm, 'jvm') {
            compilations.all {
                tasks[compileKotlinTaskName].kotlinOptions {
                    jvmTarget = '1.8'
                }
            }
        }
        fromPreset(presets.js, 'js') {
            compilations.all {
                tasks[compileKotlinTaskName].kotlinOptions {
                    metaInfo = true
                    outputFile = "${projectDir}/build/classes/main/${project.name}.js"
                    sourceMap = true
                    moduleKind = 'umd'
                    main = 'call'
                }
            }
        }
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
            }
        }
        commonTest {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
                implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
            }
        }
        jvmMain {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
            }
        }
        jvmTest {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
                implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
            }
        }
        jsMain {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutines_version"
            }
        }
        jsTest {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
            }
        }
    }
}

repositories {
    maven { url = 'https://dl.bintray.com/russhwolf/multiplatform-settings' }

    google()
    jcenter()
}

这是一个多平台库,针对的是jvm和js平台。

我还有另一个模块“ someapp”,即build.gradle也是如此

buildscript {
    ext.kotlin_version = '1.2.71'

    repositories {
        maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: "application"

group 'mpp2'
version '1.1'

mainClassName = "com.asofttz.someapp.indexKt"

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    api project(":calculator") // doesn't seem to show any effect
}

我的settings.gradle看起来像这样

rootProject.name = 'mpp2'
include ':calculator'
include ':someapp'

现在,我想使用gralde将计算器模块的jvm平台目标添加到someapp项目中,而不发布到maven。有人可以帮我吗?我似乎停留在这一点上,我想继续使用这种类型的配置

其他问题: 假设我在计算器模块中添加了一个android目标,如何将其作为依赖项添加到android项目中?

1 个答案:

答案 0 :(得分:0)

您的配置没有任何问题,只需将Kotlin版本提高到1.3.0及更高版本即可。还可以使用gradle版本4.6及更高版本。

要更改gradle版本,只需进入文件gradle/wrapper/gradle-wrapper.properties,将distributionUrl更改为distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

所有设置,同步并开始调试