0
0
Fluttermobile~5 mins

Code obfuscation and optimization in Flutter

Choose your learning style9 modes available
Introduction

Code obfuscation hides your app's code to protect it from copying. Optimization makes your app run faster and use less space.

When you want to protect your app's code from being copied or understood by others.
Before releasing your app to make it smaller and faster.
When you want to improve app performance on older or slower devices.
To reduce app size so users download it faster.
When you want to keep your app secure from reverse engineering.
Syntax
Flutter
flutter build apk --obfuscate --split-debug-info=/<project-name>/debug-info
flutter build ios --obfuscate --split-debug-info=/<project-name>/debug-info

Use --obfuscate to hide your Dart code names.

--split-debug-info saves debug info separately to help with debugging after obfuscation.

Examples
Builds an Android app with code obfuscation and saves debug info in the debug-info folder.
Flutter
flutter build apk --obfuscate --split-debug-info=./debug-info
Builds an iOS app with code obfuscation and saves debug info in the debug-info folder.
Flutter
flutter build ios --obfuscate --split-debug-info=./debug-info
Builds a release APK without obfuscation but with some optimization.
Flutter
flutter build apk --release
Sample App

This simple Flutter app shows a message. You can build it with obfuscation and optimization using the commands shown earlier.

Flutter
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Obfuscation Demo',
      home: Scaffold(
        appBar: AppBar(title: const Text('Obfuscation and Optimization')),
        body: const Center(child: Text('App is ready for obfuscation!')),
      ),
    );
  }
}
OutputSuccess
Important Notes

Obfuscation only changes names in your Dart code; it does not encrypt your app.

Keep the debug info folder safe to debug crashes after obfuscation.

Optimization happens automatically in release builds but can be improved with obfuscation flags.

Summary

Code obfuscation protects your app by hiding code details.

Optimization makes your app smaller and faster.

Use Flutter build commands with --obfuscate and --split-debug-info to apply these.