apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.76k stars 653 forks source link

Publishing library that uses Apollo Kotlin 4.0.0 to maven repo requires declaring explicit dependency on certain Gradle task #6146

Closed seab4ss closed 1 month ago

seab4ss commented 1 month ago

Version

4.0.0

Summary

Our library project includes Apollo Kotlin. We use maven-publish plugin for publishing the library .aar file. After migrating from v3.8.2 to v4.0.0, we started seeing error when attempting to publish our library .aar artifact. It seems that task mylibrary:sourceReleaseJar has to depend on :mylibrary:generateMylibrarysvcApolloSources task To fix the issue, i added the following code in afterEvaluate {} in the myLibrary/build.gradle.kts:

afterEvaluate {
    val apolloSrcTask = project.tasks.getByName(“generateMylibrarysvcApolloSources”)
    val srcReleaseTask = project.tasks.getByName("sourceReleaseJar")
    srcReleaseTask.dependsOn(apolloSrcTask)
}

Is there some configuration setting in Apollo Gradle Plugin that will fix this issue?

Steps to reproduce the behavior

dependencies {

    // @Keep annotation
    implementation(libs.androidx.annotation)

    // Coroutines
    implementation(libs.kotlinx.coroutines.core)
    implementation(libs.kotlinx.coroutines.android)
    testImplementation(libs.kotlinx.coroutines.test)

    // Apollo core runtime dependencies
    implementation(libs.apollo.kotlin.runtime)
...
}

apollo {
    service("Mylibrarysvc) {
        // apollo compiler by default will generate Kotlin models from queries, mutations etc
        packageName.set("mylibrary")
        // Keep schemaFile
        schemaFile.set(file("mylibraryFilepath"))
        // explicitly set srcDir
        srcDir(file("graphqlFilesFilepath"))
}
}

here is publishing section in mylibrary/build.gradle.kts

publishing {
    publications {
        register<MavenPublication>(ARTIFACT_NAME) {
            groupId = ARTIFACT_PACKAGE
            artifactId = ARTIFACT_NAME
            version = getPublishingVersion(ARTIFACT_VERSION)

            // Library documentation created with Dokka
            artifact(dokkaDocsJarTask)

            // Note that when running the Maven Publish Plugin with AGP,
            // the software components are not created directly when the plugin is applied.
            // They are instead created during the afterEvaluate() callback step.
            // Therefore, the publication that selects the software component
            // must also be configured during the afterEvaluate() step.
            afterEvaluate {
                from(components["release"])
            }
        }
    }

    repositories {

        mavenLocal()
    }
}

run myLibrary/publishToMavenLocal task in Android Studio

Logs

FAILURE: Build failed with an exception.

martinbonnin commented 1 month ago

Hi 👋 I'm guessing you're using something like so to configure the android publication?

android {
  publishing {
    singleVariant("release") {
        withSourcesJar()
    }
  }
}

The generated sources are registered using srcDir() and I would have expected that to carry task dependencies but maybe there's more to it. I'll take a deeper look. In the meantime your workaround should work fine.

seab4ss commented 1 month ago

Yes. that's how we configured the android publication. Thank you

martinbonnin commented 1 month ago

@seab4ss fix is here. There's a surprising amount of context and backstory but I think/hope it's good. If you get a chance to try it out in the SNAPSHOTs, let me know how it goes (should land in the SNAPSHOTs once merged and deployed, hopefully today).

github-actions[bot] commented 1 month ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better.

martinbonnin commented 1 month ago

(PR merged, SNAPSHOTs are deploying here)

seab4ss commented 4 weeks ago

@martinbonnin this issue is fixed in Apollo Kotlin v4.0.1. Thank you!

martinbonnin commented 4 weeks ago

Glad to hear that! Thanks for the follow up!