1. Manually integrate Flutter into the Android project

  1. Flutter support x86_64, armeabi – v7a arm64 – v8a

    android {
      //...
      defaultConfig {
        ndk {
          // Filter forArchitectures supported by Flutter. // Add this if testing using an Android simulator'x86'Platform abiFilters'armeabi-v7a'.'arm64-v8a'.'x86_64'}}}Copy the code
  2. Java8 support

    android {
      //...
      compileOptions {
        sourceCompatibility 1.8 targetCompatibility 1.8}}Copy the code
  3. Run the command to create a Flutter project, Flutter create -t module –org cn. It200 my_flutter, in the same directory as the existing Android project

  4. The configuration Settings. Gradle

    // Include the host app project.
    include ':app'                                    // assumed existing content
    setBinding(new Binding([gradle: this]))                                // new
    evaluate(new File(                                                     // new
      settingsDir.parentFile,                                              // new
      'my_flutter/.android/include_flutter.groovy'                         // new
    ))                                                                     // new
    
    Copy the code
  5. Relies on the Flutter module for Android projects

    dependencies {
      implementation project(':flutter')}Copy the code
  6. The package related to Flutter can be imported normally in Java code and the project is compiled without exception

2. Start the Flutter page in the Activity

  1. Register FlutterActivity into the manifest file

    <activity
      android:name="io.flutter.embedding.android.FlutterActivity"
      android:theme="@style/LaunchTheme"
      android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
      android:hardwareAccelerated="true"
      android:windowSoftInputMode="adjustResize"
    />
    Copy the code
  2. Start the FlutterActivity

    / / open the homepage startActivity (FlutterActivity createDefaultIntent (currentActivity)); StartActivity (flutterActivity.withnewEngine ().initialRoute(flutterActivity.withnewEngine ()."/other")
                    .build(MainActivity.this)
    );
    Copy the code
  3. FlutterEngine is introduced to speed up startup

    FlutterEngine FlutterEngine = new FlutterEngine(this); flutterEngine.getDartExecutor().executeDartEntrypoint( DartExecutor.DartEntrypoint.createDefault() ); FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);
    Copy the code
    / / change the way we start startActivity (FlutterActivity withCachedEngine ("my_engine_id").build(MainActivity.this); ) ;Copy the code
  4. Initialize the route when importing FlutterEngine

    Insert the following content: after creating the object flutterEngine flutterEngine. GetNavigationChannel () setInitialRoute ("/other");
    Copy the code

Load the Flutter page in your Activity

  1. Layout file adds code

    <FrameLayout
        android:layout_weight="1"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    Copy the code
  2. Add code to the Activity

    // Define a tag String to represent the FlutterFragment within this
    // Activity's FragmentManager. This value can be whatever you'd like.
    private static final String TAG_FLUTTER_FRAGMENT = "flutter_fragment";
    
    // Declare a local variable to reference the FlutterFragment so that you
    // can forward calls to it later.
    private FlutterFragment flutterFragment;
    Copy the code
    // Get a reference to the Activity's FragmentManager to add a new // FlutterFragment, or find an existing one. FragmentManager fragmentManager = getSupportFragmentManager(); // Attempt to find an existing FlutterFragment, // in case this is not the first time that onCreate() was run. flutterFragment = (FlutterFragment) fragmentManager .findFragmentByTag(TAG_FLUTTER_FRAGMENT); // Create and attach a FlutterFragment if one does not exist. if (flutterFragment == null) { flutterFragment = FlutterFragment.createDefault(); fragmentManager .beginTransaction() .add( R.id.fragment_container, flutterFragment, TAG_FLUTTER_FRAGMENT ) .commit(); }Copy the code
    @Override
    public void onPostResume() {
        super.onPostResume();
        flutterFragment.onPostResume();
    }
    
    @Override
    protected void onNewIntent(@NonNull Intent intent) {
        super.onNewIntent(intent);
        flutterFragment.onNewIntent(intent);
    }
    
    @Override
    public void onBackPressed() {
        flutterFragment.onBackPressed();
    }
    
    @Override
    public void onRequestPermissionsResult(
            int requestCode,
            @NonNull String[] permissions,
            @NonNull int[] grantResults
    ) {
        flutterFragment.onRequestPermissionsResult(
                requestCode,
                permissions,
                grantResults
        );
    }
    
    @Override
    public void onUserLeaveHint() {
        flutterFragment.onUserLeaveHint();
    }
    
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        flutterFragment.onTrimMemory(level);
    }
    Copy the code
  3. Using FlutterFragment. WithCachedEngine (” my_engine_id “). The build (); accelerating

    if (flutterFragment == null) {
        flutterFragment = FlutterFragment.withCachedEngine("my_engine_id").build();
        fragmentManager
                .beginTransaction()
                .add(
                        R.id.fragment_container,
                        flutterFragment,
                        TAG_FLUTTER_FRAGMENT
                )
                .commit();
    }
    Copy the code