Android Flutter plugin development

Background Note

Flutter’s official library support is limited (mostly ui framework docking platform native rendering service), if you need more support related to native platform capabilities, such as camera, recording, viewing device information, etc., you need to manually develop plug-in support (different platform native provides implementation , the dart side provides an interface and exposes it)

1. Environment preparation

AS2021.3.1
Flutter sdk 3.3.10

2. Create a Flutter Plugin project

Method 1: AS creation

insert image description here

insert image description here
insert image description here
An error may be reported after clicking finish directly here

insert image description here
At this time, if you use AS to open the generated project, an error will be reported, and the reason is unknown.

Change to method 2

Method 2: Command line creation

G:\MyWork\Flutter\Projects>G:\MyWork\Flutter\Tools\flutter\bin\flutter.bat create --org com.example --platforms=android,ios,windows --template=plugin flutter_plugin_demo
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Creating project flutter_plugin_demo...
Running "flutter pub get" in flutter_plugin_demo...                907ms
Running "flutter pub get" in example...                            804ms
ERROR_ACCESS_DENIED file system exception thrown while trying to create a symlink from source to dest

G:\MyWork\Flutter\Tools\flutter\bin\flutter.bat create --org com.example --platforms=android,ios,windows --template=plugin flutter_plugin_demo

AS opens the project flutter_plugin_demo, which is displayed normally at this time

Directory structure of the Flutter Plugin project

insert image description here

3. Add plug-in function

First look at the plug-in module (corresponding to 1, 3)

Plug-in module-android platform side (corresponding to 1)

only one class

insert image description here

Plug-in module-dart side (corresponding to 3)

insert image description here
insert image description here

Look at the plug-in module (corresponding to 2)

How to reference the plugin module in pubspec.xml See the next section "Deploying the Flutter Plugin Project"

Directly see how the dart-side code is called

insert image description here

Here is another example of how to use the plug-in module to use the plug-in, in two ways

insert image description here

Summarize:

The above are commonly used plug-in module development methods online

From the perspective of the plug-in user
, compared with calling the plug-in function directly with MethodChannel, it is more engineering and elegant to publish and be referenced by the plug-in module. The module using the plug-in does not need to remember the declared plug-in name and plug-in method, only needs to call the dart instance object method can

4. Publish the Flutter Plugin project

4.1 Local

Two pubspec files, corresponding to the previous 4, 5

pubspec for plugin modules

insert image description here

  # This section identifies this Flutter project as a plugin project.
  # The 'pluginClass' specifies the class (in Java, Kotlin, Swift, Objective-C, etc.)
  # which should be registered in the plugin registry. This is required for
  # using method channels.
  # The Android 'package' specifies package in which the registered class is.
  # This is required for using method channels on Android.
  # The 'ffiPlugin' specifies that native code should be built and bundled.
  # This is required for using `dart:ffi`.
  # All these are used by the tooling to maintain consistency when
  # adding or updating assets for this project.

The general content is as follows:
1. This is the declaration section of the plug-in module
2. pluginclass indicates the class of native capabilities (different native platforms and different development languages)
3. It is used to register plug-ins for plug-in modules (in fact, the android gradle compilation plug-in of flutter is in If the plug-in module is found to declare pluginclass, a class GeneratedPluginRegistrant will be dynamically reflected)

Plugins use the module's pubspec

insert image description here
At this time, the plug-in will automatically generate a class GeneratedPluginRegistrant using module compilation

insert image description here
This class is the pluginclass declared in the previous plug-in module pubspec. The specific android platform class GeneratedPluginRegistrant instantiation object is initialized and executed by the Java layer FlutterEngine. The plug-in module android side does not need to write the plug-in registration code

Summarize

Therefore, for local deployment, it is required to use the plug-in module to refer to the correct root directory path of the plug-in module.

4.2 pub official website release

slightly

4.3 Private warehouse release

slightly

Guess you like

Origin blog.csdn.net/weixin_41548050/article/details/129947907