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

问题描述 投票:1回答:1

我有以下gradle模块“calculator”,其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()
}

它是一个多平台的liblary,它针对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项目中?

gradle kotlin
1个回答
0
投票

您的配置没有任何问题,只需将kotlin版本提升到1.3.0及以上版本。也使用gradle版本4.6及以上。

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

全部设置,同步和开始调试

© www.soinside.com 2019 - 2024. All rights reserved.