Summary of The Mixed Development of The Android Flutter

After looking at some of the considerations and some of the basics of the Android Flutter hybrid development, here are two pits that you are now sure to encounter during the actual development process. In the development process of Flutter, if the dependency is in the form of module, it is not possible to debug the native part of Android. It is not possible to attach debug to Android process. In order to solve this problem, we need to consider using an AAR for dependencies in the form of a library, but when we package the flutter into a library, if we use a third party framework, the third party framework cannot be packaged into an AAR, so we need to solve this problem.

Mixed development dependencies

Package as a separate AAR

Packaging an AAR is easy. Go to project Flutter and go to the. Android directory. Type the command:

./gradlew flutter:assembleDebug
Copy the code

Debug release:

./gradlew flutter:assembleRelease
Copy the code

Good package in flutter module/android/flutter/build/outputs/aar

The aar package can be directly copied to the Android project to rely on. However, if using a third-party framework causes the third-party framework to not be able to enter the AAR, we can use fat-Aar-Android to solve this problem: in the build. Gradle of the Flutter project add:

The classpath 'com. Kezong: fat - the aar: 1.2.7'Copy the code

Add the following configuration to the build.gradle of the Flutter:

apply plugin: 'com.kezong.fat-aar'

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')
    }
}
Copy the code

Add the following Settings to the Android project settings.gradle:

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, path -> def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile() include ":$name" project(":$name").projectDir = pluginDirectory }Copy the code

The packaged AAR thus includes a third-party AAR.

The project Flutter depends on as a module

Module dependencies have been covered before and can be viewed directly in this article for a summary of Android Flutter mixed development issues.

Debugging pits for mixed development

The way the Flutter android hybrid project depends is written here, but there is an inevitable pitfall in actual development that the Flutter project cannot debug Android code, as shown below: native Android project:

Flutter projects:

To solve this problem, the solution I came up with was to use aar mode for native Android development and module dependent development for the Flutter project.

Development mode parameter configuration

We can use a variable to control whether the current development environment is Android or Flutter, as follows:

A value in gradle.properties controls whether the flutter is developed on Android or not.

Add the following dependencies to the Android project’s project build.gradle:

The classpath 'com. Kezong: fat - the aar: 1.2.7'Copy the code

3. Add the following configuration to app’s build.gradle:

dependencies{
    def properties = new Properties()
    def pluginsFile = new File("${rootDir.getAbsolutePath()}/gradle.properties")
    if (pluginsFile.exists()) {
        pluginsFile.withReader('UTF-8') { reader -> properties.load(reader) }
    }
    def isFlutterDebug = properties.getProperty('isFlutterDebug').toBoolean()
    if (isFlutterDebug) {
        implementation project(':flutter')
    } else {
        implementation files('libs/flutter-release.aar')
//        implementation files('libs/flutter-debug.aar')
    }
}
Copy the code

4. Add the following parameters to gradle.properties to control debugging mode:

## Whether to debug flutter online isFlutterDebug=falseCopy the code

Gradle change the configuration to the following: xxX_flutter is the name of the own flutter project.

def properties = new Properties() def rootProjectFile = new File(settingsDir.getPath()).getAbsolutePath() def propertiesFile = new File("${rootProjectFile}/gradle.properties") if (propertiesFile.exists()) { propertiesFile.withReader('UTF-8') { reader -> properties.load(reader) } } def isFlutterDebug = properties.getProperty("isFlutterDebug").toBoolean() if (isFlutterDebug) { setBinding(new Binding([gradle: this])) evaluate(new File(settingsDir.parentFile, 'xxx_flutter/.android/include_flutter.groovy' )) include ':xxx_flutter' project(':xxx_flutter').projectDir = new File('.. /xxx_flutter') }else { 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, path -> def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile() include ":$name" project(":$name").projectDir = pluginDirectory } }Copy the code

This configuration allows you to control whether to develop the Flutter or Android.

conclusion

There are many more pits developed by Flutter, so this is a good place to record them.