1. 創(chuàng)建要發(fā)布的插件
創(chuàng)建項目一般有2中方法
1) 用命令創(chuàng)建
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創(chuàng)建(推薦)
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) 發(fā)布配置
修改項目的 pubspec.yaml 文件, 添加如下配置:
name: helloword # 要發(fā)布的項目名稱
description: A Test Flutter plugin. # 項目描述
version: 1.0.0 # 發(fā)布的版本
authors: [zhangsan <zhangsan@qq.com>] # 項目作者
homepage: http://localhost:8080 # 項目地址
publish_to: http://localhost:8080 # 發(fā)布的私有服務(wù)器地址 如果要發(fā)布到公共pub庫,則該行可以省略
配置好了之后通過命令檢查一下:
flutter packages pub publish --dry-run
如用以上命令有warning就按要求修改下,如沒錯誤就開始發(fā)布了, 發(fā)布命令:
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.
如出現(xiàn)以上情況就表示發(fā)布成功了
然而,多數(shù)情況下我們會出現(xiàn)下述情況:
對堤器,沒錯屋剑。需要進(jìn)行g(shù)oogle驗證呛谜,即便發(fā)布到私有倉庫也要通過這一步风罩。而國內(nèi)進(jìn)行g(shù)oogle驗證要fq,比較麻煩傍衡。
如果發(fā)布到共有pub庫就乖乖進(jìn)行驗證吧,驗證后接著發(fā)布负蠕,坐等成功蛙埂。
那發(fā)布到私有倉庫如何設(shè)置跳過google驗證?別著急遮糖,我們往下看
2. 搭建pub私服
下載私服源碼
這個源碼也是個dart項目绣的,我們可以解壓后直接用Android Studio打開
打開Terminal窗口,輸入pub get
拉取依賴欲账,成功后會顯示Got dependencies!
然后執(zhí)行命令:dart example/example.dart -d /tmp/package-db
來啟動服務(wù)
出現(xiàn)上述圖案表示服務(wù)已經(jīng)成功啟動
3. 跳過google驗證
-
我們?nèi)匀挥肁ndroid Studio打開屡江,打開Terminal窗口,更新依賴:
pub get
然后執(zhí)行
dart --snapshot=mypub.dart.snapshot bin/pub.dart
完成之后會在項目根目錄下多出來一個 mypub.dart.snapshot 文件
復(fù)制之后放入${flutterSDK Path}/bin/cache/dart-sdk/bin/snapshots/ 目錄下
用txt編輯器打開${flutterSDK Path}/bin/cache/dart-sdk/bin/pub文件
將倒數(shù)第三行的:
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工程進(jìn)行發(fā)布赛不,ok盼理!發(fā)布成功、已經(jīng)不需要google驗證了
4. 依賴我們發(fā)布的插件
在我們需要依賴的項目的yaml文件中 dependencies:下添加:
helloword:
hosted:
name: helloword
url: http://localhost:8080
version: ^1.0.0
注意:yaml中層級一定要用空格對齊(helloword一級俄删,hosted和version二級宏怔,name和url3級)奏路,否則會報錯;然后執(zhí)行pub get
即可
如果我們僅僅只需要在本地依賴臊诊,可以直接用路徑依賴鸽粉,免去發(fā)布的這一步:
helloword:
path: ...../Workspace/plugin/hellowork