Search code examples
androidkotlinmavengradle

Publish .aar to Jitpack using Kotlin Gradle without sources.jar


I am trying to publish Adnroid library .aar to Jipack using maven-publish without sources.jar from kotlin gradle kts.

AGP: 8.2.2

Kotlin: 1.9.22

Gradle: 8.2

Java: 17

Android Studio: Hedgehog

Below is the library module build.gradle.kts

import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.archivesName

kotlin {
    jvmToolchain(17)
}

plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.android")
    id("maven-publish")
}

android {
    namespace = "com.example.mylibrary"
    compileSdk = 34

    defaultConfig {
        minSdk = 24
        version = "2.0"
        archivesName = "myLib"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }

    buildFeatures {
        buildConfig = true
    }
}

dependencies {
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.11.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

publishing {
    publications {
        register<MavenPublication>("release") {
            groupId = "com.example.mylibrary"
            artifactId = project.archivesName.get()
            version = project.version.toString()
            pom.packaging = "aar"
            artifact("$buildDir/outputs/aar/myLib-release.aar")
            afterEvaluate {
                from(components["release"])
            }
        }
    }

    repositories {
        maven {
            url = uri("https://jitpack.io")
        }
    }
}

Jitpack produced artifacts reference:

Files:


com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b

com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b/Jitpack-Artifact-PoC-bcf5c2b-sources.jar

com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b/Jitpack-Artifact-PoC-bcf5c2b.aar

com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b/Jitpack-Artifact-PoC-bcf5c2b.module

com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b/Jitpack-Artifact-PoC-bcf5c2b.pom

com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b/Jitpack-Artifact-PoC-bcf5c2b.pom.md5

com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b/Jitpack-Artifact-PoC-bcf5c2b.pom.sha1

com/github/amirraza/Jitpack-Artifact-PoC/bcf5c2b/build.log

Solution

  • So, after spending much time on the internet to find the best possible answer. I finally got the solution. What you have to do is to disable the gradle task that runs to generate sources.

    Module level build.gradle.kts:

    /**
     * Configures the publishing of the Android library to a Maven repository, 
     * targeting JitPack. This script defines a Maven publication named "release"
     * and configures artifact publication and dependency management.
     */
    afterEvaluate {
        publishing {
            publications {
                register<MavenPublication>("release") {
                    from(components["release"])
                    //groupId = "com.example.myapp"
                    artifactId = "HelloLib"
                    version = "1.0.0"
                }
            }
            repositories {
                // Configure JitPack repository
                maven {
                    url = uri("https://jitpack.io")
                    credentials {
                        username = gradleProperties.getProperty("authToken")?.toString() ?: ""
                    }
                }
            }
        }
    }
    
    
    /**
     * Disables the execution of specific tasks of type AbstractArchiveTask.
     * It iterates over all tasks of this type and checks if their names are
     * either "releaseSourcesJar" or "debugSourcesJar". If a task has one of these names,
     * its enabled property is set to false, effectively preventing it from executing.
     */
    tasks.withType<AbstractArchiveTask> {
        if(this.name == "releaseSourcesJar" || this.name == "debugSourcesJar") {
            enabled = false
        }
    }