2023年12月8日发(作者:)

实践AndroidStudio中Gradle发布模块aar到GitHubPackages

Publish Android library to GitHub Packages

Working with the Gradle registry

具体代码包括

GitHub Action

脚本可以参考我的项目

目前

GitHub Packages

不支持像

jcenter

jitpack

那样的匿名下载,具体可参考我这篇文章

1. 确保本地能打包

这里我用的是release包,体积小,并且被正确混淆。

使用

./gradlew :johnbase:assembleRelease

测试成功,生成文件路径为

JohnBase/build/outputs/aar/

2. 添加

Publish

脚本

这里为了原本

的内容不被干扰,我们将发布相关内容移到

文件内容如下

apply plugin: 'y'

apply plugin: 'kotlin-android'

apply from: ''

文件内容如下

apply plugin: 'maven-publish'

buildscript {

ext{

GitHubName = "oOJohn6Oo"

// Personal Access Token

PAT = null

File localFile = ('ties')

if (()) {

Properties properties = new Properties()

(aInputStream())

PAT = perty('')

}

}

}

publishing {

repositories {

maven {

name = "GitHubPackages"

url = uri("/$GitHubName/BaseAndroid")

credentials {

username = "$GitHubName" ?: ("USERNAME")

if (PAT == null)

password = ("TOKEN")

else

password = PAT

}

}

}

publications {

//This defines a publication called "JohnBase"

// that can be published to a Maven repository

// by virtue of its type: MavenPublication.

JohnBase(MavenPublication) {

// This publication consists of just

// the production JAR artifact and its metadata,

// which combined are represented by the

// java component of the project.

// from

groupId = '6'

artifactId = 'john-base'

version = '1.0.1'

artifact("$buildDir/outputs/aar/")

}

}

}

3. 测试发布程序

因为是本地发布,所以需要本地已经有之前的 aar 文件生成,参考第一点可以完成aar打包

确保aar路径正确,文件名正确

运行

./gradlew :johnbase:publish

(如果整个项目只有这个模块发布,可以直接运行

./gradlew publish

4. 遇到的部分 ISSUE

将 publish 脚本与安卓本身的独立,想法是模块的

依赖

,但是用 groovy 新的写法会有问题

// apply from ''

plugins{

id 'xxx'

}

// apply from ''

以上写法怎么都不行

最后还是用旧的写法实现的,还要再多看看Gradle文档。

发布时提示

Received status code 403 from server: Forbidden

发布时提示

Received status code 409 from server: Conflict

版本号也就是

version

与已有的package版本号一样,属于重复发布,不被允许。

Type 'hToMavenRepository' property 'me' doesn't have a

configured value.

不能从

gradle

中直接读取

ties

中的值,而且 Kotlin 赋值需要双引号包裹住才行。

另外设置值需要在

buildscript

中进行。

buildscript {

ext{

GitHubName = "oOJohn6Oo"

// Personal Access Token

PAT = null

File localFile = ('ties')

if (()) {

Properties properties = new Properties()

(aInputStream())

PAT = perty('')

}

}

}

5. FEAT GitHub Actions

简单地让人心跳

所有脚本如下name: Package with Gradleon: push: tags:

- "v*"jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up JDK 11 uses: actions/setup-java@v2 with: java-version: '11' distribution: 'adopt' cache: gradle - name: Grant execute permission for gradlew run: chmod +x gradlew# 编译 - name: Build with Gradle run: ./gradlew :johnbase:assembleRelease# 发布 - name: Publish to GitHub run: ./gradlew publish# 设置环境参数 env: USERNAME: ${{ }} TOKEN: ${{ }}