添加flutter到已有工程

https://docs.flutter.dev/development/add-to-app/android/project-setup#add-the-flutter-module-as-a-dependency

option 1:
打包flutter工程为aar倒入已有工程(方便没有dart-sdk的开发)

Option A – Depend on the Android Archive (AAR)
This option packages your Flutter library as a generic local Maven repository composed of AARs and POMs artifacts. This option allows your team to build the host app without installing the Flutter SDK. You can then distribute the artifacts from a local or remote repository.

Let’s assume you built a Flutter module at some/path/my_flutter, and then run:

content_copy
cd some/path/my_flutter
flutter build aar
Then, follow the on-screen instructions to integrate.

More specifically, this command creates (by default all debug/profile/release modes) a local repository, with the following files:

content_copy
build/host/outputs/repo
└── com
└── example
└── my_flutter
├── flutter_release
│ ├── 1.0
│ │ ├── flutter_release-1.0.aar
│ │ ├── flutter_release-1.0.aar.md5
│ │ ├── flutter_release-1.0.aar.sha1
│ │ ├── flutter_release-1.0.pom
│ │ ├── flutter_release-1.0.pom.md5
│ │ └── flutter_release-1.0.pom.sha1
│ ├── maven-metadata.xml
│ ├── maven-metadata.xml.md5
│ └── maven-metadata.xml.sha1
├── flutter_profile
│ ├── …
└── flutter_debug
└── …
To depend on the AAR, the host app must be able to find these files.

To do that, edit app/build.gradle in your host app so that it includes the local repository and the dependency:

content_copy
android {
// …
}

repositories {
maven {
url ‘some/path/my_flutter/build/host/outputs/repo’
// This is relative to the location of the build.gradle file
// if using a relative path.
}
maven {
url ‘https://storage.googleapis.com/download.flutter.io’
}
}

dependencies {
// …
debugImplementation ‘com.example.flutter_module:flutter_debug:1.0’
profileImplementation ‘com.example.flutter_module:flutter_profile:1.0’
releaseImplementation ‘com.example.flutter_module:flutter_release:1.0’
}
Important: If you’re located in China, use a mirror site such as https://[a mirror site]/download.flutter.io rather than the storage.googleapis.com domain directly. See our Using Flutter in China page for information on mirrors.

Tip: You can also build an AAR for your Flutter module in Android Studio using the Build > Flutter > Build AAR menu.

Your app now includes the Flutter module as a dependency. You can follow the next steps in the Adding a Flutter screen to an Android app.

option 2:源码方式导入已有工程(必须有dart-sdk的开发)
Option B – Depend on the module’s source code
This option enables a one-step build for both your Android project and Flutter project. This option is convenient when you work on both parts simultaneously and rapidly iterate, but your team must install the Flutter SDK to build the host app.

Tip: By default, the host app provides the :app Gradle project. To change the name of this project, set flutter.hostAppProjectName in the Flutter module’s gradle.properties file. Finally, include this project in the host app’s settings.gradle file mentioned below.

Include the Flutter module as a subproject in the host app’s settings.gradle:

content_copy
// Include the host app project.
include ‘:app’ // assumed existing content
setBinding(new Binding([gradle: this])) // new
evaluate(new File( // new
settingsDir.parentFile, // new
‘my_flutter/.android/include_flutter.groovy’ // new
)) // new
Assuming my_flutter is a sibling to MyApp.

The binding and script evaluation allows the Flutter module to include itself (as :flutter) and any Flutter plugins used by the module (as :package_info, :video_player, etc) in the evaluation context of your settings.gradle.

Introduce an implementation dependency on the Flutter module from your app:

content_copy
dependencies {
implementation project(‘:flutter’)
}
Your app now includes the Flutter module as a dependency. You can follow the next steps in the Adding a Flutter screen to an Android app.