Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andreytreyt/ca13f8241491d7e6927437aa51e47cf4 to your computer and use it in GitHub Desktop.
Save andreytreyt/ca13f8241491d7e6927437aa51e47cf4 to your computer and use it in GitHub Desktop.

Sign and upload Flutter apk-file with Github action

Generate key

To sign your apk-file you need a key. Generate it!

keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

Remember your alias and password. ⚠️ Copy the generated key.jks to <your_project>/android/app. Add jks-files into .gitignore.

echo "android/app/*.jks" >> .gitignore

Configure signing in Gradle

Copy a code below to <your_project>/android/app/build.gradle.

android {
    ...
    
    signingConfigs {
        release {
            storeFile file("key.jks")
            storePassword = "$System.env.STORE_PASSWORD"
            keyAlias = "$System.env.KEY_ALIAS"
            keyPassword = "$System.env.KEY_PASSWORD"
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

Local build

Now build your apk-file locally! Export environment variables and start building.

export STORE_PASSWORD=<password>
export KEY_ALIAS=<alias>
export KEY_PASSWORD=<password>

flutter build apk --release

Create secrets

We don’t have a way of storing files securely in Github. So we’ll use base64 encode of the key and store that in Github secrets. Copy base64 code from the generated key.jks.

openssl base64 -in key.jks

Create a KEY_JKS secret and paste copied base64 code into the Value field. Also create secrets for your alias and password.

Settings > Secrets > New repository secret 1_IpaIo4w38ZlzFnFz0BiKBQ

Create an action

Now create a Github action!

Actions > Skip this and set up a workflow yourself 1_3IbEoFPKRS4OLdGu-B_rOQ

Copy a code below to new yml-file.

name: CI

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      KEY_JKS: ${{ secrets.KEY_JKS }}
      KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
      KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
      STORE_PASSWORD: ${{ secrets.STORE_PASSWORD }}

    steps:
    
    - uses: actions/checkout@v1
    
    - uses: actions/setup-java@v1
      with:
        java-version: 1.8
    
    # if you use ndk
    - run: echo "y" | sudo /usr/local/lib/android/sdk/tools/bin/sdkmanager --install "ndk;21.1.6352462" --sdk_root=${ANDROID_SDK_ROOT}
    
    - uses: subosito/flutter-action@v1
      with:
        channel: 'stable' # or 'dev' or 'beta'
    
    - run: echo $KEY_JKS | base64 -d > android/app/release.jks
    - run: flutter pub get
    - run: flutter test
    - run: flutter build apk --release
    
    - uses: actions/upload-artifact@v2
      with:
        path: build/app/outputs/flutter-apk/app-release.apk

Remote build

Push any commit into your main branch. Enjoy!

Actions > CI workflow 1_oCzUlQ6iujGhcV5ncCwfuw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment