flutter 发布插件到私服

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=<type>          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=<id>              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=<path>      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 就好

<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gic4wx8jopj30lv02jgo6.jpg" alt="image-20200902111524956" />

<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gic4x7iro1j30p00io3zw.jpg" alt="image-20200902111541613" style="zoom:50%;" />

3) 发布配置

修改项目的 pubspec.yaml 文件, 添加如下配置:

name: helloword # 要发布的项目名称
description: A Test Flutter plugin. # 项目描述
version: 1.0.0 # 发布的版本
authors: [zhangsan <zhangsan@qq.com>] # 项目作者
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.

如出现以上情况就表示发布成功了

然而,多数情况下我们会出现下述情况:

image-20200902161125798

对,没错。需要进行google验证,即便发布到私有仓库也要通过这一步。而国内进行google验证要fq,比较麻烦。

如果发布到共有pub库就乖乖进行验证吧,验证后接着发布,坐等成功。

那发布到私有仓库如何设置跳过google验证?别着急,我们往下看

2. 搭建pub私服

下载私服源码

私服源码下载地址

这个源码也是个dart项目,我们可以解压后直接用Android Studio打开

打开Terminal窗口,输入pub get拉取依赖,成功后会显示Got dependencies!

然后执行命令:dart example/example.dart -d /tmp/package-db 来启动服务

image-20200902145529779

出现上述图案表示服务已经成功启动

3. 跳过google验证

  1. 下载项目:https://github.com/ameryzhu/pub

  2. 我们仍然用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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270