-
Notifications
You must be signed in to change notification settings - Fork 1
Experimental: Launch Flutter with non main entrypoint
Everything in this doc and linked from this doc is experimental. These details WILL change. Do not use these instructions or APIs in production code because we will break you.
Typically, a Flutter app begins execution at the Dart method called main()
, however this is not required. Developers can specify a different Dart entrypoint:
Two options are available to specify a non-standard Dart entrypoint for a FlutterActivity
.
Specify your desired Dart entrypoint as meta-data
in your AndroidManifest.xml
:
<application ...>
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
...
>
<meta-data
android:name="io.flutter.Entrypoint"
android:value="myMainDartMethod"
/>
</activity>
</application>
Option 2: Subclass FlutterActivity
and override a method
Override the getDartEntrypointFunctionName()
method:
public class MyFlutterActivity extends FlutterActivity {
@Override
@NonNull
public String getDartEntrypointFunctionName() {
return "myMainDartMethod";
}
}
Two options are available to specify a non-standard Dart entrypoint for a FlutterFragment
.
// Example for a FlutterFragment that creates its own FlutterEngine.
//
// Note: a Dart entrypoint cannot be set when using a cached engine because the
// cached engine has already started executing Dart.
FlutterFragment flutterFragment = new FlutterFragment
.withNewEngine()
.dartEntrypoint("myMainDartMethod")
.build();
public class MyFlutterFragment extends FlutterFragment {
@Override
@NonNull
public String getDartEntrypointFunctionName() {
return "myMainDartMethod";
}
}
When manually initializing a FlutterEngine
, you take on the responsibility of
invoking the desired Dart entrypoint, even if you want the standard main()
method.
The following examples illustrate how to execute a Dart entrypoint with a
FlutterEngine
.
Example using standard entrypoint:
// Instantiate a new FlutterEngine.
FlutterEngine flutterEngine = new FlutterEngine(context);
// Start executing Dart using a default entrypoint, which resolves to "main()".
flutterEngine
.getDartExecutor()
.executeDartEntrypoint(
DartEntrypoint.createDefault();
);
Example using custom entrypoint:
// Instantiate a new FlutterEngine.
FlutterEngine flutterEngine = new FlutterEngine(context);
// Start executing Dart using a custom entrypoint.
flutterEngine
.getDartExecutor()
.executeDartEntrypoint(
new DartEntrypoint(
FlutterMain.findAppBundlePath(),
"myMainDartMethod"
)
);
When you build in release mode, your Dart code is tree-shaken. This means that the compiler removes any Dart code that it thinks you're not using. This includes your special entrypoints. To avoid crashing in release mode as a result of tree-shaking, be sure to place the following @pragma
above each of your custom entrypoints.
@pragma('vm:entry-point')
void myMainDartMethod() {
// implementation
}
- Home of the Wiki
- Roadmap
- API Reference (stable)
- API Reference (master)
- Glossary
- Contributor Guide
- Chat on Discord
- Code of Conduct
- Issue triage reports
- Our Values
- Tree hygiene
- Issue hygiene and Triage
- Style guide for Flutter repo
- Project teams
- Contributor access
- What should I work on?
- Running and writing tests
- Release process
- Rolling Dart
- Manual Engine Roll with Breaking Commits
- Updating Material Design Fonts & Icons
- Postmortems
- Setting up the Framework development environment
- The Framework architecture
- The flutter tool
- API Docs code block generation
- Running examples
- Using the Dart analyzer
- The flutter run variants
- Test coverage for package:flutter
- Writing a golden-file test for package:flutter
- Setting up the Engine development environment
- Compiling the engine
- Debugging the engine
- Using Sanitizers with the Flutter Engine
- Testing the engine
- The Engine architecture
- Flutter's modes
- Engine disk footprint
- Comparing AOT Snapshot Sizes
- Custom Flutter engine embedders
- Custom Flutter Engine Embedding in AOT Mode
- Flutter engine operation in AOT Mode
- Engine-specific Service Protocol extensions
- Crashes
- Supporting legacy platforms
- Metal on iOS FAQ
- Engine Clang Tidy Linter
- Why we have a separate engine repo
- Reduce Flutter engine size with MLGO
- Setting up the Plugins development environment
- Setting up the Packages development environment
- Plugins and Packages repository structure
- Plugin Tests
- Contributing to Plugins and Packages
- Releasing a Plugin or Package
- Unexpected Plugins and Packages failures