Sub topic 14
Debug, profile, and release modes
This chapter assumes you can already run an app with flutter run. It also assumes you know what hot reload does, and that you have read the generated counter app. Here you will learn what a Flutter build mode is, how the three modes differ, and which one to use for each task.
What a build mode is
A build mode is a set of choices the Flutter tool makes when it compiles and launches your app. It controls how Dart code is compiled, which checks stay on, and whether tools can attach to the running app. Flutter has three modes: debug, profile, and release.
When you type flutter run with no flag, you get debug mode. This is why the same app can feel slow while you develop it and fast when you ship it. The app code is identical, but the compiler and runtime settings are different.
The three modes at a glance
| Aspect | Debug | Profile | Release |
|---|---|---|---|
| Command flag | `--debug` (default for `flutter run`) | `--profile` | `--release` |
| Mobile and desktop compilation | JIT (just-in-time) in the Dart VM | AOT (ahead-of-time) to machine code | AOT to machine code, fully optimized |
| Hot reload and hot restart | Yes | No | No |
| Assertions (`assert`) | On | Off | Off |
| Debugger and breakpoints | Yes | No | No |
| Performance tracing and DevTools profiling | Available, but numbers are misleading | Enabled | Disabled |
| Typical use | Everyday development | Measuring performance | Shipping to users |
| Emulators and simulators | Yes | Not on mobile | Not on mobile |
JIT means the Dart VM compiles code while the app runs. That is what makes hot reload possible, because the VM can swap in new code without restarting. AOT means the code is compiled to native machine code before the app starts. AOT code starts faster and runs faster, but it cannot be patched while running.
Debug mode
Debug mode is built for fast feedback. It gives you these features:
- Hot reload and hot restart.
- Breakpoints and stepping in your editor.
- Assertions, which are checks written as
assert(condition)that stop the app when the condition is false. - Extra framework checks, such as layout overflow warnings (the yellow-and-black stripes) and "setState called after dispose" errors.
- Service extensions, which let Flutter DevTools inspect the widget tree.
Debug builds are larger and slower than the other modes. On Android and iOS you also see a "DEBUG" banner in the corner of the app. Do not judge performance from a debug build. Dropped frames in debug mode often disappear in release.
Run it explicitly like this:
flutter run --debugProfile mode
Profile mode is close to release mode, but it keeps enough tooling to measure the app. It compiles AOT, so timings resemble what users will see. It also leaves the profiling hooks enabled, so DevTools can record frame timings, CPU samples, and memory use.
Profile mode has several rules:
- Use a physical device on Android and iOS. Flutter disables profile mode on emulators and simulators, because their performance does not represent real hardware.
- You cannot hot reload or use breakpoints.
- On the web, profile mode works in Chrome.
- You can still attach DevTools. The
flutter runoutput prints a link to open it.
flutter devices
flutter run --profile -d <device-id>Replace <device-id> with an ID from the flutter devices list.
Release mode
Release mode is what you ship. It compiles AOT with full optimization and removes everything that exists only for development. Specifically:
- Assertions and debug-only checks are off.
- Tree shaking removes code, fonts, and icons your app never uses.
- Debug symbols and service extensions are removed, so the app is smaller.
- Startup and frame times are the best they can be.
For the web, debug mode compiles with the development compiler (DDC) to allow fast rebuilds. Release mode compiles with dart2js, which produces minified, optimized JavaScript. Flutter also supports compiling the web release to WebAssembly in recent versions.
flutter run --release -d <device-id>Build commands default to release
flutter run defaults to debug, but flutter build defaults to release. These commands produce the artifacts you distribute:
flutter build apk
flutter build appbundle
flutter build ios
flutter build webYou can override the mode with a flag, for example flutter build apk --debug or flutter build apk --profile. The mode flags are the same across commands.
Choosing a mode
A practical workflow looks like this:
- Develop in debug mode with hot reload.
- When something feels slow, switch to profile mode on a real device and open DevTools. Find the cause there.
- Before every release, run the app in release mode on real hardware and check the main flows by hand. Some bugs show up only in release.
Reading the mode from code
The flutter/foundation.dart library exposes three constants: kDebugMode, kProfileMode, and kReleaseMode. The compiler knows their values at build time, so code inside a false branch can be removed from release builds.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
assert(() {
debugPrint('Assertions are enabled, so this is a debug build.');
return true;
}());
runApp(const ModeApp());
}
String get buildMode {
if (kReleaseMode) return 'release';
if (kProfileMode) return 'profile';
return 'debug';
}
class ModeApp extends StatelessWidget {
const ModeApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Build mode')),
body: Center(
child: Text(
'Running in $buildMode mode',
style: Theme.of(context).textTheme.headlineMedium,
),
),
),
);
}
}Run this file with each of the three flags and watch the text change. The assert(() { ... }()) pattern runs a block only when assertions are on. The closure returns true so the assertion passes. In profile and release builds, the whole call is skipped.
Use kDebugMode to guard debug-only behavior, such as extra logging:
import 'package:flutter/foundation.dart';
void logRequest(String url) {
if (kDebugMode) {
debugPrint('Requesting $url');
}
}Do not hide real logic behind assertions
Code inside assert does not run in release. If you put a needed side effect there, the app works in debug and breaks in release. Use assertions only for checks that should fail loudly during development. Do not put program logic in them.
Why a release build can behave differently
Release builds change more than speed. These are the differences that most often surprise people.
Android internet permission
The Flutter project template declares the INTERNET permission in the debug manifest at android/app/src/debug/AndroidManifest.xml, so hot reload and DevTools can reach the app. The main manifest at android/app/src/main/AndroidManifest.xml does not include it. If you add the http package and call a server, the call works in debug. In release it fails with a socket error, because the app has no permission.
The fix is to add this line to the main manifest, inside the <manifest> element and before <application>:
<uses-permission android:name="android.permission.INTERNET"/>Signing
The template builds Android release artifacts with the debug signing key, so flutter run --release works out of the box. That key is not valid for the Play Store. Before publishing, you must configure your own signing key. The same applies to iOS, where you need a provisioning profile and a signing team.
Timing and error handling
Because debug mode is slower, race conditions can hide or appear. Release mode also drops framework checks. An error that shows a red screen in debug may fail quietly in release. Apps you ship should install their own error handling, for example by setting FlutterError.onError, so failures are reported.
Code size and obfuscation
Release builds are smaller, but stack traces from them can be hard to read. Flutter can obfuscate Dart code with --obfuscate and --split-debug-info=<directory>. Keep the debug info files for each release, because you need them to translate stack traces later.
flutter build apk --obfuscate --split-debug-info=build/symbolsCommon errors
- "Profile mode is not supported on emulators" or a similar message. Connect a physical device, or use Chrome or a desktop target for profile runs.
- Hot reload does nothing. You are in profile or release mode. Run
flutter runwithout flags. - Network calls work in debug but fail in release on Android. Add the
INTERNETpermission to the main manifest. - The app feels slow in debug. This is expected. Check in profile mode before changing code.
- A
flutter buildoutput is not what you tested. Remember thatflutter builddefaults to release, while your dailyflutter runused debug.
Summary
Debug mode is for development and gives you hot reload, assertions, and a debugger. Profile mode is for measurement on a physical device, with AOT speed and DevTools tracing. Release mode is for shipping, with full optimization and no development tooling. Test your final flows in release mode before you publish, because assertions, permissions, and signing can all behave differently there.
Exercise
You added the http package and a button that fetches data. The fetch works with flutter run, but it fails on an Android phone with flutter run --release. What is the likely cause, and what is the fix?
Show answer
Debug runs use the debug manifest, which the template gives the INTERNET permission. The main manifest has no such permission, so a release build cannot open network connections. Add <uses-permission android:name="android.permission.INTERNET"/> to android/app/src/main/AndroidManifest.xml, before the <application> element.
Exercise
Your list screen drops frames when you scroll it in debug mode on an emulator. Which mode and device should you use to decide whether it is a real problem, and which tool should you open?
Show answer
Use profile mode on a physical device, started with flutter run --profile -d <device-id>. Open Flutter DevTools and look at the performance view. Debug mode on an emulator is slower than real use, so its frame times do not show whether users would see jank.