See a lot of people on the Internet have introduced this part of the content, most of them have similarities. But generally write relative overview. For me, who was new to Both Android and Flutter. It’s a little rough. Record some problems in the process of using yourself here. This article focuses on some of the processes.


  • Note that Android Studio is at least 3.2.

    The project uses AndroidX. For those unfamiliar with the androidx support library move to an in-depth look at androidx

Start by creating a new Android project using Android Studio

When creating a project, check the following box to support androidx.

Example Modify the Android project configuration

  • The first step

Open the Project Structure and make the following Settings

  • The second step

Set SDK not less than 28, the following two parameters fixed 1.8

  • The third part

Create a New Flutter Module New >New Flutter Project–> Select the flutter Module–> Fill in the generated Module name and path. Here I put it in the same directory as the Android project. Just next

  • The fourth part

Import a flutter Module New–> New Module–>Import a flutter Module–> Select the New flutter Module created in the previous step.

The next step

Sync Project is then required to see the Flutter project configuration file

Modify the configuration file in the Flutter project

  • Open the android/gradle/wrapper/gradle – wrapper. The properties and modifying distributionUrl values.
/ / if the version is not 4.10.2 replace distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zipCopy the code
  • Open the android/build gradle, modify the com. Android. View the build: gradle 3.3.0
Dependencies {classpath'com. Android. Tools. Build: gradle: 3.2.1'} // replace dependencies {classpath'com. Android. Tools. Build: gradle: 3.3.0'
}
Copy the code
  • Open the android/gradle. The properties, add two lines
android.enableJetifier=true
android.useAndroidX=true
Copy the code
  • Also, make sure to add the following code to your Android project directory app/build.gradle:
android { compileSdkVersion 28 defaultConfig { ... } //flutter related declarations compileOptions {sourceCompatibility 1.8 targetCompatibility 1.8}}Copy the code
  • Replace all deprecated libraries with their AndroidX equivalents. For example, if you use the default. Gradle file, make the following changes:

The android/app/build. Gradle and android/flutter/build. Gradle

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"/ / replacetestInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Copy the code

Second android/flutter/build. Gradle, replacement in the dependencies

implementation 'com. Android. Support: support - v13:27.1.1'
implementation 'com. Android. Support: support - annotations: 27.1.1 / / replace support androidx implementation'Androidx. Legacy: legacy support - v13:1.0.0'
implementation 'Androidx. Annotation: the annotation: 1.0.0'
Copy the code

Finally, replace android/app/build.gradle in Dependencies

implementation 'com. Android. Support: appcompat - v7:27.1.1'
implementation 'com. Android. Support. The constraint, the constraint - layout: 1.1.2'
implementation 'com. Android. Support: design: 27.1.1'
androidTestImplementation 'com. Android. Support. Test: runner: 1.0.2'
androidTestImplementation 'com. Android. Support. Test. Espresso: espresso - core: 3.0.2'// replace with implementation like android project app/build.gradle'androidx. Appcompat: appcompat: 1.0.2'
implementation 'androidx. Constraintlayout: constraintlayout: 1.1.3'
androidTestImplementation 'androidx. Test: runner: 1.2.0'
androidTestImplementation 'androidx. Test. Espresso: espresso - core: 3.2.0'
Copy the code

See more substitution mappings here

Most important is the modification of the FLUTTER engineering files

Modify android/Flutter/SRC/main/Java/IO/Flutter/facade of the two files below

  1. Flutter.java
import android.arch.lifecycle.Lifecycle; import android.arch.lifecycle.LifecycleObserver; import android.arch.lifecycle.OnLifecycleEvent; import android.support.annotation.NonNull; / / modify for import androidx. Lifecycle. Lifecycle; import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.OnLifecycleEvent; import androidx.annotation.NonNull;Copy the code
  1. FlutterFragment.java
import android.support.annotation.NonNull; import android.support.v4.app.Fragment; Modified to import androidx. The annotation. NonNull; import androidx.fragment.app.Fragment;Copy the code

Configuration is complete, and finally flutter is embedded in the Android project

There are two ways to embed flutter: FlutterView and FlutterFragment.

  1. In the MainActivity generated by default for Android projects, use FlutterView.
View flutterView = Flutter.createView(
    MainActivity.this,
    getLifecycle(),
    "route1"
);
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600, 800);
layout.leftMargin = 100;
layout.topMargin = 400;
addContentView(flutterView, layout);
Copy the code
  1. In the MainActivity generated by default for Android projects, use FlutterFragment.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fl_container, Flutter.createFragment("route1"));
fragmentTransaction.commit();
Copy the code

Fl_containerview corresponds to a FrameLayout defined in the RES/Layout XML in Android:

   <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
Copy the code

Then run the Engineering skills to see the Flutter interface. If you have any questions, please leave a message. We can discuss it together.