Create the plug-in

flutter create --platforms=android,ios --template=plugin native_add
Copy the code

Add C source code files to the plug-in project

Add the C source file to the ios/Classes folder of the plug-in project, such as native_add.c:

int native_add(int a, int b)
{
    return a + b;
}
Copy the code

Configure iOS automatic compilation

Under open the plug-in with the Xcode project example/ios/Runner xcworkspace, right-click on the Runner in the Xcode folder, through the Add Files to “Runner” Add native_add. C file.

Configure Android automatic compilation

Create the following cmakelists.txt file in the Android folder of the plugin project:

cmake_minimum_required(VERSION 3.4.1)
add_library( native_add SHARED .. /ios/Classes/native_add.cpp )Copy the code

Add the following externalNativeBuild section to the build.gradle file in the same directory:

android {
  // ...
  externalNativeBuild {
    cmake {
      path "CMakeLists.txt"
    }
  }
  // ...
}
Copy the code

Automatically generate FFI bindings

Add header files to the plug-in project, such as native_add.h to the plug-in project root directory:

int native_add(int a, int b);
Copy the code

Add ffigen to dev_dependencies of the plug-in project pubspec.yaml and add ffigen configuration information to pubspec.yaml:

ffigen:
  name: 'NativeAdd'
  llvm-path:
    - 'LLVM path'
  output: 'lib/generated_bindings.dart'
  headers:
    entry-points:
      - 'native_add.h'
Copy the code

Dart Run ffigen automatically generates the FFI binding (if an error occurs, try running the flutter Clean first).

Load the compiled library

You can replace the plug-in project’s lib/native_add.dart with something like this:

import 'dart:ffi';
import 'dart:io';
import 'package:native_add/generated_bindings.dart';

final nativeAdd = NativeAdd(Platform.isAndroid
    ? DynamicLibrary.open('libnative_add.so')
    : DynamicLibrary.process());
Copy the code

Dart: /lib/main. Dart: /lib/main. Dart: /lib/main.

import 'package:flutter/material.dart';
import 'package:native_add/native_add.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('1 + 2 = ${nativeAdd.native_add(1.2)}'),),),); }}Copy the code

Run it to see if it works.

Call the plugin in the Flutter project

Add plugin dependencies:

dependencies:
 #...
 native_add:
   path: The plugin path
Copy the code

Call plug-in function:

import 'package:flutter/material.dart';
import 'package:native_add/native_add.dart';

void main() {
 runApp(MyApp());
}

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     home: Scaffold(
       appBar: AppBar(
         title: const Text('App'),
       ),
       body: Center(
         child: Text('1 + 2 = ${nativeAdd.native_add(1.2)}'),),),); }}Copy the code