[TOC]

Why Flutter ?

Flutter framework and application

Framework with Source Code

The overall framework

Source Code

Therefore, it can be seen from the above analysis that In order to achieve no difference of Flutter platforms, Google mainly ADAPTS to PlatForm Integration and Dart:UI.

APK(Android application) structure

We will explore what changes happen to the package after adding Flutter by unzipping a release of the mixed development APK package and what the corresponding effects of these changes are.

The point of change can be clearly seen in the markings above

  • Change 1

    It is not marked in the figure, but I believe most developers can guess that the dex has PlatForm Integration code (FutterActivity, FlutterApplication, FlutterView etc.) in it.

  • Change 2

    The four files under assets are arm instructions

    • isolate_snapshot_data/isolate_snapshot_instr

      AOT compilation of our Flutter code was used to create a new ISOLATE

    • vm_snapshot_data/vm_snapshot_instr

      It is used to initialize the Dart VM and provide runTime and GC services

    Because this part of content is compiled by gen_snapshot and stored in the data directory of App, so

    Provides the possibility for the future dynamic update.

  • Change 3

    The ICU Dart Language package provides language-related data information.

  • Change 4

The code for the Flutter Engine layer

Flutter mixing mode

It is believed that the cost of building a new App from scratch is quite high for most apps, so hybrid development is the first choice for them to try Flutter.

Idle fish pattern

Coexistence of two Branches (Flutter mode && Standalone Mode)

Standalone mode: Purely Native development or platform packaging mode

Flutter mode: Development, library generation, compilation, and debugging of Flutter functions follow the process defined by Flutter.

  • advantages

In Standalone mode, the purely Native developer and packaging platform is unaware of the Flutter. In this case, the code associated with Flutter can be thought of as a regular third-party library file.

  • preparation

    Clarify the Standalone mode dependencies on Flutter and extract them into an AAR library.

  • Development steps

    1. Develop Flutter related functions in Flutter mode.
    2. Package the code into an AAR library and upload it to a Repository for version control.
    3. Switch the branch to Standalone mode and change the version number of the relevant dependency package.

    Of course, this method will also encounter many other problems in the actual development process, such as the creation of aar library scripts in complex processes, such as code synchronization in two modes, etc..

Google mode

With so much interest in developing a hybrid mode for the convenience of Flutter, Google created a Wiki dedicated to this issue and updated it with 42 versions over a period of four months.

Create Flutter Module mode
1.1.1 Switching a Flutter Branch

If directly used

$ cd some/path/
$ flutter create -t module my_flutter
Copy the code

We found that the default clone branch of Flutter does not support module commands because the beta version of Flutter does not support module commands

flutter channel
flutter channel master
flutter upgrade
Copy the code

Switch the branch to Master and perform the upgrade operation.

1.1.2 Creating a Flutter Module Template
flutter create -t module flutter_module
Copy the code

At this point you’ll see a new Flutter_module added to project that contains.android,.ios, and the key include_flutter. Groovy files

1.1.3 Add Flutter to an existing project
  • Add it to settings.gradle in the root directory of the Android project

    include ':app'                                     // assumed existing content
    setBinding(new Binding([gradle: this]))                                 // new
    evaluate(new File(                                                      // new
      settingsDir.parentFile,                                               // new
      'flutter_module/.android/include_flutter.groovy'                      // new
    )) 
    Copy the code
  • Add dependencies in your app’s build.gradle

    dependencies {
      implementation project(':flutter')
    Copy the code
1.1.4 In summary

To put it simply, Google creates a chain of logical dependencies for Insert flutter Module without compromising maintenance and development costs

flutter_module/.android/include_flutter.groovy ->
flutter_module/.android/Flutter/build.gradle ->
$flutterRoot/packages/flutter_tools/gradle/flutter.gradle 
Copy the code

Using $flutterRoot/packages/flutter_tools/lib/flutter in the command &&

$AndroidRoot/build-tools/buildToolsVersion/ android Command

Complete the packaging operation in mixed development mode.