1. 创建要发布的插件
创建项目一般有2中方法
1) 用命令创建
flutter create -t plugin helloworld
你也可以通过 flutter create -h
命令来查看flutter支持的所有命令:
-h, --help Print this usage information.
--[no-]pub Whether to run "flutter pub get" after the project has been created.
(defaults to on)
--[no-]offline When "flutter pub get" is run by the create command, this indicates
whether to run it in offline mode or not. In offline mode, it will need
to have all dependencies already available in the pub cache to succeed.
--[no-]with-driver-test Also add a flutter_driver dependency and generate a sample 'flutter
drive' test.
-t, --template= Specify the type of project to create.
[app] (default) Generate a Flutter application.
[module] Generate a project to add a Flutter module to an existing Android or iOS
application.
[package] Generate a shareable Flutter project containing modular Dart code.
[plugin] Generate a shareable Flutter project containing an API in Dart code with
a platform-specific implementation for Android, for iOS code, or for
both.
-s, --sample= Specifies the Flutter code sample to use as the main.dart for an
application. Implies --template=app. The value should be the sample ID
of the desired sample from the API documentation website
(http://docs.flutter.dev). An example can be found at
https://master-api.flutter.dev/flutter/widgets/SingleChildScrollView-cla
ss.html
--list-samples= Specifies a JSON output file for a listing of Flutter code samples that
can created with --sample.
--[no-]overwrite When performing operations, overwrite existing files.
--description The description to use for your new Flutter project. This string ends up
in the pubspec.yaml file.
(defaults to "A new Flutter project.")
--org The organization responsible for your new Flutter project, in reverse
domain name notation. This string is used in Java package names and as
prefix in the iOS bundle identifier.
(defaults to "com.example")
--project-name The project name for this new Flutter project. This must be a valid dart
package name.
-i, --ios-language [objc, swift (default)]
-a, --android-language [java, kotlin (default)]
--[no-]androidx Generate a project using the AndroidX support libraries
(defaults to on)
2) 直接用android studio创建(推荐)
File->New->New Flutter Project...
然后选择 plugin 就好
3) 发布配置
修改项目的 pubspec.yaml 文件, 添加如下配置:
name: helloword # 要发布的项目名称
description: A Test Flutter plugin. # 项目描述
version: 1.0.0 # 发布的版本
authors: [zhangsan ] # 项目作者
homepage: http://localhost:8080 # 项目地址
publish_to: http://localhost:8080 # 发布的私有服务器地址 如果要发布到公共pub库,则该行可以省略
配置好了之后通过命令检查一下:
flutter packages pub publish --dry-run
如用以上命令有warning就按要求修改下,如没错误就开始发布了, 发布命令:
flutter packages pub publish
|-- lib
| '-- helloworld.dart
|-- pubspec.yaml
'-- test
'-- helloworld_test.dart
Looks great! Are you ready to upload your package (y/n)? y
Uploading...
Successfully uploaded package.
如出现以上情况就表示发布成功了
然而,多数情况下我们会出现下述情况:
对,没错。需要进行google验证,即便发布到私有仓库也要通过这一步。而国内进行google验证要fq,比较麻烦。
如果发布到共有pub库就乖乖进行验证吧,验证后接着发布,坐等成功。
那发布到私有仓库如何设置跳过google验证?别着急,我们往下看
2. 搭建pub私服
下载私服源码
私服源码下载地址
这个源码也是个dart项目,我们可以解压后直接用Android Studio打开
打开Terminal窗口,输入pub get
拉取依赖,成功后会显示Got dependencies!
然后执行命令:dart example/example.dart -d /tmp/package-db
来启动服务
出现上述图案表示服务已经成功启动
3. 跳过google验证
下载项目:https://github.com/ameryzhu/pub
-
我们仍然用Android Studio打开,打开Terminal窗口,更新依赖:
pub get
然后执行
dart --snapshot=mypub.dart.snapshot bin/pub.dart
完成之后会在项目根目录下多出来一个 mypub.dart.snapshot 文件
复制之后放入${flutterSDK Path}/bin/cache/dart-sdk/bin/snapshots/ 目录下
用txt编辑器打开${flutterSDK Path}/bin/cache/dart-sdk/bin/pub文件
将倒数第三行的:
pub.dart.snapshot
替换为mypub.dart.snapshot
function follow_links() { file="$1" while [ -h "$file" ]; do # On Mac OS, readlink -f doesn't work. file="$(readlink "$file")" done echo "$file" } function array_contains() { local needle="$1" local element shift for element; do [ "$element" = "$needle" ] && return 0; done return 1 } # Unlike $0, $BASH_SOURCE points to the absolute path of this file. PROG_NAME="$(follow_links "$BASH_SOURCE")" # Handle the case where dart-sdk/bin has been symlinked to. BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)" unset VM_OPTIONS declare -a VM_OPTIONS # Allow extra VM options to be passed in through an environment variable. if [[ $DART_VM_OPTIONS ]]; then read -a OPTIONS <<< "$DART_VM_OPTIONS" VM_OPTIONS+=("${OPTIONS[@]}") fi # Run the pub snapshot. DART="$BIN_DIR/dart" if array_contains "--no-preview-dart-2" "${VM_OPTIONS[@]}"; then echo "Pub no longer supports Dart 1" exit -1 else SNAPSHOT="$BIN_DIR/snapshots/mypub.dart.snapshot" exec "$DART" "${VM_OPTIONS[@]}" "$SNAPSHOT" "$@" fi
保存&退出
再切换到plugin工程进行发布,ok!发布成功、已经不需要google验证了
4. 依赖我们发布的插件
在我们需要依赖的项目的yaml文件中 dependencies:下添加:
helloword:
hosted:
name: helloword
url: http://localhost:8080
version: ^1.0.0
注意:yaml中层级一定要用空格对齐(helloword一级,hosted和version二级,name和url3级),否则会报错;然后执行pub get
即可
如果我们仅仅只需要在本地依赖,可以直接用路径依赖,免去发布的这一步:
helloword:
path: ...../Workspace/plugin/hellowork