Repository: flutter
Issue: Custom Android FlutterEngineGroup prevents CPU Profiling
Labels: platform-android,engine,has reproducible steps,P2,team-engine,triaged-engine,found in release: 3.13,found in release: 3.16

### Background

Similar to the setup described in https://github.com/flutter/flutter/issues/124546, I am trying to create and manage my own `FlutterEngineGroup` in a Flutter Application that runs on Android. However, after creating an engine group and specifying that `WidgetsFlutterBinding.ensureInitialized()` in the custom dart entry-point breaks the ability to measure the CPU performance in the Flutter dev tools.

```dart
@pragma('vm:entry-point')
void runContentProvider() {
  // commenting out the following re-enables the CPU profiler
  WidgetsFlutterBinding.ensureInitialized();
}
```


### Steps to reproduce

1. Create a new flutter project: `flutter create app`
2. In the Android Platform code, create a custom `FlutterApplication`:

```kotlin
import android.content.Context
import io.flutter.app.FlutterApplication
import io.flutter.embedding.engine.FlutterEngineGroup

class App : FlutterApplication() {
    lateinit var engines: FlutterEngineGroup
    private var isEngineAttached = false;

    override fun onCreate() {
        super.onCreate()
        createEngineGroupIfNeeded(this);
    }

    fun createEngineGroupIfNeeded(context: Context) {
        if (isEngineAttached) {
            return;
        }
        engines = FlutterEngineGroup(this);
        isEngineAttached = true;
    }
}
```

3. In the activity, use the engine group to create `provideFlutterEngine`:

```kotlin
import android.content.Context
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine

class MainActivity: FlutterActivity() {
    override fun provideFlutterEngine(context: Context): FlutterEngine? {
        val app = context.applicationContext as App
        return app.engines.createAndRunDefaultEngine(context);
    }
}
```

4. Define a custom content provider:

```kotlin
class ExampleContentProvider : ContentProvider() {
    private lateinit var engine: FlutterEngine

    override fun onCreate(): Boolean {
        val app = context!!.applicationContext as App

        val flutterLoader = FlutterInjector.instance().flutterLoader()
        flutterLoader.startInitialization(context!!.applicationContext)

        // create the engine group
        app.createEngineGroupIfNeeded(app);

        val entrypoint = DartExecutor.DartEntrypoint(flutterLoader.findAppBundlePath(), "runContentProvider")
        engine = app.engines.createAndRunEngine(context!!, entrypoint)

        return true;
    }
}
```

5. In your `AndroidManifest.xml`, use the custom application and the content provider:

```xml
    <application
        android:label="app"
        android:name=".App"
        android:icon="@mipmap/ic_launcher">
        <provider
            android:name=".ExampleContentProvider"
            android:authorities="${applicationId}.ExampleContentProvider">
        </provider>
```

6. Define your entry point on the dart side in your `main.dart`:

```dart
@pragma('vm:entry-point')
void runContentProvider() {
  WidgetsFlutterBinding.ensureInitialized();
}
```

6. Run the app in the app in profile `flutter run --profile`


### Expected results

Ensuring that the flutter bindings are initialized from a custom dart entry points retains the ability to use the CPU profiler.

### Actual results

CPU Profiler is disabled, enabling the profiler does not collect any samples of the process.

### Code sample

<details><summary>Code sample</summary>

```dart
[Paste your code here]
```

</details>


### Screenshots or Video

<img width="800" alt="Flutter Dev Tools - CPU Profiler Disabled" src="https://github.com/flutter/flutter/assets/8084674/63eb9308-0515-406e-acfa-e59c9e31be52">

### Logs

_No response_

### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.5, on macOS 13.5 22G74 darwin-arm64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[!] Android Studio (version unknown)
    ✗ Unable to determine Android Studio version.
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.82.3)
[✓] Connected device (3 available)
[✓] Network resources
```

</details>