简体   繁体   中英

Trying to share com.apollographql.apollo3 between my kmm gradle files

Working on an Android project that has a lot of KMM modules, so I've tough I would extract a common gradle file and simply use it from the project specific gradle files.

My common gradle file is shared-library.gradle.kts

package commons

import dependencies.Dependencies
import dependencies.TestDependencies

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.apollographql.apollo3")
    id("com.android.library")
}

version = "1.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(Dependencies.Koin.CORE)
                implementation(Dependencies.Result.KMM)
                implementation(Dependencies.Coroutines.CORE)
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation(TestDependencies.KOIN)
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    compileSdk = BuildAndroidConfig.COMPILE_SDK_VERSION
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = BuildAndroidConfig.MIN_SDK_VERSION
        targetSdk = BuildAndroidConfig.TARGET_SDK_VERSION
    }
}

And then I can go use it like this from a build.gradle.kts

import dependencies.Dependencies

plugins {
    id("commons.shared-library")
}

....

This all works great except the id("com.apollographql.apollo3") part, when added in the shared gradle file I get the following compilation error

org.gradle.internal.exceptions.LocationAwareException: Precompiled script plugin '/Users/calin/Playground/SharedAppSample/buildSrc/src/main/kotlin/commons/shared-library.gradle.kts' line: 1
Plugin [id: 'com.apollographql.apollo3'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)

I see that the plugin is available as a gradle plugin https://plugins.gradle.org/search?term=com.apollographql.apollo3

And I have setting.gradle.kts configured like this

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

But for some reason the KMM Gradle file will ignore this configuration (maybe?)

The error says that it can't use the plugin repositories because no version is provided.

- Plugin Repositories (plugin dependency must include a version number for this source)

There are a few different ways of providing a version. Using buildSrc is my preferred way.

If shared-library.gradle.kts is a buildSrc convention plugin, then add a dependency using the plugins Maven coordinates (not the plugin ID!) in ./buildSrc/build.gradle.kts .

The Maven coordinates are available from the Gradle plugin portal

Gradle 插件门户的屏幕截图,突出显示插件的 Maven 坐标的位置为 classpath("com.apollographql.apollo3:apollo-gradle-plugin:3.5.0")

// ./buildSrc/build.gradle.kts

...

dependencies {
  implementation("com.apollographql.apollo3:apollo-gradle-plugin:3.5.0")
}

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