简体   繁体   中英

How to add dependency to kotlin multiplatform projects with single build.gradle configuration

I have the following gradle module "calculator" whose build.gradle file looks as shown below. It works perfectly

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()
}

It is a multiplatform liblary, which has targeted the jvm and the js platform.

I have another module "someapp", that's build.gradle looks like this as well

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
}

My settings.gradle looks like this

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

Now, I want to add the jvm platform target of the calculator module using gralde to someapp project without publishing to maven. Can someone help me how? I seem to stuck at this point, and I wanna move forward with this type of configuration

Extra question: Suppose I add an android target in my calculator module, how do I add it as a dependency to an android project?

You have got nothing wrong on your configuration, just bump you kotlin version to 1.3.0 and above. also use gradle version 4.6 and above.

To change your gradle version, just go into the file gradle/wrapper/gradle-wrapper.properties change the the distributionUrl to distributionUrl=https\\://services.gradle.org/distributions/gradle-4.6-all.zip .

all set, sync and start debuging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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