一部分的知识学习记录
一 Flutter插件(Plugin)
首先使用Android Studio 创建一个Flutter的插件开发项目。以项目名为flutter_plugin例,创建好之后整个项目的目录结构应该是:
#flutter_plugin的所有二级目录
├── CHANGELOG.md
├── LICENSE
├── README.md
├── analysis_options.yaml
├── android
│ ├── build.gradle
│ ├── gradle
│ ├── gradlew
│ ├── gradlew.bat
│ ├── libs
│ ├── local.properties
│ ├── settings.gradle
│ └── src
├── build
│ └── ios
├── example
│ ├── README.md
│ ├── analysis_options.yaml
│ ├── android #开发安卓部分
│ ├── build
│ ├── images
│ ├── integration_test
│ ├── ios #开发iOS部分的代码
│ ├── lib
│ ├── pubspec.lock
│ ├── pubspec.yaml #这块是你的第三方的依赖
│ └── test
├── images
│ ├── 2.0x
│ ├── 3.0x
│ └── a_logo_1.png
├── ios
│ ├── Assets
│ ├── Classes
│ ├── Framework
│ ├── Gemfile
│ ├── Gemfile.lock
│ ├── Resources
│ └── flutter_plugin.podspec
├── lib
│ ├── flutter_plugin.dart
│ ├── flutter_plugin_method_channel.dart
│ └── flutter_plugin_platform_interface.dart #实际开发先在这里进行
├── pubspec.lock
├── pubspec.yaml
└── test
├── flutter_plugin_method_channel_test.dart
└── flutter_plugin_test.dart
#开发完成后,直接删除example文件夹,就可以本地引入或者发布到pub.dev
1.安卓插件开发
1.1 注意点
需要打开的路径是在使用Android Studio打开flutter_plugin/example/android目录,这是原生的安卓项目,同样iOS的原生项目在example/ios目录下。在使用AS打开项目之后,等待gradle的同步完成。如果没有任何报错,那就进行开发即可。如果报错了,一步一步解决。
使用 Flutter Doctor 检查配置环境,找到app/build.gradle,修改其中的配置等等。
buildscript { }
allprojects{ }
android {
// 配置源代码集
sourceSets {
// 添加Kotlin源代码目录
main.java.srcDirs += "src/main/kotlin"
test.java.srcDirs += "src/test/kotlin"
main {
jniLibs.srcDirs = ['libs'] #可以在此处配置你的aar包 你的libs目录在 flutter_plugin/android/libs
}
}
}
1.2 插件开发完成后,在你的 yaml文件中 导入flutter的本地插件。
name: demoplugin
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application
version: 1.0.0+1
environment:
# flutter的版本设置
sdk: '>=3.1.3 <4.0.0'
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.0.2
# 依赖的pub.dev上的库
# 检查网络的插件
connectivity_plus: 5.0.2
# icon的插件
cupertino_icons: ^1.0.8
# 网络请求的插件
dio: ^5.8.0
# Toast的插件
flutter_easyloading: 3.0.5
# 存储的插件
get_storage: 2.1.1
# 数据流
get: 4.6.6
# 依赖本地插件 下面这个相对路径 直接在主项目平行路径
flutter_plugin:
path: ./flutter_plugin
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^2.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter packages.
flutter:
# 图片资源
assets:
- images/logo_1.png
- images/2.0x/logo_1@2x.png
- images/3.0x/logo_1@3x.png
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
2.iOS插件开发
2.1 打开example/ios执行pod install之后,找到Runner.xcworkspace打开。修改flutter_plugin.podspec文件中的配置。
# 对Podspec 格式或者语法有疑问,查看 http://guides.cocoapods.org/syntax/podspec.html.
Pod::Spec.new do |s|
s.name = 'flutter_plugin' #名称
s.version = '0.0.1' #版本号
s.summary = '标题描述'
s.description = <<-DESC
描述项目文字必须比标题长
DESC
s.homepage = 'https://www.aaaa.com' #主页
s.license = { :file => '../LICENSE' } #开源协议
s.author = { ' Company' => '@aaa.com' } #作者
s.source = { :path => '.' } # 源文件路径
s.source_files = 'Classes/**/*' #源文件格式
s.dependency 'Flutter' #第三方依赖
s.platform = :ios, '15.0' #iOS 或者是 Macos版本
s.vendored_frameworks = 'Framework/Sdk.framework' #本地依赖的SDK的路径和名称!! 此处建议编译或者你的库为通用的xcFrameWork,这样不会影响模拟器运行
# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } #编译时的设定
s.swift_version = '5.0' #swift版本号
# If your plugin requires a privacy manifest, for example if it uses any
# required reason APIs, update the PrivacyInfo.xcprivacy file to describe your
# plugin's privacy impact, and then uncomment this line. For more information,
# see https://developer.apple.com/documentation/bundleresources/privacy_manifest_files
# s.resource_bundles = {'flutter_plugin_privacy' => ['Resources/PrivacyInfo.xcprivacy']} # 隐私文件
end
Podspec文件是iOS的Pod的核心配置,Flutter的不管是项目还是插件都是以Pod的组件化将Flutter作为一个模块嵌入到整个iOS的工程中的。所以iOS的项目和Flutter部分是完美兼容的,你可以只用Flutter渲染页面,然后使用Flutter 的Channel和原生交互,可以使得你的iOS项目开发效率大大的加快。当然,UI渲染Flutter肯定是不能还原原生Native100%的。
二 UNI-APP插件
三 RN插件(RN Module)
四 Ionic/Cordova 的插件
五 SDK的打包(aar和xcFrameWork)