Obfuscate Dart source
Requires Codefend 3.3.0 or later for the recommended m_ preset.
This example renames selected identifiers in a Dart CLI program before running it. It calculates a discounted order total in integer cents.
Install the Dart SDK and Node.js for Codefend. In a new directory:
npm install --global codefend1. Add a standalone program
Create source/main.dart:
void main() {
int m_total(int m_price, int m_count, int m_percent) {
final m_subtotal = m_price * m_count;
final m_discount = m_subtotal * m_percent ~/ 100;
return m_subtotal - m_discount;
}
print(m_total(1000, 2, 10));
}Expected obfuscated source
With the configuration in the next step, Codefend produces:
void main() {
int Ox0(int Ox1, int Ox2, int Ox3) {
final Ox4 = Ox1 * Ox2;
final Ox5 = Ox4 * Ox3 ~/ 100;
return Ox4 - Ox5;
}
print(Ox0(1000, 2, 10));
}The program's expected output remains 1800. Generated identifier numbers can change when the source or Codefend version changes.
The helper is local to main, so this example does not rename an API imported by another library. Dart's main, print, and built-in type names stay unchanged. The ~/ operator keeps the discount calculation in integers.
2. Configure Codefend
Create .codefendrc.json in the project root:
{
"generation": {
"inputDir": "source",
"outputDir": "release"
},
"parser": {
"name": "startWithMAndUnderscore"
}
}The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.
Plain main.dart does not match it; the empty pool uses generated prefix-and-sequence names.
Keep release separate from source and reserve it for disposable generated files. Codefend removes an existing output directory. Keep package caches, Flutter assets, compiled artifacts, and other binary files outside the processed input tree.
3. Run both source files
Run from the project root:
dart run source/main.dart
codefend --obfuscate
dart run release/main.dartBoth versions should print:
1800Dart supports running a source file by its relative path. This standalone example needs no package dependencies.
Dart and Flutter integration
Preserve library APIs, named-argument contracts, JSON keys, generated-code references, and platform-channel method names. Dart uses a leading underscore for library privacy; this example's m_ convention is a selection marker, not a privacy mechanism. Renaming private identifiers with a prefix that drops their leading underscore can change their visibility.
This guide covers source-text transformation of a CLI program. It does not establish compatibility with an entire Flutter application or replace Flutter's own build-time obfuscation options. Test the generated Dart application, including reflection or interop features you use, and review current limitations.
Add Codefend to your workflow
Run Codefend before your Dart build or validation step, then run the generated application and its tests. Keep package configuration and dependencies in their normal locations, outside the text-only input tree.
codefend --obfuscate
dart run release/main.dartIn CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.