使用maven-publish插件发布Android工件(kts)

本文参考 Android Developers 相关文档(源地址).


在项目模块化的过程中, 各个模块单独进行开发, 最终在上层模块引入.
为了引入方便和版本管理, 通常会使用 maven 来管理各个底层模块.

如果你的项目是使用 Gradle 构建的, 那么 Gradle 提供了一个很方便的插件用于发布模块工件: maven-publish , 我们参考 Android Developer 的说明, 可以很快速的在使用 Groovy 的 Gradle 中实现发布. 不过遗憾的是官方文档中没有提供使用 kotlin-kts 相关的指引.

在研究后整理了以下关于发布 library 为 Maven 工件的配置, 请添加到 Library Module 的 build.gradle.ktx 中:

plugins {
    id("com.android.library")
    id("maven-publish") // 添加插件
    ...
}

// 创建一个task来发布源码
tasks.register("sourcesJar") {
    archiveClassifier.set("sources")
    val sources = android.sourceSets.map { set -> set.java.getSourceFiles() }
    from(sources)
}

afterEvaluate {
    publishing {
        repositories {
            maven {
                // isAllowInsecureProtocol = true // 如果Maven仓库仅支持http协议, 请打开此注释
                url = uri("https://your-repository") // 请填入你的仓库地址
                authentication {
                    create("basic")
                }
                credentials {
                    username = "your-username" // 请填入你的用户名
                    password = "your-password" // 请填入你的密码
                }
            }
        }

        publications {
            create("product") {
                from(components["release"])
                groupId = "your-group-id" // 请填入你的组件名
                artifactId = "your-artifact-id" // 请填入你的工件名
                version = "your-version" // 请填入工件的版本名
                artifact(tasks["sourcesJar"]) // 打包源码到工件中

                pom {
                    name.set("name") // (可选)为工件取一个名字
                    url.set("https://xxx") // (可选)网站地址
                    developers {
                        developer {
                            name.set("name") // (可选)开发者名称
                            email.set("email") // (可选)开发者邮箱
                        }
                    }
                }
            }
        }
    }
}

添加完配置之后, 可以执行以下 gradle task 发布工件(假设 Module 名称为 lib):
gradle clean :lib:build :lib:sourcesJar :lib:publish

如果在终端中使用 gradle-wrapper , 则可以使用:

  • MacOS/Linux: ./gradlew clean :lib:build :lib:sourcesJar :lib:publish
  • Windows: gradlew.bat clean :lib:build :lib:sourcesJar :lib:publish

近期升级 AGP 到 7.1.2 时发现出现了以下提示:

Software Components will not be created automatically for Maven publishing from Android Gradle Plugin 8.0. To opt-in to the future behavior, set the Gradle property android.disableAutomaticComponentCreation=true in the gradle.properties file or use the new publishing DSL.

估计在 AGP 8.0 版本之后上述的方法会失效, 到时候再看看有没有相关文档补充~


更新: 关于适配新版 AGP 的说明:

由于前述末尾中的提示让人烦心, 所以看了一下资料更新了一下发布功能:

更新步骤:

  1. 在项目的 gradle.properties 中添加一下配置:

    android.disableAutomaticComponentCreation=true
    
  2. 在 Library Module 中的 build.gradle.kts 文件中补充:

    android {
        ...
        publishing {
            singleVariant("release") {
                withSourcesJar()
            }
        }
    }
    
  3. 因为第二步中已经配置了发布源码, 所以可以将前述中添加的打包源码的 task(sourcesJar) 给移除掉;

  4. 修改发布逻辑:

    afterEvaluate {
        publishing {
            repositories {
                maven {
                    // isAllowInsecureProtocol = true // 如果Maven仓库仅支持http协议, 请打开此注释
                    url = uri("https://your-repository") // 请填入你的仓库地址
                    authentication {
                        create("basic")
                    }
                    credentials {
                        username = "your-username" // 请填入你的用户名
                        password = "your-password" // 请填入你的密码
                    }
                }
            }
    
            publications {
                create("release") {
                    from(components["release"])
                    groupId = "your-group-id" // 请填入你的组件名
                    artifactId = "your-artifact-id" // 请填入你的工件名
                    version = "your-version" // 请填入工件的版本名
    
                    pom {
                        name.set("name") // (可选)为工件取一个名字
                        url.set("https://xxx") // (可选)网站地址
                        developers {
                            developer {
                                name.set("name") // (可选)开发者名称
                                email.set("email") // (可选)开发者邮箱
                            }
                        }
                    }
                }
            }
        }
    }
    
  5. 完成.

你可能感兴趣的:(使用maven-publish插件发布Android工件(kts))