This article introduces the development process of the Flutter Plugin, including how to develop and release the Plugin using Android Studio.

This article describes how to develop the Android part of the Flutter Plugin. For information about Flutter and the concept of the Flutter Plugin, see the website.

Introduction to the

The author’s environment is developed by Android Studio on Mac, and AS is also promoted by Google. Compared with other ides, flutter development is much more convenient after the installation of the plugin, which comes with three templates:

  • The Application of a Flutter
  • Flutter Plugin: The Flutter Plugin
  • Flutter Package: Pure Dart components

A Plugin is simply a special Package. The Flutter Plugin provides the underlying encapsulation of Android or iOS and provides component functions on the Flutter layer, enabling Flutter to easily access Native modules. Many platform-dependent or complex aspects of Flutter implementation can be encapsulated as plugins. Here’s how it works

Messages are sent between client and host through platform channels, and communication between them is asynchronous.

Create components

Create a new Flutter Plugin project directly in Android Studio. This can also be done from the command line, for example, creating a Flutter_text_plugin.

flutter create –org com.example –plugin flutter_text_plugin

If you want to support swift or Kotlin, you can create it with the following command:

flutter create –org com.example –plugin -i swift -a kotlin flutter_text_plugin

For more parameter options, you can check the help documentation. Of course, it is still recommended to create AS directly, which is simple and intuitive. When you open the project with AS, you can see the organizational structure of the project

root
	android
	example
	ios
	lib
	...
Copy the code

The Android and ios folders are where we will write the native layer of the plug-in, and the lib folder is where we write the mapping with the Native layer. Native cannot communicate with flutter directly, but must be invoked indirectly through MethodChannel. The Example folder is the example project, in which plug-ins written can be verified directly. In this article, we’ll focus on the Android directory, the Android section.

Writing the Android section

Open the Flutter_text_plugin/Android project with AS, which makes development easier. However, after opening flutter, we found many errors, indicating that we could not find anything related to Flutter. When we looked at this project carefully, we found that there were many parts missing and the directory was different from the Android project we usually built with AS. This is because the Android project doesn’t need to be able to run directly, so it takes a lot out of things. But for the first time, it can be confusing, such as how to add third-party libraries, how to add ProGuard Rule, etc.

The introduction of flutter library

The Android plugin project did not introduce the FLUTTER library, so the error message appeared. We created a libs folder in the root directory of the project to store the Flutter library.

The Flutter library is in our Flutter SDK and the path is as follows

/bin/cache/artifacts/engine

Engine contains all the flutter libraries for various platforms. We can copy any Android Library into the libs folder. Right-click the flutter. .

After this step, no more errors will be reported in the Project. However, since the whole Flutter plugin contains the flutter library, it cannot simply be added. In flutter_text_plugin Dependencies, change the Scope of flutter library from Implementation to Compile Only. At this point, the introduction of the Flutter library is complete and the plugin can be written.

Adding third-party Libraries

There are two ways to add third-party libraries, one is jar packages, and the other is gradle. This step is much easier due to the introduction of the first flutter library. If you look at the build.gradle file, you can see the following information at the bottom.

dependencies {
  compileOnly files('libs/flutter.jar')}Copy the code

This makes it clear that adding static libraries as well as online libraries can be done here. For example, I add a bugly static library and the OKHttp3 library:

dependencies {
  compileOnly files('libs/flutter.jar')
  implementation 'com. Squareup. Okhttp3: okhttp: 3.10.0'
  implementation files('libs/bugly_crash_release.jar')}Copy the code

Add proguard rule

Because of the Bugly and okHttp3 libraries, progurad rule needs to be added. Proguard-rules. pro file is not found in the project, so we need to create this step ourselves. In the root directory, create proGuard-rules. pro file, add the obturation rule, then modify the build.gradle file, add the following information: Similar to normal Android projects:

buildTypes {
    release {
      minifyEnabled true
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}Copy the code

The Android permission

Add bugly and okhttp3 library, need corresponding permission statement, can run normally. Add the corresponding permissions directly in the manifest file

  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.READ_LOGS"/>
Copy the code

Plug-in development

Now that everything is ready, you can think of the project as a standalone Android project, encapsulate it, and expose the interface in the FlutterTestPlugin file. It is associated with the flutter layer through platform channels.

release

When the plug-in is developed, you can release it for others to use. Make sure that the pubspec.yaml, readme. md, and Changellog. md files are filled out correctly before Posting. You can use the dry-run command to see if you are ready.

flutter packages pub publish –dry-run

After checking, run the following command to publish the information to the Pub.

flutter packages pub publish

How to reference

There are two types of references to plug-ins, published and unpublished.

Reference published libraries

The resource management of the Flutter project is in the root directory of pubspec.yaml. It is similar to the package management of js.

Dependencies: url_launcher: ^ 0.4.2Copy the code

If the library contains something platform-specific, such as something that needs to be used in the Native layer, it needs to be referenced separately in the corresponding Native project.

Android

Add a reference to dependencies in android/build.gradle.

dependencies {
        provided rootProject.findProject(":url_launcher")}Copy the code

iOS

Modify the ios/hello.podspec file

Pod::Spec.new do |s|
  # lines skipped
  s.dependency 'url_launcher'
Copy the code

Reference to conflicts

Referencing different libraries can lead to conflicts, such as plug-ins A and B, which both contain C plug-ins but require different versions. Therefore, we can take the following measures to avoid this problem:

  • Try to use scope versions rather than specifying a specific version.
  • Enforce the unification of conflicting plug-in versions
  • For native layer, Android can use the force command to force the version, while iOS does not support the override function.

Reference unpublished libraries

There are two ways to reference an unpublished library, using a local path or git address:

Path-based references:

This approach is mainly for local unpublished libraries and can refer to relative or absolute paths.

dependencies: plugin1: path: .. /plugin1/Copy the code

Git based reference:

This approach works for libraries hosted on Git, where path is optional and can be located to a subdirectory

dependencies:
  package1:
    git:
      url: git://github.com/flutter/packages.git
      path: packages/package1   
Copy the code

extension

This article introduces the Flutter platform, gives a brief analysis of the Flutter platform technology, and investigates the feasibility of Flutter in a commercial environment from multiple dimensions.

The latter

The author has created a project related to flutter learning. The github address contains some articles about flutter learning written by the author. There will be regular updates in the future and some learning demos will also be uploaded.

reference

  1. Flutter Advancements – platform plugin
  2. Flutter – Creating a Plugin
  3. Flutter for Android Developers
  4. Writing custom platform-specific code with platform channels
  5. Developing Packages & Plugins
  6. Using Packages