Recently, I received a task from the company to make a business component of the company’s project and upload it to the company’s private PUB warehouse so that it can be used by other colleagues quickly

First, let’s briefly introduce two methods: how to create a component plugin project

1. Use The Android Studio tool to create quickly, but the project created by default in ios swift language, Android Kotlin language

2. You can also use the command line tool to create the default development language:

flutter create -t plugin helloworld

If you want to specify the development language, you can simply use this command, for example, ios for OC, Android for Java. For more commands, refer to the official command

flutter create --template=plugin --platforms=android,ios -i objc -a java hello

Release configuration

Your YAML file should basically look like this after your component is complete

Version: 1.0.0 author: XXX <xxx.com> homepage: http://xxx.com # http://xxx.com # Published private server address This line can be omitted if you want to publish to the public pub libraryCopy the code

After the configuration is complete, perform the following operations

  • In terminal CD to project file, execute the following command to perform pre-check before release

flutter packages pub publish --dry-run

  • If an error occurs, modify the information as prompted. After the check is complete, run the following command to release the information

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.
Copy the code

If you see this output, it means that the upload was successful

However, most of the time we have the following:

Here, if you are Posting to the public pub warehouse, there is no way, we have to go through the ladder to Google authentication, but since we are uploading to our own company’s private PUB warehouse, so we can use the following method to build a private PUB server, to bypass Google authentication, so upload

1. Set up a private PUB server

  • Download private server source code: private server source code address
  • The source code is a Dart project that can be unpacked and opened directly with Android Studio
  • Open the Terminal window and enterpub getGet dependencies!
  • Then execute the command:dart example/example.dart -d /tmp/package-dbTo start the service

If the following style appears, the startup is successful

2. Skip Google validation

  • Download project: Project address
  • We are still using Android Studio to open, open the Terminal window, update the dependency:pub get
  • Perform:dart --snapshot=mypub.dart.snapshot bin/pub.dart
  • The mypub.dart. Snapshot file will be added to the root directory of the current project
  • Find the location of your flutter SDK file, copy mypub.dart.snapshot and put it in the -> /bin/cache/dart-sdk/bin/snapshots/ directory
  • Open the flutter SDK file -> /bin/cache/dart-sdk/bin/pub with the TXT editor
  • Put the third row from the bottom:pub.dart.snapshotReplace withmypub.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
Copy the code
  • Save & Exit
  • Then switch to the Plugin project for publishing

3. Some problems after replacement

Json file pub did not create. Dart_tools /package_config.json file pub did not create. So after you’ve published your own component, it’s best to change it back to avoid some random error in the future!

4. If your post is publicpub.devWarehouse, according to the following steps:

  • Open your FQ software, and set the terminal traffic to go proxy. The tool I use here is the ClashX client of Shadowfly, which has its own terminal command. Just copy it into the terminal and execute it

For example, export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890

  • Again, check your code

flutter packages pub publish --dry-run

  • Release to pub warehouse, execute the following command, the first terminal will let you open an address with Google Browser, copy the address to Google Browser, log in your Google account will automatically agree, and then push success!

flutter packages pub publish --server=https://pub.dartlang.org