【Flutter】アプリケーションの名前を変更するpackage/plugin「flutter_launcher_name」を作った
概要
前回、Flutterでアプリケーション名を変更するのに、手動でAndroidManifest.xmlとInfo.plistを変更しましたが、だるいと思ったので、自作でpackageを作って公開してみました。
そんなに変更するものではないけど、せっかくFlutter使ってるのに各OS毎に個別に変更するというのはナンセンスだと思ったのと、package/plugin作成の練習で作ってみました。
前回の。
- 概要
- Flutterのpakage/pluginってどこで管理されてるの?
- packageの作り方 参考資料を探す
- 作成する
- Adding documentation ドキュメント追加
- Publishing packages パッケージ公開する
- 使い方
- まとめ
Flutterのpakage/pluginってどこで管理されてるの?
FlutterというかDartのpackage/pluginですね。jsのnpmみたいなものです。
pubspec.yamlの「dependencies」「dev_dependencies」に書いて
pub get
するとここからとってくるようです。
packageの作り方 参考資料を探す
公式 flutter.dev
日本語訳 qiita.com
やってみた dev.classmethod.jp
ドキュメントはけっこうしっかりしてそうなので、これに従ってやっていきます。
作成する
FlutterのWidgetやネイティブのコードを直接触るわけではないので、「Dart packages」の方で。
全体として、設定ファイル触る系のpluginとして「flutter_launcher_icons」を参考にします。
名前も真似して「flutter_launcher_name」で。
Step 1: Create the package
とりあえず雛形つくります
$ flutter create --template=package flutter_launcher_name Creating project flutter_launcher_name... flutter_launcher_name/LICENSE (created) flutter_launcher_name/test/flutter_launcher_name_test.dart (created) flutter_launcher_name/flutter_launcher_name.iml (created) flutter_launcher_name/.gitignore (created) flutter_launcher_name/.metadata (created) flutter_launcher_name/pubspec.yaml (created) flutter_launcher_name/README.md (created) flutter_launcher_name/lib/flutter_launcher_name.dart (created) flutter_launcher_name/.idea/libraries/Dart_SDK.xml (created) flutter_launcher_name/.idea/modules.xml (created) flutter_launcher_name/.idea/workspace.xml (created) flutter_launcher_name/CHANGELOG.md (created) Running "flutter pub get" in flutter_launcher_name... 4.2s Wrote 12 files. All done! Your package code is in flutter_launcher_name/lib/flutter_launcher_name.dart
AndroidStudioで開いてみたらこんな感じで各種ファイルが作成されてます。
Step 2: Implement the package 下準備
公式ドキュメント的には作ってね、としか書いてないので頑張って作ります。
exampleプロジェクト作成
テストで動かすようの対象プロジェクトが無いと開発しようが無いので、package配下にプロジェクトを作成します。
$ cd flutter_launcher_name $ flutter create example Creating project example... example/ios/Runner.xcworkspace/contents.xcworkspacedata (created) example/ios/Runner/Info.plist (created) example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png (created) example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png (created) example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md (created) example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json (created) example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png (created) example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png (created) example/ios/Runner/Base.lproj/LaunchScreen.storyboard (created) example/ios/Runner/Base.lproj/Main.storyboard (created) example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata (created) example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme (created) example/ios/Flutter/Debug.xcconfig (created) example/ios/Flutter/Release.xcconfig (created) example/ios/Flutter/AppFrameworkInfo.plist (created) example/test/widget_test.dart (created) example/example.iml (created) example/.gitignore (created) example/.metadata (created) example/ios/Runner/AppDelegate.h (created) example/ios/Runner/main.m (created) example/ios/Runner/AppDelegate.m (created) example/ios/Runner.xcodeproj/project.pbxproj (created) example/android/app/src/profile/AndroidManifest.xml (created) example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png (created) example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png (created) example/android/app/src/main/res/drawable/launch_background.xml (created) example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png (created) example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png (created) example/android/app/src/main/res/values/styles.xml (created) example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png (created) example/android/app/src/main/AndroidManifest.xml (created) example/android/app/src/debug/AndroidManifest.xml (created) example/android/gradle/wrapper/gradle-wrapper.properties (created) example/android/gradle.properties (created) example/android/settings.gradle (created) example/pubspec.yaml (created) example/README.md (created) example/lib/main.dart (created) example/android/app/build.gradle (created) example/android/app/src/main/java/com/example/example/MainActivity.java (created) example/android/build.gradle (created) example/android/example_android.iml (created) example/.idea/runConfigurations/main_dart.xml (created) example/.idea/libraries/Flutter_for_Android.xml (created) example/.idea/libraries/Dart_SDK.xml (created) example/.idea/libraries/KotlinJavaRuntime.xml (created) example/.idea/modules.xml (created) example/.idea/workspace.xml (created) Running "flutter pub get" in example... 4.3s Wrote 66 files. All done! [✓] Flutter is fully installed. (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.14.6 18G87, locale ja-JP) [✓] Android toolchain - develop for Android devices is fully installed. (Android SDK version 29.0.2) [✓] Xcode - develop for iOS and macOS is fully installed. (Xcode 10.3) [✓] iOS tools - develop for iOS devices is fully installed. [✓] Android Studio is fully installed. (version 3.5) [✓] Android Studio is fully installed. (version 3.5) [✓] VS Code is fully installed. (version 1.37.1) [!] Connected device is not available. Run "flutter doctor" for information about installing additional components. In order to run your application, type: $ cd example $ flutter run Your application code is in example/lib/main.dart.
普通のFlutterプロジェクトがexampleとして作成されました。
example->flutter_launcher_name への参照設定
exampleプロジェクトから相対パスでflutter_launcher_nameを参照するようにします。
pubspec.yamlに設定を追加して、
dev_dependencies: flutter_launcher_name: path: ../
pub get
すればOK。
コマンド叩けるようにする
flutter pub run flutter_launcher_name:main
で動くようにします。
bin/main.dart
import 'package:flutter_launcher_name/flutter_launcher_name.dart' as FlutterName; main(List<String> arguments) { FlutterName.exec(); }
lib/flutter_launcher_name.dart
lib/flutter_launcher_name.dart library flutter_launcher_name; exec() { print('flutter_launcher_name exec'); }
$ flutter pub run flutter_launcher_name:main flutter_launcher_name exec
動いた。
Step 2: Implement the package ごりごり開発
普通に頑張って作ります、、、
Adding documentation ドキュメント追加
パッケージ公開のためのドキュメントを作ります。
README
英語わかりません、、、 flutter_launcher_iconsを参考にして、関係ないとこ消して、iconsをnameに変更したくらいです。
長いのでリンクで。
CHANGELOG
初期バージョンなのでシンプルに。
# Changelog ## 0.0.1 - Initial version.
LICENSE
BSDライセンスにしました。コピペして年度と名前変えるだけ。
Copyright 2019 moyashidaisuke Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Publishing packages パッケージ公開する
コマンドが用意されてるので叩きます。 まずはdry-run。
$ flutter pub pub publish --dry-run Publishing flutter_launcher_name 0.0.1 to https://pub.dartlang.org: |-- .gitignore |-- .metadata |-- CHANGELOG.md |-- LICENSE |-- README.md |-- bin | '-- main.dart |-- example 〜省略〜 |-- pubspec.yaml '-- test '-- flutter_launcher_name_test.dart Missing requirements: * Your pubspec.yaml must have an "author" or "authors" field. * Your pubspec.yaml is missing a "homepage" field. Sorry, your package is missing some requirements and can't be published yet. For more information, see: https://dart.dev/tools/pub/cmd/pub-lish. pub finished with exit code 65 * Your pubspec.yaml must have an "author" or "authors" field. * Your pubspec.yaml is missing a "homepage" field.
pubspec.yamlに「author」と「homepage」を書けとの事なので加筆します。
--- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ name: flutter_launcher_name -description: A new Flutter package project. +description: A command-line tool which simplifies the task of updating your Flutter app's launcher name. version: 0.0.1 -author: -homepage: +author: moyashidaisuke <guitaristdaisuke@gmail.com> +homepage: https://github.com/daisuke-fukuda/flutter_launcher_name
公開
$ flutter pub pub publish Publishing flutter_launcher_name 0.0.1 to https://pub.dartlang.org: |-- .gitignore |-- .metadata |-- CHANGELOG.md |-- LICENSE |-- README.md |-- bin | '-- main.dart |-- example 〜省略〜 |-- pubspec.yaml '-- test '-- flutter_launcher_name_test.dart Looks great! Are you ready to upload your package (y/n)? y Pub needs your authorization to upload packages on your behalf. In a web browser, go to https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&response_type=code&client_id=XXXXXXXX(省略) Then click "Allow access".
ブラウザでURLを開けとの事なので、コピペしてブラウザで開くと認証が取ります。(google認証です。)
Waiting for your authorization... Authorization received, processing... Successfully authorized. Uploading... Successfully uploaded package.
うおーーできたーー
公開されてる!
使い方
READMEの通りですが、、、
pubspec.yaml
dev_dependencies: flutter_launcher_name: "^0.0.1" flutter_launcher_name: name: "アプリケーション名"
$ flutter pub get Running "flutter pub get" in english... Process finished with exit code 0
$ flutter pub run flutter_launcher_name:main start exit
「android/app/src/main/AndroidManifest.xml」と「ios/Runner/Info.plis」が更新されてればOKです。
まとめ
超絶雑なのでPR、issueお待ちしております。