本文參考 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<Jar>("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<BasicAuthentication>("basic")
}
credentials {
username = "your-username" // 請填入你的用戶名
password = "your-password" // 請填入你的密碼
}
}
}
publications {
create<MavenPublication>("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 的說明:
由于前述末尾中的提示讓人煩心, 所以看了一下資料更新了一下發布功能:
更新步驟:
-
在項目的 gradle.properties 中添加一下配置:
android.disableAutomaticComponentCreation=true
-
在 Library Module 中的 build.gradle.kts 文件中補充:
android { ... publishing { singleVariant("release") { withSourcesJar() } } }
因為第二步中已經配置了發布源碼, 所以可以將前述中添加的打包源碼的 task(sourcesJar) 給移除掉;
-
修改發布邏輯:
afterEvaluate { publishing { repositories { maven { // isAllowInsecureProtocol = true // 如果Maven倉庫僅支持http協議, 請打開此注釋 url = uri("https://your-repository") // 請填入你的倉庫地址 authentication { create<BasicAuthentication>("basic") } credentials { username = "your-username" // 請填入你的用戶名 password = "your-password" // 請填入你的密碼 } } } publications { create<MavenPublication>("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") // (可選)開發者郵箱 } } } } } } }
完成.