Skip to main content

How to set up CI/CD on android flutter app

Step 1: Install fastlane​

cd android
fastlane init

Step 2: Setup firebase project​

Step 3: Create Google Play Store Developer .json file​

  • Official Document here
  1. Go to: https://play.google.com/console/
  2. Click Account Details > Note Developer Account ID
  3. Setup > API Access
  4. Click Create new service account
  5. Follow Google Cloud Platform
    • Click Create Service Account
    • Click Select a role, then find and select Service Account User, and proceed
    • Click Done
    • Then, download JSON Key and place at android/play-store-credentials.json
  6. Return Google Play Store Console, click on Grant Access, click Refresh
  7. Choose permission Admin (all permissions), but you may want to manually select all checkboxes and leave out some of the Releases such as Release to production
  8. Click Invite user to finish
tip

Step 3, you only need to complete once, if you already did it before, just use the old file and put it at android/play-store-credentials.json

Step 4: Test & Change Appfile​

To test credentials

fastlane run validate_play_store_json_key json_key:./play-store-credentials.json

Update the first line of Appfile to:

json_key_file("./play-store-credentials.json")

Step 5: Setup Firebase App Distribute​

fastlane add_plugin firebase_app_distribution
fastlane add_plugin load_json

Then, add android/.bundle/config

---
BUNDLE_PATH: "vendor/bundle"

Then, add android/.ruby-version

3.1.2

Use this Gemfile.lock

Step 6: Update Fastfile​

default_platform(:android)

platform :android do
before_all do
ENV["FIREBASE_LOGIN_CREDENTIALS"] = "fastlane/firebase_login_credentials.json"
ENV["FIREBASE_APP_ID"] = "<input app id here>"
end

desc "Distribute new debug application to firebase app distribution"
lane :distribute_debug do |options|
sh("cd ../../ && flutter build apk --debug")
release_note_information = " BRANCH => #{git_branch} - developer: #{last_git_commit[:author]}"
release_note = "#{release_note_information} \n #{options[:r]}"
firebase_app_distribution(
app: ENV["FIREBASE_APP_ID"],
apk_path: "../build/app/outputs/apk/debug/app-debug.apk",
release_notes: "#{release_note}",
groups_file: "fastlane/groups.txt",
service_credentials_file: ENV["FIREBASE_LOGIN_CREDENTIALS"],
)
end
end

Step 7: Create fastlane/groups.txt​

echo "oryza-testers" >> fastlane/groups.txt

Step 8: Find App ID and create Firebase Login Credentials​

Find App ID and add it to Fastfile

Create firebase login credentials:

  1. Login to firebase, select your project
  2. Release & Monitor > App Distribution
  3. Click Get started
  4. Go to Project settings > Service Account
  5. Go to Credentials > Create new service account
  6. Add role Firebase App Distribution Admin
  7. Download .json file and place at android/fastlane/firebase_login_credentials.json

Final step: add CircleCI​

  1. Add file .circleci/config.yml
version: 2.1

orbs:
android: circleci/android@2.1.2
flutter-orb: circleci/flutter@1.1.0
ruby: circleci/ruby@1.8.0

jobs:
distribute_android:
executor:
name: android/android-docker
resource-class: large
tag: 2022.03.1
steps:
- run:
name: Add ssh host
command: ssh -oStrictHostKeyChecking=no -p 2222 root@repo.oryza.vn uptime
- checkout
- flutter-orb/install_sdk_and_pub:
cache-version: docker
flutter_version: 3.10.5
- flutter-orb/install_android_gradle
- ruby/install-deps:
app-dir: android
key: android
- run:
command: bundle exec fastlane distribute_debug
working_directory: android

workflows:
test_and_distribute:
jobs:
- distribute_android:
context:
- mobile
  1. Setup project on Circle CI
  2. Add private ssh key to project