Sub topic 1
Debug, profile, and release: what changes in a release build
Every Flutter app can be built in three modes: debug, profile, and release. The mode decides how your Dart code is compiled, which tools can attach to the app, and how fast the app runs. Most release-day surprises come from a mode difference you did not notice during development. This chapter covers what each mode does, how to pick one, and which behaviors change when you ship.
Prerequisites
This chapter assumes you can:
- Write and run a basic Flutter app with the
fluttercommand-line tool. - Use a terminal and read build output.
- Run an app on an emulator, simulator, or physical device.
You don't need Gradle or Xcode experience yet. Later chapters introduce those tools.
The three build modes
A build mode is a set of compiler and runtime choices applied to the whole app. Flutter ships with three:
- Debug is for day-to-day development. It favors fast edits and rich tooling over speed.
- Profile is for measuring performance. It behaves like release but keeps a few hooks for profiling tools.
- Release is for users. It favors small size, fast startup, and smooth rendering.
The same Dart source is compiled differently in each mode.
Debug mode
In debug mode, Dart runs on the Dart virtual machine (VM) with a just-in-time (JIT) compiler. JIT compilation turns Dart into machine code while the app runs. This is what makes hot reload possible: the VM can accept new code into a running app and keep its state.
Debug mode also turns on development checks:
assertstatements run. In release builds they are removed.- The framework checks widget constraints and prints detailed layout errors, such as the yellow-and-black overflow stripes.
- The Dart VM service is open, so the debugger and DevTools can attach and set breakpoints.
- Debug banners and other debug overlays can appear.
These conveniences cost speed. Debug apps are larger and often visibly slower, and animations can stutter on a real device. Never judge performance from a debug build.
Profile mode
Profile mode compiles with the ahead-of-time (AOT) compiler, like release. AOT compilation converts Dart to native machine code before the app is installed, so nothing is compiled at runtime. Profile mode keeps enough tracing support for DevTools to record performance data, such as frame timings and CPU samples.
Profile mode does not support emulators or simulators. Their performance does not reflect a real device, so Flutter requires a physical device. Run it with:
flutter run --profileUse profile mode when you hunt for jank (dropped frames) or measure startup time. The numbers will be close to what users see.
Release mode
Release mode is AOT-compiled and optimized for the end user. The differences from debug are large:
- Assertions are stripped out.
- Debugging information and service extensions are disabled, so you cannot attach the debugger or use hot reload.
- Code is optimized and unused code is removed through tree shaking, which drops classes, functions, and icon glyphs that your app never uses.
- Startup is fast and the package is much smaller than a debug build.
Run it on a device with:
flutter run --releaseBuild outputs are made with flutter build, which uses release mode by default.
Comparing the modes
| Feature | Debug | Profile | Release |
|---|---|---|---|
| Compilation | JIT | AOT | AOT |
| Hot reload | Yes | No | No |
| `assert` statements run | Yes | No | No |
| Debugger attach | Yes | Limited (profiling tools only) | No |
| DevTools performance tools | Slow, not representative | Yes | No |
| Emulator or simulator | Yes | No | Yes |
| Typical use | Daily coding | Performance work | Shipping to users |
Release builds do run on emulators and simulators on Android, and on the iOS simulator you can use flutter run --release only for certain targets. Treat a real device as the authoritative test either way.
How the mode flows through the toolchain
The flutter command picks the mode, and the native platform build follows it. The next diagram shows the paths.
Two defaults are worth remembering:
flutter runuses debug unless you pass--profileor--release.flutter builduses release unless you pass--debugor--profile.
Detecting the mode in code
Sometimes you want code to behave differently by mode, for example to print extra logs only while developing. The flutter/foundation.dart library exposes constants for this:
import 'package:flutter/foundation.dart';
void logMode() {
if (kReleaseMode) {
print('Running in release mode');
} else if (kProfileMode) {
print('Running in profile mode');
} else {
print('Running in debug mode');
}
}
void main() {
logMode();
assert(() {
print('Assertions are enabled, so this is debug mode.');
return true;
}());
}kReleaseMode, kProfileMode, and kDebugMode are compile-time constants. Because the compiler knows their values, it removes the branches that cannot run. Code guarded by if (kDebugMode) does not end up in the release app.
What changes in a release build
Moving to release is more than a compiler switch. These differences cause most surprises.
Things that stop working or behave differently
- Assertions disappear. Code that relied on an
assertfor a side effect, or a check you expected to protect users, no longer runs. Put real validation in normal code. - Debug-only error screens go away. In debug mode a failing widget build shows a red error screen. In release a failing widget build shows a gray box instead, and layout problems that were loud in debug can stay silent.
- Performance changes. Jank you saw in debug may vanish. Equally, a problem that debug hid, such as a slow shader compile on first use, may show up only in release or profile.
- Platform permissions matter. On Android, the
INTERNETpermission is added to the debug and profile manifests automatically, so the app can talk to the Flutter tools. The main manifest does not include it. A release app that makes network calls but never declaredINTERNETinandroid/app/src/main/AndroidManifest.xmlwill fail to reach the network, even though debug worked. - Signing is required. Debug builds are signed with an automatically generated debug key. A release build must be signed with your own key before a store accepts it. Later chapters cover this.
- Shrinking can break reflection-based code. Android release builds can remove code the shrinker thinks is unused. Libraries that load classes by name may need extra rules.
The missing INTERNET permission
This is the most common release-only bug on Android, so it is worth seeing exactly. Add the permission as a child of the manifest element in the main manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="my_app"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- activity and other entries stay as generated -->
</application>
</manifest>Add it once to android/app/src/main/AndroidManifest.xml, and it applies to every mode.
Choosing a mode for a task
Match the mode to the question you are asking:
- Does the feature work and look right? Debug. Use hot reload to iterate.
- Is it smooth and fast? Profile, on a real device, with DevTools.
- Will users see the same thing? Release, on a real device, with the exact build you plan to ship.
A common mistake is to skip step 3 and ship a build that was only ever run in debug. Release-only issues then reach users first.
Edge cases and common errors
- Web and desktop differ. Flutter's compilation targets vary by platform, so the details of JIT and AOT differ outside mobile. The mode names and the intent stay the same.
- Profile on an emulator fails. If you see an error saying profile mode is not supported, connect a physical device.
flutter buildis notflutter run.flutter buildproduces an artifact and exits. It does not install or launch the app. Useflutter run --releaseto install and start a release build on a connected device.- Plugins can behave differently. Native plugins sometimes hold debug-only code paths. Test every plugin you depend on in release.
- Package size is not the same as download size. A debug build is much larger than a release one, so compare size using release artifacts only.
- Do not benchmark with
kDebugModebranches in the hot path. Though these constants are removed in release, a profile build also removes debug branches, so check you are measuring the same code you ship.
Putting it together
A sensible routine before each release looks like this:
- Develop in debug with hot reload.
- Profile performance in profile mode on a real device.
- Install
flutter run --releaseon a real device and exercise the main flows, including every network call and every plugin. - Only then build the signed artifacts that later chapters describe.
Exercise
Your app loads data from a server. It works in debug, but in a release build every request fails. Which Android setting do you check first, and why does debug differ?
Show answer
Check that the main AndroidManifest.xml at android/app/src/main/ declares the INTERNET permission. Flutter adds that permission automatically to the debug and profile manifests so the tools can connect, but the main manifest does not include it, so a release build has no network access until you declare it.
Exercise
A teammate measures scroll performance with flutter run on an emulator and reports that the list is janky. Why is this measurement unreliable, and what should they do instead?
Show answer
flutter run uses debug mode, which is JIT-compiled and has extra checks, and an emulator does not reflect real hardware. They should run flutter run --profile on a physical device and use DevTools to record the frame timings.
Exercise
A function contains assert(user != null); followed by code that uses user.name. What happens in a release build if user is null, and what is the fix?
Show answer
The assert is removed in release, so the null value is not caught there and the code that uses user.name fails or misbehaves at runtime. The fix is to validate with normal code, such as an if check that handles the null case, instead of relying on an assert.