Repository: flutter
Issue: Add a Flutter module to an Android library project
Labels: platform-android,tool,a: existing-apps,P3,team-android,triaged-android

Hello there.

For the past year, I've been able to build an Android library with an embedded Flutter module inside. The way I've done this is following the "Option B - Depend on the module’s source code" in the [Integrate a Flutter module](https://docs.flutter.dev/development/add-to-app/android/project-setup#option-b---depend-on-the-modules-source-code) guide.

I had to make some tweaks to my implementation and also modify the flutter.gradle file inside flutter_tools folder of the flutter SDK so it can compile inside a library type project. This workaround is really not the way I want to proceed since I guess it's not how it should be done.

Before all of that, I tried to use "Option A - Depend on the Android Archive (AAR)", but I had problems when exporting my own Android library with the Flutter module inside as an aar, since I couldn't make a proper fat aar.

**TLDR;** Is there a way to integrate a Flutter module into an Android Library project and not into an Android App project? How should I proceed?

EDIT 1: Add more information about my current implementation.

What I'm currently doing is as follows:

1. Run the command `flutter build aar` as mentioned in the Option A to generate AARs and POMs files to compile my flutter module (flutter_module).

NOTE: If I import the aar in my android library (android_library) and then import that into an android app (integration_example), I get runtime errors because Flutter classes can't be found. I guess this happens because android_library is the one that needs to get the flutter classes and not the intregration_example. Can't reproduce right now since that was my first implementation long time ago. Because of this:

2. Modify the POMs generated in step 1 to remove flutter references since we are going to import it into android_library with the Option B.
3. Install the flutter_module (which now has NOT Flutter included) on a maven repository (maven_local_repo).
4. Follow the Option B to import the flutter_module as a source code dependency in android_library.

NOTE: In this step, we get a gradle error since Flutter can't be embedded inside an android library this way. The error is: **Could not get unknown property 'applicationVariants' for extension 'android' of type com.android.build.gradle.LibraryExtension**
To workaround this, I modified the flutter.gradle file inside flutter/packages/flutter_tools/ folder with the following:

```
appProject.afterEvaluate {
            assert appProject.android != null
            project.android.libraryVariants.all { libraryVariant ->
                String variantBuildMode = buildModeFor(libraryVariant.buildType) //MOVED
                Task copyFlutterAssetsTask = addFlutterDeps(libraryVariant) //MOVED
                if (appProject.android.hasProperty("applicationVariants")) { //NEW
                    appProject.android.applicationVariants.all { appProjectVariant ->
                        Task appAssembleTask = getAssembleTask(appProjectVariant)
                        if (!shouldConfigureFlutterTask(appAssembleTask)) {
                            return
                        }
                        
                        if (buildModeFor(appProjectVariant.buildType) != variantBuildMode) {
                            return
                        }

                       //REMOVED variantbuildmode and copyFlutterAssetsTask lines

                        Task mergeAssets = project
                            .tasks
                            .findByPath(":${hostAppProjectName}:merge${appProjectVariant.name.capitalize()}Assets")
                        assert mergeAssets
                        mergeAssets.dependsOn(copyFlutterAssetsTask)
                    }
                }
                if(appProject.android.hasProperty("libraryVariants")) { //NEW
                    appProject.android.libraryVariants.all { appProjectVariant -> //NEW
                        Task appAssembleTask = getAssembleTask(appProjectVariant) //NEW
                        if (!shouldConfigureFlutterTask(appAssembleTask)) { //NEW
                            return //NEW
                        }
                        if (buildModeFor(appProjectVariant.buildType) != variantBuildMode) { //NEW
                            return //NEW
                        }
                        Task mergeAssets = project //NEW
                                .tasks
                                .findByPath(":${hostAppProjectName}:merge${appProjectVariant.name.capitalize()}Assets")
                        assert mergeAssets //NEW
                        mergeAssets.dependsOn(copyFlutterAssetsTask) //NEW
                    } //NEW
                } //NEW
            }
        }
```

5. Create android_library FAT AAR with Flutter SDK included aswell.
6. Modify android_library POMs to include flutter as root instead of flutter_module.flutter
7. Install the android_library (which now has Flutter included) on a maven repository (maven_local_repo).
7. Pull AARs from maven_local_repo to integration_example host app. We can include only android_library and this will work, since android_library includes flutter_module. So, the build.gradle on integration_example looks like:

```
repositories {
    // Development local repository
    maven {
        url '../../maven_local_repo'
    }
    maven {
        url 'https://storage.googleapis.com/download.flutter.io'
    }
}

...

dependencies {
   ...

    //SDK library
    debugImplementation 'com.example.sdk.android_library:android_debug:1.0.0'
    releaseImplementation 'com.example.sdk.android_library:android_release:1.0.0'
}
```