You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Kotlin Gradle KTS将Android .aar发布到Jitpack且不含sources.jar

问题描述

我使用maven-publish插件通过Kotlin Gradle KTS将Android库的.aar文件发布到Jitpack时,希望不生成sources.jar,但当前Jitpack生成的产物中仍包含该文件。环境信息如下:

  • AGP:8.2.2
  • Kotlin:1.9.22
  • Gradle:8.2
  • Java:17
  • Android Studio:Hedgehog

当前Jitpack产物包含不需要的Jitpack-Artifact-PoC-bcf5c2b-sources.jar,需要解决该问题。


解决方法

要阻止生成sources.jar,需要从AGP配置、Maven发布配置、任务禁用三个层面入手:

1. 禁用AGP自动生成Sources Artifact

在android块中添加发布配置,明确告诉AGP不为release变体生成sources.jar:

android {
    // ... 原有配置 ...
    publishing {
        singleVariant("release") {
            withSourcesJar = false
        }
    }
}

2. 清理Maven Publication中的Sources Artifact

如果AGP的配置仍无法阻止生成,可在发布配置中手动移除sources相关产物:

publishing {
    publications {
        register<MavenPublication>("release") {
            // ... 原有配置 ...
            afterEvaluate {
                from(components["release"])
                // 移除所有classifier为sources的artifact
                artifacts.removeIf { it.classifier == "sources" }
            }
        }
    }
}

3. 禁用所有Sources相关Jar任务

直接禁用项目中所有生成sources jar的Gradle任务,彻底阻断生成路径:

tasks.withType<Jar>().matching { it.name.contains("sources") }.configureEach {
    enabled = false
}

修改后的完整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
    }

    // 禁用release变体的sources jar生成
    publishing {
        singleVariant("release") {
            withSourcesJar = false
        }
    }
}

// 禁用所有sources相关Jar任务
tasks.withType<Jar>().matching { it.name.contains("sources") }.configureEach {
    enabled = false
}

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"])
                // 移除已存在的sources artifact
                artifacts.removeIf { it.classifier == "sources" }
            }
        }
    }

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

验证

修改后重新推送代码触发Jitpack构建,产物中将不再包含sources.jar文件。

内容的提问来源于stack exchange,提问作者Amir Raza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 15:45:36