How to set up CI/CD on android flutter app
Step 1: Install fastlane
- View Installation
- Then
cd android
fastlane init
Step 2: Setup firebase project
Step 3: Create Google Play Store Developer .json file
- Official Document here
- Go to: https://play.google.com/console/
- Click
Account Details> NoteDeveloper Account ID Setup>API Access- Click
Create new service account - Follow
Google Cloud Platform- Click
Create Service Account - Click
Select a role, then find and selectService Account User, and proceed - Click
Done - Then, download
JSON Keyand place atandroid/play-store-credentials.json
- Click
- Return
Google Play Store Console, click onGrant Access, clickRefresh - Choose permission
Admin (all permissions), but you may want to manually select all checkboxes and leave out some of theReleasessuch asRelease to production - Click
Invite userto 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:
- Login to firebase, select your project
Release & Monitor>App Distribution- Click
Get started - Go to
Project settings>Service Account - Go to
Credentials>Create new service account - Add role
Firebase App Distribution Admin - Download
.jsonfile and place atandroid/fastlane/firebase_login_credentials.json
Final step: add CircleCI
- 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
- Setup project on Circle CI
- Add private ssh key to project