flutter插件开发

文章目录

      • Android
        • 1. Open for Editing in Android Studio
        • 2. new module 创建插件
        • 3. 修改build.gradle
        • 4. 编写插件类
        • 5. 编写插件注册类
        • 6. 注册插件
        • 7. 编写dart
        • 8. dart中使用
      • IOS
        • FlutterPlugin.h
        • FlutterPlugin.m
        • SwiftFlutterPlugin.swift
      • 其他
        • 检查插件
        • 发布插件
        • 依赖包冲突
        • 打包存在多个一样的so文件,冲突

Android

1. Open for Editing in Android Studio
2. new module 创建插件
3. 修改build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'


def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
    compileSdkVersion 28


    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}
flutter {
    source '../..'
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

4. 编写插件类
package com.ityu.flutter_plugin

import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

class FlutterPlugin: MethodCallHandler {
  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
      val activity = registrar.activity()
      val channel = MethodChannel(registrar.messenger(), "flutter_plugin")
      channel.setMethodCallHandler(FlutterPlugin())
    }
  }

  override fun onMethodCall(call: MethodCall, result: Result) {
    if (call.method == "getPlatformVersion") {
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
    } else {
      result.notImplemented()
    }
  }
}

5. 编写插件注册类
package io.flutter.plugins;

import io.flutter.plugin.common.PluginRegistry;
import com.ityu.flutter_plugin.FlutterPlugin;

/**
 * Generated file. Do not edit.
 */
public final class GeneratedPluginRegistrant {
  public static void registerWith(PluginRegistry registry) {
    if (alreadyRegisteredWith(registry)) {
      return;
    }
    FlutterPlugin.registerWith(registry.registrarFor("com.ityu.flutter_plugin.FlutterPlugin"));
  }

  private static boolean alreadyRegisteredWith(PluginRegistry registry) {
    final String key = GeneratedPluginRegistrant.class.getCanonicalName();
    if (registry.hasPlugin(key)) {
      return true;
    }
    registry.registrarFor(key);
    return false;
  }
}

6. 注册插件
class MainActivity: FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
  }
}
7. 编写dart
class FlutterPlugin {
  static const MethodChannel _channel =
      const MethodChannel('flutter_plugin');

  static Future get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future  getString() async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
}
8. dart中使用
String platformVersion = await FlutterPlugin.platformVersion;

IOS

FlutterPlugin.h
#import 

@interface FlutterPlugin : NSObject<FlutterPlugin>
@end

FlutterPlugin.m
#import "FlutterPlugin.h"
#import 

@implementation FlutterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  [SwiftFlutterPlugin registerWithRegistrar:registrar];
}
@end
SwiftFlutterPlugin.swift
import Flutter
import UIKit

public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "flutter_plugin", binaryMessenger: registrar.messenger())
    let instance = SwiftFlutterPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    result("iOS " + UIDevice.current.systemVersion)
  }
}

其他

检查插件

flutter packages pub publish --dry-run

发布插件

flutter packages pub publish

依赖包冲突

多个依赖都引用第三方依赖
直接在下面引用这个第三方依赖 优先级最高

打包存在多个一样的so文件,冲突
packagingOptions {
    pickFirst '**/libstlport_shared.so'
}

你可能感兴趣的:(flutter)