Promptless AI is here soon - Production-ready contextual code. Don't just take our word for it. Know more
Know More
Education

Deploy it Right: Mastering Flutter Release on Android, iOS and Web

No items found.
logo

Nidhi Sorathiya

Engineering
August 8, 2023
image
Author
logo

Nidhi Sorathiya

{
August 8, 2023
}

Developing an app and pushing it to the production environment is an accomplishment for every developer. It is more than just writing the code; it involves building and packaging the app for different platforms. Today, we'll be discussing how to build and release a Flutter app for Android, iOS, and the Web, also known as Flutter release.

Flutter is a modern, reactive framework developed by Google that makes it easier to build native apps for Android, iOS, and Web from a single codebase. It offers a rich set of features and an extensive library of widgets that make app development quick, efficient, and enjoyable.

The build and release process involves preparing your app for release and setting it up on the respective app stores. This process often differs between Android and iOS due to the differences in platform specifications. However, Flutter allows you to streamline this process through the use of a single command known as Flutter build release. This command compiles your Flutter app code into a native app specifically designed for release on the target platform.

The focus of this blog post will be to guide you through this process. Therefore, if you've just finished developing your first Flutter app and want to learn how to prepare it for release, or if you're a Flutter developer looking to make your app available to a wider audience, then continue reading.

Getting Started with Flutter

Before diving into the build and release process, let's cover some basic groundwork on Flutter.

Deploy it Right: Mastering Flutter Release on Android, iOS and Web

What is Flutter?

Flutter is a robust and flexible framework developed by Google, allowing developers to write once and run anywhere. It focuses heavily on providing a native interface for both iOS and Android platforms from a single code base. Furthermore, it also extends its support to web app development, making it a universal framework for crafting elegant UI interfaces.

Why Use Flutter for App Development?

The answer lies in Flutter's exclusive benefits:

Single Codebase

Flutter allows developers to use the same codebase for developing applications for Android, iOS, and Web. This ability significantly reduces development time and effort.

Hot Reload

An essential feature of Flutter, hot reload, allows developers to see changes in the output immediately after tweaking the code.

Efficient Development

Flutter’s rich set of customizable widgets helps in creating complex UIs more efficiently and effectively.

Setting Up the Flutter Environment

Setting up Flutter involves downloading and installing the Flutter SDK, and setting up an editor, typically Visual Studio Code or Android Studio, to write your applications. Installation instructions can be found on the Flutter official website.

To check if Flutter has been correctly set up on your machine, you can run the below code snippet in the terminal or command prompt.

This command checks your environment and displays a report to the terminal window.

Creating a Flutter App

In this section, we will be creating a simple Flutter app that greets users with a welcome message. The app structure is simple - a title bar and a body with a message. We shall go through each step in detail.

Understanding Flutter’s Project Structure

First, let's understand the basics of a Flutter project structure. When you create a new Flutter project, it generates a directory structure where all the necessary code, images, fonts, and configuration files are stored. "lib/main.dart" is the entry point of the Flutter application.

Navigating Flutter’s Widgets

Widgets are the basic building blocks of a Flutter UI. They describe what their view should look like given their current configuration and state. Everything in the Flutter UI is part of a widget or widget tree.

For this example, we will import the Flutter package and start creating our main function.

Building Your First Flutter App

Now, we will define our app. Following is the code of a simple app that displays a welcome message. The app uses the MaterialApp widget that provides a number of widgets required for implementing Material design.

This app creates a Material app with a scaffold that includes an app bar and body with a simple text message in the center of the screen.

Running the main.dart file will result in the display of the created application.

Flutter Build Release: An Overview

As your Flutter app evolves and you deem it ready for the market, there comes the point where your app needs to be built for release. Building a release version of an app differs from debug mode. In contrast to debug mode, the release mode enables optimizations and obfuscation to deliver an efficient and smooth user experience.

Deploy it Right: Mastering Flutter Release on Android, iOS and Web

What the Flutter Build Release Command Does

The flutter build command compiles your app and prepares it for distribution in your chosen platform's app store. The release mode uses Dart's compiler to increase the app's performance by reducing the size of the package and ensuring fast startup.

To elaborate more on this, let's consider a few essential Flutter build release commands:

Flutter build release apk

To build a release version of your app for Android, the command flutter build apk --release is used. This command will generate a release APK at "projectname/build/app/outputs/flutter-apk/app-release.apk".

Flutter build web release

To prepare your app for a release on the web, use flutter build web. The command builds your application for the web and places the output in the "build/web" directory of your project.

Flutter Android release

For releasing the Android app, after it is built using flutter build apk --release, the generated APK file can be submitted to the Google Play Store using the Play Console.

Understanding the Difference Between Debug and Release Mode

In Flutter, you can build your app in either debug or release mode.

Debug Mode

The debug mode is for development. It includes assertions to catch and report errors and provides debugging information to the developers.

Release Mode

The release mode is intended for distribution to end-users and is built with optimizations to guarantee the smoothest and most efficient user experience.

Preparing an Android App for Release

Getting a Flutter app ready for release on Android involves a series of steps to ensure that it is prepared to meet user's and Google Play's requirements.

Deploy it Right: Mastering Flutter Release on Android, iOS and Web

Overview of Android App Bundle

Google Play prefers to have the release version of an Android app deployed as an Android App Bundle, instead of an APK. An Android App Bundle is a publishing format containing all the app's compiled codes and resources, yet defer APK generation and signing to Google Play Store. This reduces app size and enables the new dynamic delivery models.

To build an Android App Bundle, run flutter build appbundle from the app's top directory.

Creating a Keystore

To make your Flutter Android app ready for release, you need to digitally sign it with a keystore. This keystore is a binary file serving as a cryptographic proof that verifies authorship of the app. The keystore is mandatory for Google Play Store releases.

In your Android development environment, create a new key using the following command:

Enter the necessary information, and this command will create a new file named my-release-key.jks. Store this file in a safe place separate from your project directory.

Referencing the Keystore in Your App

After creating the keystore, reference it in your app. In the android/ directory, create a file named key.properties that contains a reference to the keystore using the following format:

Remember, replace the placeholders <password from previous step> with your actual passwords, and /path/to/my-release-key.jks with the actual path where your key is stored. This file should NOT be check-in into your source version control.

Configuring the App Manifest

Your app's manifest file, AndroidManifest.xml, contains essential information about your app needed by the device's system to run it correctly. Here, you may wish to update the application's android:label to reflect the app's final name or add any permissions your application needs, like the android.permission.INTERNET permission if your app requires internet access.

Building the App for Release

Building your Android application for release includes compiling your app into a deployable unit, either an APK file or an app bundle, that you can distribute to users.

To build an APK, run the following command in your terminal from your application's root:

To construct an app bundle, use:

After building, the APK or app bundle should reside in the 'projectname/build/app/outputs/flutter-apk' or 'projectname/build/app/outputs/bundle/release' directory, respectively.

Releasing the Android App

Once the preparation process for the release of the Android version of your Flutter app is finished, the next step is to distribute your application to the users on the Google Play Store.

Understanding Android App Distribution Process

Distributing your app to users is an exciting stage in the development process where your app becomes available to the public. The app can be distributed via several channels, but the most common one is the Google Play Store.

Distributing your app through the Play Store is an effective way to reach your target audience, as this platform allows potential users to find, download, and update your app easily.

Uploading to the Google Play Store

This is the final step in which you submit your application to the Google Play Store for users to download.

Before uploading, ensure you update your app's version details. In your pubspec.yaml file, update the version property to version: 1.0.0+1 (or update the version to the relevant number following semantic versioning practices).

After building, the app bundle or APK that you can upload to Google Play Store is generated at 'projectname/build/app/outputs/bundler/release/app-release.aab' or 'projectname/build/app/outputs/flutter-apk/app-release.apk' respectively.

Make sure the Google Play Console is set up for the app. Then, navigate to the "Release" section, upload the generated file, and follow the instructions to publish the new release.

Preparing an iOS App for Release

Releasing an iOS application involves a sequence of steps to ensure that it aligns with Apple's App Review Guidelines. This process involves preparing your app for release, registering the app, and, finally, distributing it through App Store Connect.

Deploy it Right: Mastering Flutter Release on Android, iOS and Web

Understanding the iOS Archive Process

Before you prepare your application for release, you need to have Xcode installed on your machine. Within Xcode, you will have to register your iOS app and its unique identifier (called a Bundle ID), create an archive of the app, and then upload this archive to App Store Connect.

The iOS archive (.xcarchive) is a package that bundles your app's executable (along with any associated libraries) and any assets required to run the app, including the app icon and launch image.

Reviewing Xcode Project Settings

Next, review your Xcode project settings. Particularly, verify the most important settings:

  1. Display name: The display name of your application.
  2. Bundle Identifier: The bundle identifier you registered on the Apple Developer Site.
  3. Version: The user-facing version number string displayed to users (for example, 1.0.0).
  4. Deployment Target: The minimum iOS version that your app supports.

Creating an App Store Connect Record

An App Store Connect record contains metadata about your app and links to your app's binary files. You should create the app record within App Store Connect. It's important to ensure that the bundle identifier in your Xcode project matches the one in this record.

Archiving the App

Archiving your app compiles it and prepares it for distribution in the Apple App Store. You can create a build archive by running flutter build ipa within your Flutter app's root directory. This command will produce an .xcarchive file in the build/ios/archive directory.

Releasing the iOS App

To release your app and distribute it to users, follow these steps:

Validate and upload the app to App Store Connect

  1. Open your app's .xcarchive in Xcode's Organizer window.
  2. Validate the app. After validation, an app summary is shown.
  3. Upload the app to App Store Connect.

Submit for iOS App Review

Upon successful validation, submit your app for the iOS App Review. To do this, go to your app's status page in App Store Connect and add any necessary app information, screenshots, app previews and version information. Then, click "Submit for Review".

Release the iOS App

After your app has been reviewed, you will receive a notification from Apple. If your app passes the review, you'll be able to release it whenever you're ready. If any issues arise from the review, you'll need to address them and submit your app for review again.

Preparing a Web App for Release

After building your Flutter web application, you'll want to prepare it for release on the web. This section will guide you through this process.

Deploy it Right: Mastering Flutter Release on Android, iOS and Web

Compiling Your Flutter Web App for Release

To compile your Flutter web app for release, run the 'flutter build web' command in your app's top directory. By default, this command creates a build using the auto renderer option, which deploys the HTML renderer for mobile browsers and the CanvasKit renderer for desktop browsers. However, if you wish to specify a renderer, modify your command like 'flutter build web --web-renderer html' or 'flutter build web --web-renderer canvaskit'.

Deploying Your Flutter Web App

The deployment process is as simple as uploading the output of your build (build/web) to your web server. However, several services can help to simplify this process:

  • Firebase: Using the Firebase CLI, you can automatically build and deploy your web app. Firebase also offers features like analytics, databases, and messaging.
  • GitHub Pages: GitHub Pages is a static hosting service that takes files straight from a repository on GitHub and deploys them as a website.
  • Google Cloud: Google Cloud is another web service that allows you to deploy, scale, and monitor your web app.

Deploying to Firebase Hosting

Firebase provides tools to automatically build and deploy your Flutter web app. By installing and initializing Firebase in your app's directory, you can utilize the firebase init hosting and firebase deploy commands to upload your assets to Firebase Hosting.

Choosing a Web Renderer

By default, Flutter's auto renderer option is applied. This uses the HTML renderer for mobile browsers and the CanvasKit renderer for desktop browsers. If you want to specify a renderer, you can do it with the 'flutter build web' command by passing '--web-renderer html' or '--web-renderer canvaskit'.

Useful Tips for a Smooth Flutter Build Release

In this section, we will be offering some valuable tips and suggestions that could help to ensure a smoother and more efficient Flutter build release process.

Common Issues and How to Tackle Them

When dealing with software development, issues and bugs are inevitable parts. Here are a few common issues that developers might face during the build release:

Build fails

Typically, this is due to some issues with the project dependencies. Check if all dependencies are compatible with each other and the current Flutter SDK version.

App crashes on startup

Check your main() function and debug your issues carefully. Sometimes unexpected null values or not handling exceptions properly can cause the app to crash.

Differences in Debug and Release Behavior

Sometimes, an app behaves differently in debug and release modes. This could be due to factors like different operating conditions or optimizations made during release builds.

Always have proper error handling and logging mechanisms in your app. Make sure to test your app thoroughly in all modes.

User-friendly Error Messages

While debugging issues, good, user-friendly error messages can be very helpful. Ensure that your app provides helpful error messages during failures, such as network requests exceptions or database exceptions. This not only enhances the user experience but also eases the debugging process when an issue arises.

Incorporating Feedback into Your App

Always be open to feedback from your app users. Users are the best source for identifying real-world issues in your app. Responses and reviews from users can help identify blind spots in the development and improve future versions of the app. Actively fix those issues and roll out updates regularly.

The Power of Flutter in App Development

In conclusion, Flutter is a robust framework that answers the crucial necessity of modern application development – crafting efficient and aesthetically pleasing applications targeting multiple platforms from a single code base. With its multitude of features and a growing community, Flutter has made application development enjoyable and efficient. This guide offered an overview of how to build and release your Flutter application on multiple platforms, including Android, iOS, and Web.

You can also visit official Flutter docs for detailed information. Keep deploying powerful Flutter apps! 💙

Frequently asked questions

Q: When was Flutter first released?

Flutter was first released by Google in May 2017 at the Google I/O conference.

Q: How do I create a release version in Flutter?

The process varies based on the platform. For Android, use the 'flutter build apk --release' command. For iOS, first complete the preliminary requirements, then use 'flutter build ios'. For a web application, employ 'flutter build web'.

Q: What is the release name of flutter build apk?

After executing the 'flutter build apk --release' command, an APK is generated, typically named 'app-release.apk', in the 'projectname/build/app/outputs/flutter-apk' directory.

Q: What is the difference between flutter APK debug and release?

Debug and release versions of an APK mostly differ in optimizations. A debug APK is suitable for testing and debugging your application and comes with extra logging and debugging symbols. On the counterpart, a release APK is meant for distribution to end users—it lacks those extra debugging information, is optimized for performance, and is usually smaller in size.

Q: Is Flutter good for building websites?

Yes, Flutter can be used for creating highly interactive, graphically rich websites. Flutter's widget-based design approach and the availability of a fast, standards-based web runtime (running either on the HTML or CanvasKit renderer) makes it a suitable framework for web development.

Q: Is Flutter better for Android?

Flutter was developed by Google, so it provides excellent support and integration for Android. Additionally, it is also equally effective for developing iOS applications. Its "write once, run anywhere" philosophy helps developers build apps for both Android and iOS from a single codebase, saving development time and effort.

Frequently asked questions

No items found.