Como construir, assinar e publicar aplicativos Android usando Github Actions

Mar 16 2023
O principal objetivo deste artigo é fornecer uma implementação completa de um Github Workflow capaz de criar, assinar e publicar aplicativos Android com segurança. Siga estas etapas para criar o fluxo de trabalho do Github: 1.
Foto de @dianamia no Unsplash

O principal objetivo deste artigo é fornecer uma implementação completa de um Github Workflow capaz de criar, assinar e publicar aplicativos Android com segurança.

Siga estas etapas para criar o fluxo de trabalho do Github :

1. Gere um novo armazenamento de chaves de assinatura

2. Adicione os seguintes segredos ao seu repositório Github (você precisará converter keystore.jks em string base64 )

Nas configurações do repositório, adicione os seguintes segredos à segurança -> Segredos e variáveis ​​-> Ações -> Segredos do repositório

3. Adicione uma nova configuração de assinatura ao arquivo build.gradle de seu aplicativo . Observe que rootProject.name é definido no arquivo settings.gradle .

android {
    // ...

    defaultConfig {
        // ...
        versionName "1.1"
        // ...
    }

    signingConfigs {
        release {
            storeFile = file("keystore/android_keystore.jks")
            storePassword System.getenv("SIGNING_STORE_PASSWORD")
            keyAlias System.getenv("SIGNING_KEY_ALIAS")
            keyPassword System.getenv("SIGNING_KEY_PASSWORD")
        }
    }

    buildTypes {
        // ...
        release {
            // ...
            applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "${rootProject.name}_${versionName}.apk"
                }
            }
            signingConfig signingConfigs.release
        }
    }
}

name: Android CI

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Application version'
        required: true
        default: '1.0'
jobs:
  build:
    name: Build Signed APK
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'temurin'
          cache: gradle
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      # Here we need to decode keystore.jks from base64 string and place it
      # in the folder specified in the release signing configuration
      - name: Decode Keystore
        id: decode_keystore
        uses: timheuer/[email protected]
        with:
          fileName: 'android_keystore.jks'
          fileDir: '/home/runner/work/<<project name>>/<<project name>>/app/keystore/'
          encodedString: ${{ secrets.KEYSTORE }}

      # Build and sign APK ("-x test" argument is used to skip tests)
      - name: Build APK
        run: ./gradlew :app:assembleRelease -x test
        env:
          SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
          SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
          SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}

      # Show information about the APK's signing certificates
      - name: Verify Signature
        run: $ANDROID_SDK_ROOT/build-tools/33.0.1/apksigner verify --print-certs app/build/outputs/apk/release/<<application name>>_${{ github.event.inputs.version }}.apk

      # Save the APK after the Build job is complete to publish it as a Github release in the next job
      - name: Upload APK
        uses: actions/[email protected]
        with:
          name: <<application name>>
          path: app/build/outputs/apk/release/<<application name>>_${{ github.event.inputs.version }}.apk
  release:
    name: Release APK
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download APK from build
        uses: actions/download-artifact@v1
        with:
          name: <<application name>>
      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ github.event.inputs.version }}
          name: Release v${{ github.event.inputs.version }}
          draft: false
          prerelease: false
          files: <<application name>>/<<application name>>_${{ github.event.inputs.version }}.apk

Fluxo de trabalho de ação do Github

6. Verifique se o APK está assinado corretamente

Etapa de verificação de assinatura

7. Verifique se a versão do Github foi publicada corretamente

Lançamento publicado com APK assinado

O projeto completo também está disponível no meu GitHub .