Find out how to Android Integration in native projects Flutter

Specific operation

Build configuration
Execute the command at the root of the native project flutter create -t module --org {package_name} {module_name} // here
module_name Follow your orders Android son module Just name it . There must be no middle line . // such as , flutter create -t
module --org com.engineer.mini.flutter flutter_sub // here module_name Follow your orders
Android son module Just name it . There must be no middle line . // such as , flutter create -t module --org
com.engineer.mini.flutter flutter_sub </pre>
result
<pre class="prettyprint hljs groovy" style="padding: 0.5em; font-family:
Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68,
68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size:
14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word;
white-space: pre; background-color: rgb(246, 246, 246); border: none;
overflow-x: auto;">Creating project sub_flutter...
sub_flutter/test/widget_test.dart (created) sub_flutter/sub_flutter.iml
(created) sub_flutter/.gitignore (created) sub_flutter/.metadata (created)
sub_flutter/pubspec.yaml (created) sub_flutter/README.md (created)
sub_flutter/lib/main.dart (created) sub_flutter/sub_flutter_android.iml
(created) sub_flutter/.idea/libraries/Dart_SDK.xml (created)
sub_flutter/.idea/modules.xml (created) sub_flutter/.idea/workspace.xml
(created) Running "flutter pub get" in sub_flutter... 1,054ms Wrote 11 files.
</pre>
Finally, the above documents are generated , Notice the end here   Automatically flutter pub get  Your orders . about flutter pub get What did you do , You can refer to the following .

Here, create a child in the root of the project module Just to put the code in a warehouse , Easy maintenance , In theory, it can be placed anywhere on the hard disk .

Configure native project settings.gradle

In configuration settings.gradle Let's first briefly review the Gradle Some basic knowledge of .

If you know Gradle Related configuration , There must be a concept , namely   Convention is better than configuration  , What do you mean , According to the idea of object-oriented to understand , Every project is a huge one
Project class , There are many properties in the whole class . And every project we create is actually a concrete project Project object ( That's an example ). Agreement is better than configuration , That's right
project When instantiating , Its internal properties already have default values . So how do we know what the default values are ? Execute in the project root directory
./gradlew properties
You can get the whole thing Project Some default configurations of , such as ( Some results are excerpted here )
------------------------------------------------------------ Root project
------------------------------------------------------------ allprojects: [root
project 'MiniApp', project ':app', project ':thirdlib']
android.agp.version.check.performed: true android.enableJetifier: true
android.enableR8: true android.enableR8.libraries: true android.useAndroidX:
true buildDir: /Users/username/Documents/mygithub/MinApp/build buildFile:
/Users/username/Documents/mygithub/MinApp/build.gradle projectDir:
/Users/username/Documents/mygithub/MinApp rootDir:
/Users/username/Documents/mygithub/MinApp rootProject: root project 'MiniApp'
Here are some of our current configurations , such as useAndroidX, But there are also agreements , such as For the whole project For buildDir It's the root of the project
build Folder, etc .

implement
./gradlew :app:properties
Excerpts of some results
buildDir: /Users/username/Documents/mygithub/MinApp/app/build buildFile:
/Users/username/Documents/mygithub/MinApp/app/build.gradle
You'll get information about it app whole module Some configuration information at this stage , Of course, the configuration information is different from the Convention , And your own configuration , such as buildToolsVersion
, Signature and other related information . You can see that buildDir And the whole thing project It's not the same .

Back to the theme , Let's see how to put the sub_flutter Modules are integrated into the project .( Strictly speaking, it's not integration sub_flutter modular , Because he's just one
flutter Module for , And in Android The main project can only integrate sub projects Android module, So how to make specific changes , Let's take a look at the mystery )

According to the official method of operation , We will be asked to add the following configuration to the settings.gradle in .
// 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
First look there settingsDir Value of . stay settings.gradle Add directly in
println "settings.dir=" + settingsDir println "settings.dir.parent=" +
settingsDir.parent
sync Then you see the output
settings.dir=/Users/username/Documents/mygithub/MinApp
settings.dir.parent=/Users/username/Documents/mygithub
therefore , Configuration information above , That is to say, combination settings The parent directory of the directory is combined with our configured directory , Found one named include_flutter.groovy
File for , Then go and execute him .

I said that , Create child module When I was young , It can be in the project root directory , It can also be in other places , If it's in another location , there my_flutter
It can be replaced by the absolute strength of your directory .

This is created directly in the root directory , Then the above configuration can be simplified to
setBinding(new Binding([gradle: this])) evaluate(new File(settingsDir,
'sub_flutter/.android/include_flutter.groovy')) include ':sub_flutter' #### about
include_flutter.groovy
It says so ,settings.gradle Configuration of , In fact, it is to carry out include_flutter.groovy This document , You can take a brief look at this file
def scriptFile = getClass().protectionDomain.codeSource.location.toURI() def
flutterProjectRoot = new File(scriptFile).parentFile.parentFile gradle.include
":flutter" gradle.project(":flutter").projectDir = new File(flutterProjectRoot,
".android/Flutter") def localPropertiesFile = new File(flutterProjectRoot,
".android/local.properties") def properties = new Properties() assert
localPropertiesFile.exists(), ":exclamation:️The Flutter module doesn't have a
`$localPropertiesFile` file." + "\nYou must run `flutter pub get` in
`$flutterProjectRoot`." localPropertiesFile.withReader("UTF-8") { reader ->
properties.load(reader) } def flutterSdkPath =
properties.getProperty("flutter.sdk") assert flutterSdkPath != null,
"flutter.sdk not set in local.properties" gradle.apply from:
"$flutterSdkPath/packages/flutter_tools/gradle/module_plugin_loader.gradle"
.android In fact, it is one Android project , He contains one Flutter folder , this Flutter It's a library Type Android
module , This point comes from his build.gradle As can be seen from the documents . include_flutter.groovy What we did , It's about putting the current
library Named flutter One of them moudle. Then check the project local.properties in sdk Related configuration of , Finally, it's time to execute
FlutterSDK Of gradle script , Here, the specific analysis is no longer carried out .

in other words , Now there's one called flutter Of Android Library Module . this module contain flutter
All configurations of . If we rely on this module , So it's like dependence Flutter .

rely on flutter

Finally, in the context of the native project application-module Of build.gradle Of dependencies Add to closure
implementation project(':flutter')
thus , Native projects already exist Flutter The dependence of , have access to Flutter Of View It's over .

thus , Now the native project contains Flutter SDK It's all dependent on you ,UI Related content , How to write or use dart stay main.dart
Written in Chinese , And then we can take this dart The rendered content is in accordance with the Activity ,Fragment or View Added to an existing project in the form of .

flutter pub get

flutter pub get perhaps pub get What are you doing flutter When using a third party lib
You often use a command when you update a file or version , This command will pull related dependencies , In fact, this command will be generated automatically Android and iOS Native project of . For example, when we created it
sub_flutter In the module , Are generated automatically .android and .ios Native project directory for . At the same time, these two directories start with a dot , In general, it is to hide the file , At the same time
.gitignore The file can also be seen , about flutter module In form , Both of these folders are ignored , after all flutter module
The core of , Or for convenience module Integrated into native projects in the form of , Two native directories inside , One is to facilitate integration , On the other hand, it is easy to run directly hot-reload
Debugging of .

Technology