CI/CD for the Flutter Android app with Codemagic

Setting up continuous integration and continuous delivery (CI/CD) for a Flutter Android app with Codemagic can greatly improve the development workflow and help ensure that the app is always in a deployable state. In this blog, we will go over the steps to set up CI/CD for a Flutter Android app using Codemagic.

Prerequisites

Before setting up CI/CD with Codemagic, you need to make sure that you have the following:

  • A Flutter project with a pubspec.yaml file.

  • A GitHub or GitLab repository for the project.

  • An account with Codemagic (you can sign up for a free account at https://codemagic.io/).

Setting up the project on Codemagic

  1. Go to https://codemagic.io/ and log in to your account.

  2. Click on the "Add application" button.

  3. Select the "GitHub" or "GitLab" option, depending on where your repository is hosted.

  4. Follow the prompts to authorize Codemagic to access your repositories.

  5. Select the repository for your Flutter project.

  6. Select project type as "Flutter App (via Workflow Editor)"

  7. Click on the "Finish" button.

  8. On the next screen, you will see the default build configuration for your project in the "Workflow Editor" section.

  9. In "Workflow" section select Default workflow from dropdown.

  10. In "Build for platforms" section select Android.

  11. In the "Build triggers" section, select the build trigger type as Trigger on push or Trigger on pull request update and select the branch on which you want to trigger the build.

  12. In "Dependency caching" section you can tick "Enable dependency caching" to reduce the build time in subsequent builds.

  13. In the "Test" section, you can add any test steps that you want to run as part of your CI/CD pipeline.

  14. In "Build" section you can select Flutter version, Android build format(aab,apk or both), build mode (debug or release) and build arguments (if any)

    for example

    --build-name=1.0.$PROJECT_BUILD_NUMBER --build-number=$PROJECT_BUILD_NUMBER
    

    above argument will automatically increase the version of the app in every build.

  15. In the "Distribution" section, you can add any steps to sign the app or publish your app, such as uploading to the Google Play Store.

Customizing the build configuration

Codemagic provides a lot of customization options for your build configuration. Some of the things you can customize include:

  • The Flutter version to use for the build.

  • The build type (e.g. debug, release).

  • The target platform (e.g. Android, iOS).

  • The signing configuration for the app (if building a release version).

  • The environment variables to set during the build.

To customize your build configuration, click on the "Edit" button next to the relevant section in the build configuration screen.

Android code signing

Android code signing is required if you want to publish your app on play store or your app usage any API like firebase for login etc.

for this first we need to generate a upload_keystore in our local machine and then upload it to the codemagic along with Keystore password,alias, and key password.

for steps to generate upload key and configuring the build.gradle follow following links.

a. https://docs.flutter.dev/deployment/android#signing-the-app

b. https://docs.codemagic.io/flutter-code-signing/android-code-signing/

Google play publish

  1. login to google play console and create your new app using "create app button".

  2. After this upload your app signed .aab i.e app bundle signed with your upload-keystore first time manually.

  3. After that follow the instructions given in this codemagic blog to set up play console api access to codemagic.

    https://docs.codemagic.io/flutter-publishing/publishing-to-google-play/

Note : After app is uploaded to play store, it is signed by deployment key. You need to add SHA-1 and SHA-256 of this deployment key to firebase.

You can find these keys inside play console in

Setup -> App integrity -> App Signing

get the keys from here and add it in your firebase project fingerprint.

Conclusion

Setting up CI/CD for a Flutter Android app with Codemagic is a straightforward process that can greatly improve the development workflow. With Codemagic, you can set up automatic builds and tests, and even publish your app to the Google Play Store, all with just a few clicks. Give it a try and see how it can help streamline your development process.

References

  1. https://stackoverflow.com/questions/48687249/firebase-phone-authentication-is-not-working-after-upload-app-on-playstore

  2. https://support.google.com/googleplay/android-developer/answer/9842756?hl=en&visit_id=638077580436876380-4138504550&rd=1

  3. https://docs.codemagic.io/knowledge-codemagic/build-versioning/

  4. https://blog.codemagic.io/build-versioning-with-codemagic/

  5. https://stackoverflow.com/questions/51845559/generate-sha-1-for-flutter-react-native-android-native-app

0
Subscribe to my newsletter

Read articles from Sandeep Kumar Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sandeep Kumar Jha
Sandeep Kumar Jha