实践Android Studio中Gradle发布模块aar到

2021-11-04  本文已影响0人  小强开学前

Publish Android library to GitHub Packages

Working with the Gradle registry

具体代码包括GitHub Action脚本可以参考我的项目

目前GitHub Packages不支持像jcenterjitpack那样的匿名下载,具体可参考我这篇文章

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打包


4. 遇到的部分 ISSUE

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 }}

上一篇下一篇

猜你喜欢

热点阅读