实践Android Studio中Gradle发布模块aar到
Publish Android library to GitHub Packages
具体代码包括GitHub Action
脚本可以参考我的项目
目前GitHub Packages
不支持像jcenter
、jitpack
那样的匿名下载,具体可参考我这篇文章
1. 确保本地能打包
这里我用的是release包,体积小,并且被正确混淆。
使用./gradlew :johnbase:assembleRelease
测试成功,生成文件路径为JohnBase/build/outputs/aar/JohnBase-release.aar
2. 添加Publish
脚本
这里为了原本build.gradle
的内容不被干扰,我们将发布相关内容移到publish.gradle
build.gradle
文件内容如下
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply from: 'publish.gradle'
publish.gradle
文件内容如下
apply plugin: 'maven-publish'
buildscript {
ext{
GitHubName = "oOJohn6Oo"
// Personal Access Token
PAT = null
File localFile = rootProject.file('local.properties')
if (localFile.exists()) {
Properties properties = new Properties()
properties.load(localFile.newDataInputStream())
PAT = properties.getProperty('JohnBase.token')
}
}
}
publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/$GitHubName/BaseAndroid")
credentials {
username = "$GitHubName" ?: System.getenv("USERNAME")
if (PAT == null)
password = System.getenv("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 components.java
groupId = 'com.github.john6'
artifactId = 'john-base'
version = '1.0.1'
artifact("$buildDir/outputs/aar/JohnBase-release.aar")
}
}
}
3. 测试发布程序
因为是本地发布,所以需要本地已经有之前的 aar 文件生成,参考第一点可以完成aar打包
- 确保aar路径正确,文件名正确
- 运行
./gradlew :johnbase:publish
(如果整个项目只有这个模块发布,可以直接运行./gradlew publish
)
4. 遇到的部分 ISSUE
-
将 publish 脚本与安卓本身的独立,想法是模块的
build.gradle
依赖publish.gradle
,但是用 groovy 新的写法会有问题// apply from 'publish.gradle' plugins{ id 'xxx' } // apply from 'publish.gradle'
以上写法怎么都不行
最后还是用旧的写法实现的,还要再多看看Gradle文档。 -
发布时提示
Received status code 403 from server: Forbidden
我猜可能是本地终端默认使用的SSH,所以尝试
GitHub Actions
发布,但是还是这样。
最后发现发布的url
不对😂, 这里应该是https://maven.pkg.github.com/$USERNAME/$REPONAME
-
发布时提示
Received status code 409 from server: Conflict
版本号也就是
version
与已有的package版本号一样,属于重复发布,不被允许。 -
Type 'org.gradle.api.publish.maven.tasks.PublishToMavenRepository' property 'credentials.username' doesn't have a configured value.
不能从
gradle
中直接读取local.properties
中的值,而且 Kotlin 赋值需要双引号包裹住才行。
另外设置值需要在buildscript
中进行。buildscript { ext{ GitHubName = "oOJohn6Oo" // Personal Access Token PAT = null File localFile = rootProject.file('local.properties') if (localFile.exists()) { Properties properties = new Properties() properties.load(localFile.newDataInputStream()) PAT = properties.getProperty('JohnBase.token') } } }
5. FEAT GitHub Actions
简单地让人心跳
所有脚本如下
name: Package with Gradle
on:
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: ${{ github.actor }}
TOKEN: ${{ github.token }}