Flutter + Bitrise are a perfect pair. Today weâre going to go through the process of adding a Flutter app to Bitrise, discuss the default Flutter workflow, and set up code signing for our app.
Highlights
- Create a new Flutter app
- Create a GitHub repository
- Commit your sample app to the Github repository
- Add your app to Bitrise
- Your first Bitrise Workflow
- Android code signing
- iOS code signing
If you want to follow along with the tutorial make sure that Flutter is installed and setup on your computer. Youâll also need git installed on your local machine and a GitHub account. Youâll also need an Apple developer account and a Mac with Xcode installed to follow along with the iOS code signing section.
Once running, the flutter doctor gives you all green checkmarks (like the image below) youâre ready to proceed.
Part 1: Create a new Flutter app
From the command line, type in the following:
flutter create FlutterBitriseExample
This will build a default Flutter âHello Worldâ app, complete with unit tests. This is perfect for the purposes of this tutorial. A new directory should be created called FlutterBitriseExample. Letâs enter it by typing the following into the command prompt:
cd FlutterBitriseExample
If youâre new to Flutter, there are numerous resources available to help you get started with creating widgets and building out your app. For now, letâs just take a look at the âHello Worldâ app we just created.
Make sure you have either the iOS Simulator or Android Emulator open. Enter the following at the command prompt:
flutter run
Youâre presented with a simple app that counts how many times you push the button. Truly riveting stuff.
Click inside your command line window and press the Q key to close the application. Now itâs time to get our sample app into GitHub.
Part 2: Create a GitHub repository
Bitrise supports Bitbucket, GitLab, and GitHub. Since GitHub is by far the most popular weâre going to use it for this tutorial.
In the âInitialize this repository with:â section leave all the checkboxes blank. Your screen should look something like this:
Click the Create Repository button and continue to the next step. Copy the URL of your repository from your browserâs address bar.
Part 3: Commit your sample app to the Github repository
Inside your command line window type the following command:
git init
This will initialize git for your sample application. Next, letâs add the files to source control by using the following commands:
git add .
git commit -m "Initial Commit"
Next, we need to connect the Github repository to our local repository.
git remote add origin [GitHubRepoURL].git
After that, weâre going to rename the Master branch to Main in order to match GitHub convention.
git branch -M main
Finally, letâs push our sample app up to GitHub.
git push -u origin main
At this point, weâve got the repository connected. Letâs add our sample app to Bitrise!
Part 4: Add your app to Bitrise
- Click the big purple Add New App button. If this is the first time you have logged in to Bitrise, youâll automatically be taken through the first steps to build your app.
- From here youâll be asked to set the privacy of your app â weâre going to choose Private for the time being.
- If this is your first time using Bitrise, youâll be asked to connect to a source control provider. Since we created our repository on Github, weâre going to select that and hit the Connect Github button.
- Once youâve entered your GitHub credentials, youâll see a list of your repositories hosted on Github. Go ahead and choose the FlutterBitriseExample repository we created earlier.
- Next, youâll be asked if you need to connect an additional private repository. In our case we do not, so weâll select No, auto-add SSH key.
- After that, youâll be asked to choose a branch. Weâll enter main since thatâs the branch we set up on Github earlier.
- At this point, Bitrise will do some repository validation. This will take a few minutes, so go grab a cup of coffee while waiting. âď¸
- Next, youâll be asked if you want to run tests found in the project. Our âHello Worldâ project includes unit tests, so weâll say yes here.
- Weâre almost done! Since Flutter can build for both iOS and Android we need to select our ipa export method. This is used by Apple to determine when and where our app can be run. In our case, weâre going to select app-store.
- Next, youâll be asked to confirm your projectâs build configuration. Everything looks correct here so weâll hit confirm.
- At this point, youâll be asked to add an App icon. We donât need this at the moment, so weâll click Skip for Now.
- Finally, we have the option to register a webhook. This allows us to trigger a new build every time code is pushed to the repository. Donât worry, you can always define custom build triggers later. For now, weâll hit Register a Webhook for me!
Congratulations! đž Youâve added your sample app to Bitrise! A build should have been kicked off already. Letâs take a look at the Workflow Bitrise has set up for us.
Part 5: Your first Bitrise Workflow
Assuming youâve followed the guide so far, your first Bitrise build should be green. Hooray! đ
Bitrise allows you to define your build process using Workflows. Each workflow contains a series of steps. You can find a list of the steps available to you on the integrations page.
Letâs take a look at the Workflow this build used. Start by accessing your app by clicking FlutterBitriseExample in the breadcrumbs at the top of the page. This will bring up a list of all your recent builds.
Click on the Workflow tab to bring up your Workflows.
By default, Bitrise creates two Workflows for you. Primary and Deploy. The primary Workflow is designed for general building and testing. The deploy workflow is for sharing your app with the world!
For now, letâs look at the primary workflow.
Letâs look at each step and check out what it does.
- Activate SSH key - This sets up the SSH key used for the current workflow. Specifically, this usually is used with your source control provider to clone your repository.
- Git Clone Repository - When this step runs it clones the files in your repository to the virtual machine that Bitrise created when your build was initiated.
- Do anything with a Script step - We put this step here just as an example. By default all it does is print âHello Worldâ but it also gives you incredible flexibility. If Bitrise doesnât support something out of the box, thereâs a good chance that you can make it work using this step.
- Flutter Install - When your build starts, Bitrise creates a new virtual machine just for you. When your build ends, that virtual machine is deleted. As a result we have to install any tools that arenât pre-installed every time a build runs. This step make sure that Flutter is installed.
- Bitrise.io Cache:Pull - Since every build begins with a clean virtual machine, we can use caching to speed up our build times. With caching, you can preserve the contents of selected files and directories, including installed dependencies. This step pulls that cached data to make use of it.
- Flutter Analyze - Now that we have Flutter installed we can analyze the project for issues. These issues will be printed out on the buildâs log. This is the same thing as running flutter analyze from the command line.
- Flutter Test - This step runs any unit tests in your Flutter project. Since our Hello World app came with a unit test, this step will execute it.
- Deploy to Bitrise.io - Apps, Logs, Artifacts - Once the build is complete youâll see an âApps & Artifactsâ tab. This step makes your unit test results available there.
- Bitrise.io Cache:Push - This is the companion step to the Bitrise.io Cache:Pull step you saw earlier. Future builds should run faster thanks to this caching.
Each of these steps has numerous fields that allow you to customize how they run. Feel free to open each of them and take a peek at whatâs inside.
Next, open up your Deploy Workflow.
Youâll notice many of the same Steps from our âprimaryâ Workflow, with a few additions.
Letâs look over the new Steps in this Workflow.
- Certificate and profile installer - This step downloads and installs the Apple code signing certificates and provisioning profiles. Weâll make use of this later when we set up iOS codesigning.
- Flutter Build - In our primary Workflow we never actually build the app. In this Workflow, we want to generate Android and iOS binaries so we add the Flutter Build step.
- Xcode Archive & Export for iOS - The Flutter Build step prepares the iOS project to be built, but doesnât actually generate the binary. This step finishes the iOS build.
Part 6: Android code signing
Parts of this section are based on the official Flutter docs for building and releasing on Android. We highly recommend checking that guide out when you get a chance. For now, weâre going to focus solely on whatâs needed to get your app configured on Bitrise.
Create a keystore
We now have to create a keystore file that will be used to sign our app when uploading to Google Play. Use the following command:
Mac / Linux
keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key
Windows
keytool -genkey -v -keystore c:\Users\USER_NAME\key.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias key
In addition to being asked several questions, youâll also be asked for a password. Make sure you donât forget this!
This creates a key.jks file in your home directory. Do not check this file into source control! Doing so is a security risk. Instead, weâre going to upload this file to Bitrise.
- Back in Bitrise, click on the Code Signing tab.
- Drag-and-drop your keystore file to the ANDROID KEYSTORE FILE field.
- Add a password, an alias, and a private key password. (If you copy-pasted one of the commands above to generate your keystore file, your keystore alias is key)
- Click Save metadata.
Start the Deploy Workflow
- At this point, go back to the Workflows tab.
â - Select your Deploy Workflow.
â - Add the Android Sign Step directly below the Flutter Build Step.
â - For now, just leave the default parameters alone. If you followed these steps so far the defaults should work.
- âSave the changes to your Workflow by hitting the save button.
- Go back to your app by clicking FlutterBitriseExample in the breadcrumbs at the top of the page.â
- Click the Start / Schedule a Build button. Weâre going to manually trigger the deploy workflow.
- Leave the branch set as main and choose the deploy workflow.
- Click the Start Build button.
At this point, your build will fail because we havenât done the iOS portion yet. However, your signed Android APK will be generated regardless.
Download signed APK
Once the build has completed, click the Apps & Artifacts tab. Youâll notice that your signed APK is available for download! At this point, youâre ready to submit the app to Google Play. Since this is just a demo app, we wonât do that now.
Your first app submission to Google play always has to be done manually. After that you can use our Deploy to Google Play step to do this automatically. We have instructions for setting up this step in our documentation.
Part 7: iOS code signing
Just like for Android code signing, the official Flutter docs for building and releasing on iOS are excellent. You should review them when you get a chance. For now, weâre going to focus on generating a signed ipa file with Bitrise. Note that youâll need a Mac to follow along with this section.
Register your devices
- Log in to your Apple Developer account and click on Certificates, IDs & Profiles.
- On the left sidebar choose Devices.
- Click the blue â plus sign beside the Devices header.
- âName your devices and enter its UDID to register it.
Set up a bundle ID
- Click on Identifiers in the left-hand column.
- Click the blue â plus sign beside the Identifers heading.
â - On the next screen, select App IDs and click continue.
- When asked to select a type, choose app and then click continue.
- After that, choose add explicit bundle id in reverse domain name order and click continue.
- Finally, click the Register button to register the id.
Specify team
- We now need to specify the team associated with our Apple developer account in Xcode.
- In the FlutterBitriseExample directory, youâll find a folder called ios. Inside of that folder, open Runner.xcworkspace
- Sign in to your Apple Developer account inside of Xcode.
- In the menu, select Xcode and then Preferences.
- Look for the Accounts tab.
- In the lower left-hand corner, youâll see a â plus sign. Click that.
â - Select Apple ID and click Continue.
- At this point, youâll be able to enter your Apple ID and password.
Create a development and distribution certificate
- Still in the Accounts tab, click the team name that should appear now that youâve logged in.
- Now click the Manage Certificates button.
- In the new window that opens, click the â plus sign in the lower-left corner and choose Apple Development.
- Click it one more time and select Apple Distribution.
- In Xcode, select the Runner project in the Xcode project navigator.
- Then, in the main view sidebar, select the Runner target.
- Under the General tab, change the bundle identifier to the explicit bundle ID we selected earlier.
- Choose the Signing and Capabilities tab and select the team associated with your Apple Developer Account.
Push changes to Git repository
These changes must be pushed to your Git repository, otherwise your Bitrise build will fail. Letâs take care of that now. In the terminal window enter the following commands.
git add .
git commit -m âUpdated signing teamâ
git push
The next step is getting all of our code signing files uploaded to Bitrise. I have some good news here! We have a tool that automatically does this work for you.
Get a personal access token
- Back in Bitrise, go to your Account Settings.
- Once youâre there, choose Security from the left sidebar.
- Click the Generate New button.
- Give the token a description and set the expiration. I only plan to use this once, so Iâll set the expiration to 1 hour.
- Hit save and continue.
You will now be presented with a personal access token. Copy it someplace safe. Once you hit the âDoneâ button youâll never be able to retrieve it again!
Build the iOS app using the Flutter tool
Now letâs go back to the terminal. In order for our magical code sign document uploader to work, we first have to build the iOS app using the flutter tool.
In the command line, type:
flutter build ios
Now enter your iOS directory
cd ios
Now weâre going to use the Bitrise codesigndoc to upload all of our codesigning files to Bitrise. Paste the following command into the terminal:
bash -l -c "$(curl -sfL https://raw.githubusercontent.com/bitrise-io/codesigndoc/master/_scripts/install_wrap-xcode.sh)"
- After performing an archive, youâll be asked to select an ipa export method. Choose app-store.
- Youâll then be asked if you want to collect another ipaâs codesigning files. Say no here.
â - Next, you will be asked if you want to upload the provisioning profiles and certificates to Bitrise. Say yes here.
â - Youâll now be asked to paste in your personal access token that we generated earlier.
â - Finally, youâll be asked to select the application you want to upload these signing certificates for. We only have one, so weâll choose 1.
â - The codesigndoc tool will now upload several items to Bitrise. Once weâre done, go back to Bitrise in your browser.ââ
- âGo back to your deploy Workflow. â
- Select the Flutter Build Step.
- Under iOS Platform Configs check the Additonal Parameters field. Make sure it just says --release. If the --no-codesign option is listed, remove it.
- Save your changes to the Workflow.
Launch build
Alright! Itâs time to launch our build! Build the main branch again on the deploy workflow.
This time the build should be successful! On the build page, click on that Apps & Artifacts tab again.
Download ipa file
You should see your signed ipa file ready for download!
At this point you could set up the âDeploy to iTunes Connectâ step to share your app with the world! Check out this document for more information.
Part 8: Conclusion
That was quite a journey! I hope you found this guide useful, and wish you nothing but success with your Flutter project! If you have any questions about Bitrise, please feel free to reach out to our super-friendly Support Team! â¨
Watch the State of Flutter 2020 and Scaling with Flutter webinars
Tune in to our on-demand webinar, and learn about the State of Flutter in 2020, and where itâs likely to go next. Join Matt Carroll, Mariano Zorrilla, Remi Rousselet, Simon Lightfoot, and our host, Matthew Jones for this free virtual roundtable discussion. During this interactive session, we dived into topics ranging from adopting and growing Flutter to best practices, challenges, and the community behind Flutter. Watch it on-demand!
During our second Flutter webinar, Scaling with Flutter, we were joined by Tonal and Signify. Our speakers, Max Lapides and Alexei Sintotski will walk you through the steps of taking your Flutter projects to the next level in regards to matters of scaling and technicalities. Watch the recording here!When I discovered Flutter, I was the sole developer at a non-profit organization. I was asked to create a completely new mobile application for both iOS and Android in just four months. I was successful and it was thanks in large part to just how incredible the Flutter framework is. Iâve been an obsessed fanboy ever since.