flutter/docs/wiki_archive/Experimental-Launch-Flutter-with-non-main-entrypoint.md
Kate Lovett 1fbcbb73a0
[wiki migration] Leftover wiki pages and links (#148989)
This is waiting on 
- https://github.com/flutter/flutter/pull/148777
- https://github.com/flutter/flutter/pull/148790

After this PR lands, there will likely be 1-2 more clean up PRs, after which the migration will be done!

---

This moves the remaining wiki pages as planned in [flutter.dev/go/migrate-flutter-wiki-spreadsheet](https://docs.google.com/spreadsheets/d/1x65189ZBdNiLRygpUYoU08pwvXD4M-Z157c6pm8deGI/edit?usp=sharing) 

It also adds the team labels to the label bot for future PRs.

Changes to the content were only updating cross links, or links to refer to the main branch rather than master.
Remaining links to the wiki will be updated once all other pages have finished moving, they still work in the meantime.

Part of https://github.com/flutter/flutter/issues/145009
2024-05-28 15:12:10 +00:00

3.2 KiB

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.

Launch Flutter with non-main entrypoint

Typically, a Flutter app begins execution at the Dart method called main(), however this is not required. Developers can specify a different Dart entrypoint:

FlutterActivity

Two options are available to specify a non-standard Dart entrypoint for a FlutterActivity.

Option 1: AndroidManifest.xml meta-data

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";
  }
}

FlutterFragment

Two options are available to specify a non-standard Dart entrypoint for a FlutterFragment.

Option 1: Use FlutterFragmentBuilder

// 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();

Option 2: Subclass FlutterFragment

public class MyFlutterFragment extends FlutterFragment {
  @Override
  @NonNull
  public String getDartEntrypointFunctionName() {
    return "myMainDartMethod";
  }
}

FlutterEngine

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"
    )
  );

Avoid Tree Shaking in Release

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
}