Reducing App Size in Flutter

Tutorial 3 of 5

Reducing App Size in Flutter

1. Introduction

Goal

This tutorial aims to guide you through the process of reducing the size of your Flutter application. By doing so, you'll be able to minimize the impact of your app on device storage, thereby facilitating quicker downloads and enhancing user experience.

Learning Outcomes

By the end of this tutorial, you will understand:
- The importance of reducing app size
- Techniques to minimize the size of your Flutter app
- Practical application of these techniques

Prerequisites

A basic understanding of Flutter and Dart programming language is required.

2. Step-by-Step Guide

Understanding the concept

Flutter apps can be large in size due to various factors such as assets, libraries, and Dart's AOT (Ahead-of-Time) compilation. To reduce the app size, we can apply several techniques like using SVGs instead of raster images, minifying and obfuscating Dart code, and removing unused code and libraries.

Best Practices and Tips

  • Use vector graphics where possible: Vector graphics are usually smaller in size than raster images.
  • Minify and obfuscate Dart code: This step reduces the size of the executable and protects your intellectual property.
  • Remove unused code and libraries: Every bit of code adds to the size of the final app. If you're not using a package or some code, remove it.
  • Set shrinkResources and minifyEnabled to true: These settings in the build.gradle file help to remove unused resources and minify the code.

3. Code Examples

Example 1: Using SVGs instead of raster images

import 'package:flutter_svg/flutter_svg.dart';

// ...

SvgPicture.asset(
  'assets/images/flutter_logo.svg',
  semanticsLabel: 'Flutter Logo'
)

In this code snippet, we're using the flutter_svg package to display an SVG image. This image is usually smaller than a PNG or JPEG image.

Example 2: Minifying and obfuscating Dart code

To minify and obfuscate the Dart code, you need to add the following arguments to the flutter build command:

flutter build apk --obfuscate --split-debug-info=./debug_info

This command tells Flutter to obfuscate the Dart code and to separate the debug information into a directory named debug_info.

Example 3: Removing unused code and libraries

Just remove the unused imports from your Dart files:

// Bad
import 'package:unused_package/unused_package.dart';

// Good
// ... no import statement for the unused package

4. Summary

In this tutorial, we have learned the importance of reducing the app size and various techniques to achieve it, such as using SVGs, minifying and obfuscating Dart code, and removing unused code and libraries.

To continue your learning, you can explore advanced topics like tree shaking and using the --split-per-abi flag.

5. Practice Exercises

Exercise 1:

Try to convert a PNG or JPEG image in your Flutter app to SVG and use it.

Exercise 2:

Create a simple Flutter app, build it, and note down the size. Now, try to apply the techniques discussed in this tutorial and compare the size.

Exercise 3:

Use the --analyze-size flag while building your app and analyze the output.

Don't forget to experiment and practice these techniques in your Flutter projects. Happy coding!