Fluttify: fluttify.com
Series of articles:
Dart interface to generate engine Fluttify
How to use Fluttify to develop a new Flutter plugin
Fluttify output Flutter plugin works in detail
(4) Introduction to the principle of Fluttify compiler
Use Fluttify to generate the plug-in
Generating Fluttify artifacts from the native SDK is essentially a build process, so Fluttify takes the form of a Gradle plug-in. Gradle. Run gradle Fluttify and enter the necessary parameters to generate a Flutter plugin corresponding to the native SDK. This article will use the Aurora Statistics SDK as an example.
Quick start
Take the JAnalytics SDK initialization method as an example:
- Go to fluttify.com to generate plug-ins;
- in
./lib
New folderdart
Folder (this step is optional so as not to mess up the folder); - create
./dart/janalytics_service.dart
File (name whatever you like, just find a place to put the initialization code); - Go to the integration guide to find the initialization code: 4.1android:
JAnalyticsInterface.init(Context context);
; 4.2 ios:[JANALYTICSService setupWithConfig:config];
; - Write the Dart code:
import 'package:flutter/cupertino.dart';
import 'package:janalytics_fluttify/src/android/android.export.g.dart'; // This is the generated code
import 'package:janalytics_fluttify/src/ios/ios.export.g.dart'; // This is the generated code
class JAnalyticsService {
static Future<void> init({@required String iosKey}) async {
// the 'platform' method encapsulates some logic that makes it easy to call multiple platforms. If you like, you can just call each platform if else
await platform(
The pool argument is a collection of native objects that need to be freed. Native objects are automatically freed after platform methods are executed, as described below
android: (pool) async {
await cn_jiguang_analytics_android_api_JAnalyticsInterface
.init(await android_app_Application.get());
},
ios: (pool) async {
final config = await JANALYTICSLaunchConfig.create__();
await config.set_appKey(iosKey);
await config.set_channel('developer-default');
awaitJANALYTICSService.setupWithConfig(config); pool.add(config); }); }}Copy the code
- A method of the plug-in is written.
You can see that with Fluttify, it’s a lot easier to write a plugin. When you want to write a method, look up the official documentation and write the corresponding Dart code, completely masking the native interface. You don’t have to open Android Studio and Xcode again and again, you don’t have to jump back and forth between native and Dart, you don’t have to write a bunch of fields to pass objects between Dart and native, Fluttify gives you a good Dart interface for native things, That’s the goal of Fluttify.
Next, take a look at the structure of the generated plug-in and how to invoke the generated interface.
Fluttify product engineering structure
The product of Fluttify is a standard Plugin for Flutter, so the structure above the Lib folder is the same as the normal plugin. The Lib folder will be divided into Android and ios folders, and Dart classes (enumeration/interface, etc.) corresponding to classes (enumeration/interface, etc.) in SDK of each platform will be placed respectively. Android /ios folders will also generate their own:
function.g.dart
File: all top-level functions generated;type_op.g.dart
Files: All of themasandisMethod, used to judge the type and shape;ios/android.export.g.dart
Files: Export all ios/ Android types;platformview
Folder: All generatedPlatformView
;
It is customary to add a dart folder to the lib folder, place the code that abstracts each platform, and finally export only the files in that folder.
Lib folder Structure Overview:
. ├ ─ ─ janalytics_fluttify. Dart └ ─ ─ the SRC ├ ─ ─ android │ ├ ─ ─ android. Export. G.d art │ ├ ─ ─ cn... Android corresponding dart end interface │ └ ─ ─ type_op. G.d art ├ ─ ─ the dart │ └ ─ ─ janalytics_service. Dart └ ─ ─ ios ├ ─ ─ JANALYTICSBrowseEvent. G.d art ├ ─ ─... ├── ios. Export. ├─ type_opCopy the code
Basic operation
Create an object
Creating objects in Fluttify primarily uses the create method. This method is provided on the DART side as a static asynchronous method xx.create__xx, where XX is the constructor argument. Like:
class JANALYTICSLaunchConfig extends NSObject {
static Future<JANALYTICSLaunchConfig> create__() async {
/ /... Call the native create object}}Copy the code
If the constructor has no arguments, then xx.create__ is directly used, followed by two underscores to avoid conflicts with the CREATE method in the SDK.
A method is called
All methods in Fluttify are asynchronous, so if you want to look like synchronous code, you need to await every call. There’s not much else to say, because the point of Fluttify is to make native calls look exactly like DART calls.
What operations are supported?
Fluttify generates public classes, public methods, and constants in the SDK.
infrastructure
All Fluttify generated projects rely on an infrastructure plug-in, foundation_Fluttify.
This plug-in provides the DART interface to the system classes and is handwritten. I have tried to generate android. Jar and ios system framework directly. Although it can be achieved theoretically, the effect is not good, and the generated files are very large.
Foundation_fluttify also provides convenience methods, such as the Platform method to simplify multi-platform calls, the kNativeObjectPool to hold dart references to native objects, and more.
The life cycle of a native object
Any native objects that fluttiy-generated Dart code generates during invocation are placed into a global key-value pair type called HEAP, which is HashMap on Android and NSDictionary on ios. Because these objects are stored in global variables, they are strongly referenced and cannot be released unless manually released. The Ref class (DART) in Foundation_fluttify has a release method that removes the corresponding native object from the HEAP, removing strong references.
conclusion
That’s it for developing a plug-in using Fluttify. Janalytics_fluttify has been uploaded to Fluttify-Project. If you are interested in this plug-in, please contact me ([email protected]). I can set you as the maintainer, and of course I will further develop this plug-in when I have free time.
If you want to generate plug-ins, please feel free to contact me. Fluttify is still in the beta stage and will not charge any fees. If you have any feedback, you can submit an issue to Fluttify-feedback.