I used package_info, shared_preferences and other plug-ins that need to interact with native folders in Flutter AAR. After I packaged the AAR and integrated it into the Android project, I found that these plug-ins didn’t work.

The error message basically means something like method channel is not registered;

Probably most apps that integrate Flutter into Android will use a new page developed with Flutter, namely FlutterActivity, which will not cause any problems. In the new version, FlutterActivity automatically helps initialize third-party Flutter plug-ins, but flutterFragment does not.

In my project, there are only 3 fragments in the viewpager, the third of which is flutterFragment. Then the activity hosting these fragments inherits my baseActivity and cannot be changed to FlutterActivity.

My solution is to initialize the third-party Flutter plugin in my Application. The initialization plugin needs to pass in the instance of flutterEngine. Since I have preloaded the flutterEngine, I just pass it in directly:

	flutterEngine = new FlutterEngine(this);
    // Configure an initial route.
    flutterEngine.getNavigationChannel().setInitialRoute("your/route/here");
    // Start executing Dart code to pre-warm the FlutterEngine.
    flutterEngine.getDartExecutor().executeDartEntrypoint(
      DartEntrypoint.createDefault()
    );
    
    // After creating the preloaded flutterEngine, initialize the third-party FLUTTER plugin used by flutterEngine here.
    GeneratedPluginRegistrant.registerWith(flutterEngine);
    
    // Cache the FlutterEngine to be used by FlutterActivity or FlutterFragment.
    FlutterEngineCache
      .getInstance()
      .put("my_engine_id", flutterEngine);
Copy the code

The advantage of preloading flutterEngine is that flutter pages load much faster. The official documentation of Flutter will not tell you about these things. The issues of Flutter also tell you to use the latest version of flutter plugin, which is not available at all. I don’t know why the official documentation doesn’t say more.