For older Android projects, the best way to access flutter is to add a Flutter Module so that the main module relies on flutter in a module-dependent manner.

Let’s review the small steps, because there’s a lot of information on the web, I won’t go into detail.

1 Create a Flutter Module

Run the command under the current project

Flutter create -t module my_flutter (my_flutter is the generated flutter module name)Copy the code

2 Add the configuration code

Add the following configuration to your project settings.gradle

setBinding(new Binding([gradle: this]))                                 // new
evaluate(new File(                                                      // new
        settingsDir.parentFile,                                               // new
        'yq_flutter/.android/include_flutter.groovy'                          // new
))
Copy the code

Add it to app Gradle

implementation project(':my_flutter')
Copy the code

So you can develop, compile and see the effect.





Synthetic intermediate

But then there’s the question: There are only a few people on the team working on flutter, and the rest of the team doesn’t need to pay attention to flutter. The above dependencies need to install the Flutter environment, download the SDK, etc. Otherwise, the project cannot be compiled, so it needs to rely on JAR/AAR for integration.





flutter build apk
Copy the code

Can generate a release apk, at the same time in rootProj/android/Flutter/build/outputs/aar/directory to generate the aar, put the aar kao out directly to you withdraw rely on the original project will be able to get rid of relying on the module. However, to be more formal, you need to upload the AAR to your company’s own Nexus Maven (or JCenter) via address dependency. A reference to the normal AAR package upload method will show that an AAR can be generated, but will fail to run because there is no compilation product packaged into Flutter. Normally generated aar structure diagram

Normal Gradle builds cannot be packaged into intermediates and resource files, so refer to the gradle files of the Flutter project. You can see that the Flutter Gradle scripts are added to the normal Build process

Specific content can have access to the SDK directory to check, it’s job actually is to copy the product into your apk file, so we could start with the local building apk, then unzip it and take out the product, copy to your project, and then rebuilt and then uploaded to the maven, refer to some of the information online, to modify a script for your use. One of the parts you may need to modify is your company’s own uploadArchives script.

The build script

Then rely on, or even add, configuration switches in your project to control whether they are aar or Module dependent

Gradle. Properties switch

FLUTTER_DEBUG = true
Copy the code

Native Module dependencies

 if(FLUTTER_DEBUG.equals("true")){
        implementation project(':flutter')}else {
        implementation 'com. XXX. XXX: my - flutter: 1.0.0. @ aar'
    }
Copy the code

The attached:

Gradle version compatibility problems encountered

I am currently using Andrdoid Studio 3.1.4. The Android Gradle plugin version is 3.2.1 and the Gradle version is 4.10.2. The local gradle is 4.4, and the minimum requirement is 4.6. I am very confused. The local gradle is obviously higher than 4.6. The plugin version was lowered to 3.1.2 and the Gradle version was changed to 4.4. The Gradle version of the Flutter SDK was also 3.1.2. It’s just that the error message makes me feel helpless.

2. My local FLUTTER environment for your reference

✓] Flutter (Channel master, v1.5.9-pre.235, on Mac OS X 10.13.6 17G65, ✓ Locale zh-hans-cn) [✓] Android toolchain-developforAndroid Devices (Android SDK Version 28.0.3) [✓] iOS Toolchain-developforIOS Devices (Xcode 10.1) [✓] Android Studio (version 3.2) [✓] VS Code (version 1.30.2) [✓] Connected device (1) available)Copy the code

“Update”

2019.5.27

There is a problem with the previous packaging. If your Flutter Module relies on a third-party plugin, you cannot add the plugin content to the package.

Love the cat’s small guo gave a plan: github.com/CarGuo/flut… , with the help of fat-AAR.

Apply plugin to flutter Module'com.kezong.fat-aar'2 is added in the dependencies def flutterProjectRoot = rootProject. ProjectDir. ParentFile. ToPath (def) plugins = new Properties () def pluginsFile = new File(flutterProjectRoot.toFile(),'.flutter-plugins')
    if (pluginsFile.exists()) {
        pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
    }
    plugins.each { name, _ ->
        println name
        embed project(path: ":$name", configuration: 'default'} add classpath to build.gradle in project'com. Kezong: fat - the aar: 1.0.3'Third, setting. Add def gradle flutterProjectRoot = rootProject. ProjectDir. ParentFile. ToPath (def) plugins = new Properties ()  def pluginsFile = new File(flutterProjectRoot.toFile(),'.flutter-plugins')
    if (pluginsFile.exists()) {
        pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
    }

    plugins.each { name, path ->
        def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
        include ":$name"
        project(":$name").projectDir = pluginDirectory
    }

Copy the code

In addition, I have optimized the script so that the intermediate and asset resources are generated directly when the AAR is packaged, instead of having to extract the locally generated APK. The build script