安卓不同gradle版本怎么发布sdk到Maven

一直以来,公司的很多sdk上传时都没有附带源码,对于开发者来说是比较不方便的,所以我就想增加一下。因为不同的项目使用的gradle版本不一样,发布sdk的方式也不一样,特此记录一下,希望能帮到大家。

gradle7.1之前

maven插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apply plugin: 'maven'

task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}

uploadArchives {
repositories.mavenDeployer {
//repo_url、repo_username、repo_password定义在local.properties或者gradle.properties
repository(url: repo_url) {
authentication(userName: repo_username, password: repo_password)
}
pom.version = "1.1.2"
pom.groupId = "com.wesley.sdk"
pom.artifactId = "xxx"
pom.name = "xxx"
pom.packaging = 'aar' //打包的格式,aar或者jar

}
}

artifacts {
archives androidSourcesJar
}

如果是java和android混合工程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apply plugin: 'maven'

task androidSourcesJar(type: Jar) {
classifier = 'sources'
if (project.plugins.hasPlugin('java')) {
println "This is a Java project."
from sourceSets.main.java.srcDirs
} else if (project.plugins.hasPlugin('com.android.application') || project.plugins.hasPlugin('com.android.library')) {
println "This is an Android project."
from android.sourceSets.main.java.srcDirs
} else {
println "This is an unknown type of project."
}
}

uploadArchives {
repositories.mavenDeployer {
//repo_url、repo_username、repo_password定义在local.properties或者gradle.properties
repository(url: repo_url) {
authentication(userName: repo_username, password: repo_password)
}
pom.version = "1.1.2"
pom.groupId = "com.wesley.sdk"
pom.artifactId = "xxx"
pom.name = "xxx"
pom.packaging = 'aar' //或者jar

}
}

artifacts {
archives androidSourcesJar
}

上传命令:gradlew uploadArchives

maven-publish插件

The Maven Publish Plugin

Upgrading your build from Gradle 6.x to the latest

gradle7开始去除maven插件,需要用maven-publish替代。其实在Gradle 6.2 之后,就完全废弃了。

gradle maven-publish 插件,从gradle 1.3 之后开始支持。

Removal of the uploadArchives task

The uploadArchives task was used in combination with the legacy Ivy or Maven publishing mechanisms. It has been removed in Gradle 7. You should migrate to the maven-publish or ivy-publish plugin instead.

Android Gradle Plugin 3.6.0开始支持maven-publish插件(gradle版本5.6.4+)

Android Studio | Android Developers

Support for the Maven Publish plugin

The Android Gradle plugin includes support for the Maven Publish Gradle plugin, which allows you to publish build artifacts to an Apache Maven repository. The Android Gradle plugin creates a component for each build variant artifact in your app or library module that you can use to customize a publication to a Maven repository.

To learn more, go to the page about how to use the Maven Publish plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
plugins {
id 'com.android.library'
id 'maven-publish'
}

tasks.register('androidSourcesJar', Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}

afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
artifact androidSourcesJar

groupId = "com.wesley.sdk"
artifactId = "xxxxx"
version = "1.1.2"
}
}

repositories {
maven {
allowInsecureProtocol true
def releaseUrl = "http://172.20.12.12:8080/nexus/content/repositories/releases/" //发布地址
def snapshotsUrl = "http://172.20.12.12:8080/nexus/content/repositories/snapshots/" //快照地址
url = SDK_TV_MAVEN_VERSION.endsWith('SNAPSHOT') ? snapshotsUrl : releaseUrl
credentials {
username project.username //username定义在local.properties或者gradle.properties
password project.password //password定义在local.properties或者gradle.properties
}
}
}
}
}

上传命令:gradlew publish

gradle 7.1开始

一定要优先查看英文版教程,中文版有时候更新不及时。

Publish your library | Android Studio | Android Developers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
plugins {
id 'com.android.library'
id 'maven-publish' //依赖的发布插件
}

group "com.wesley.sdk"
version "1.0.4" //版本号控制

android {
publishing {
singleVariant('release') {
withSourcesJar() //发布源码,方便SDK使用者阅读
}
}
}

publishing {
publications {
maven(MavenPublication) {
groupId group //sdk的组织
artifactId "theme-tools" //sdk名称
version version

afterEvaluate {
from components.release
}
}
}

repositories {
maven {
credentials {
username project.username //username定义在local.properties或者gradle.properties
password project.password //password定义在local.properties或者gradle.properties
}

allowInsecureProtocol true //如果是http协议需要加

def releaseUrl = "http://172.20.12.12:8080/nexus/content/repositories/releases/" //发布地址
def snapshotsUrl = "http://172.20.12.12:8080/nexus/content/repositories/snapshots/" //快照地址

url = version.endsWith('SNAPSHOT') ? snapshotsUrl : releaseUrl
}
}
}

上传命令:gradlew publish

更加推荐android studio的侧边栏Gradle-模块名字-Tasks-publishing-publish

如果没有任务需要打开下面的设置

image-20240608184315147

参考

Upload your library | Android Studio | Android Developers

Publishing | Android Developers

把Android库项目发布到maven仓库且带上源代码_将android项目上传至远程moven库-CSDN博客Android SDK 上传 Maven 喂奶级教程_android 上传sdk到maven-CSDN博客

使用Gradle插件上传组件到Maven之源码上传 - 掘金

【Gradle】gradle项目转maven之maven-publish插件的使用_publishtomavenlocal-CSDN博客

Plugin with id ‘maven’ not found. - 简书

Android Gradle 7.0 使用maven-publish发布组件_android gradle publishing-CSDN博客

Android:发布aar包到maven仓库以及 maven插件 和 maven-publish 插件的区别-腾讯云开发者社区-腾讯云

Android 多模块(lib)批量打包 aar 上传 maven 仓库实践-阿里云开发者社区

使用 Gradle Kotlin DSL 将 Android aar 发布到 Maven Repository - JFrog 的几点经验 - 掘金


安卓不同gradle版本怎么发布sdk到Maven
https://iwesley.top/article/3d160ee3/
作者
Wesley
发布于
2024年6月8日
许可协议