1. How to embed the Flutter Module in an existing Android project
  2. How does an Activity jump to a Flutter page and transfer values
  3. How does a Flutter jump to an Activity and transfer values
  4. The Android module interacts with the Flutter module

All source code has been uploadedgithub

1. How to embed Flutter Module into existing Android projects

1. Create a new Android project with Android Studio. We’ll call ithyBridAndroid

2. Then close the project and create a new directory, which we’ll call hereHyBrid

Then run the flutter create -t module flutter_lib command in the HyBrid directory to create the flutter module. Put the Android project you just created into this directory, and now it looks like this

3. Open Android StudiohyBridAndroidProject, and then add dependencies

Local dependencies:

  • For Android local dependencies, include_flutter. Groovy and flutter. Gradle scripts are responsible for local dependencies and product construction of Flutter

    • Inject the include_flutter. Groovy script into settings.gradle
    • Add project(‘:flutter’) dependencies to build.gradle in the module that you want to rely on
  • The code is shown below

include ':app'
// There are many blogs on the web that do this
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir.parentFile, 'flutter_lib/.android/include_flutter.groovy'))
Copy the code
  • Then add the dependency libraries to the build.gradle file in your Android native app
implementation project(':flutter')
Copy the code
  • Now that we’ve done this, our Android Studio directory structure looks like this

Potholes are possible

It is easy for Android developers to mistakenly think that the Flutter Module integration is just like the Android Engineering Module integration before, so in this vein, Include \_flutter. Groovy files are lost when using Sync dependencies. There are two ways to solve this problem. In the first case, your configuration is essentially wrong and the wiki tells you to put the Flutter_module in the same directory as the Android project instead of the Android project directory. Think about why this kind of problem will appear, mainly is the problem caused by incorrect translation of some bad articles.

Pay attention to

If buildTypes in your project has release, preview, debug… Wait, you need to add the following code

BuildTypes {release {} preview {// key code, release, BuildType matchingFallbacks = ['release', 'debug']} debug {}}Copy the code

4. Run the project

You can now directly open the 'hyBridAndroid' project to edit the native part of the code, and open the 'Flutter_lib' project to edit the flutter part of the codeCopy the code

Note: The Android project has a Flutter embedded in it. The Android project and the Flutter project are developed separately. This means that the Android project and the Flutter project should be opened separately when you develop them

Reference:

Github.com/yangchong21…

Welcome to the Flutter development group 457664582. Click join to learn and discuss Flutter