美文网首页
实践Android Studio中Gradle发布模块aar到

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

作者: 小强开学前 | 来源:发表于2021-11-04 11:15 被阅读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打包

    • 确保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 }}
    
    

    相关文章

      网友评论

          本文标题:实践Android Studio中Gradle发布模块aar到

          本文链接:https://www.haomeiwen.com/subject/owohzltx.html