preface

Flutter Tools integrates many powerful commands, such as:

  • Flutter create creates app, Module, plugin, package
  • Flutter build: build APK, AAR, etc
  • To understand the principles behind the various commands, one can look at static code on the one hand and debug dynamically on the other.

Import the source code of Flutter Tools

We know that flutter tools are implemented using the dart, its source is located in the path/to/flutter/packages/flutter_tools. Open the project with Android Studio.

To set breakpoints

The debugging principle of many programs is similar. When starting the corresponding program, pass in some debugging parameters, and let the corresponding process wait until the Debugger process attaches to the process and continues to run.

Flutter Tools is essentially a DART program whose debugging is supported by the DART VIRTUAL machine.

For Android Studio, there are two configurations:

  1. Dart Command Line App
  2. Dart Remote Debug

Dart Command Line App

Create a Configuration for the Dart Command Line App.

Edit Configurations – “New” Dart Command Line App If as shown:

Dart file points to flutter_tools. Dart, which is the entry file. Programma arguments fill in the command to run, such as create Flutter_app. Then add a breakpoint on the main method and click the debug button, as shown

Dart Remote Debug

Create a Configuration. Edit Configurations – “New -” Dart Remote Debug – “Enter Host as 127.0.0.1 and port number as 12345, as shown:

--enable-vm-service:12345 --pause_isolates_on_start
FLUTTER_TOOL_ARGS

 export FLUTTER_TOOL_ARGS="--enable-vm-service:12345 --pause_isolates_on_start"
Copy the code

Then run the flutter command

flutter create flutter_app
Copy the code

The following information is displayed:

Observatory listening on http://127.0.0.1:12345/-7kH8m0Z5Ys=/
Copy the code

After setting the breakpoint again and clicking the debug button, attach failed. The error message is as shown in the figure

Failed to connect to the VM observatory service: java.io.IOException: Failed to connect: Ws: / / 127.0.0.1:12345 / ws under Caused by: DE. Roderick. Weberknecht. WebSocketException: errorwhileCreating a socket to ws: / / 127.0.0.1:12345 / ws under Caused by: java.net.ConnectException: Connection refused (Connection refused)Copy the code

A -7KH8m0z5ys = -7KH8m0z5ys = -7KH8M0z5ys = -7KH8M0z5ys = -7KH8M0z5ys = -7KH8M0z5ys = -7KH8M0z5ys = -7KH8M0z5ys = -7KH8M0z5ys = -7KH8M0z5ys = This can be turned off with the –disable-service-auth-codes parameter.

 export FLUTTER_TOOL_ARGS="--enable-vm-service:12345 --pause_isolates_on_start --disable-service-auth-codes"
Copy the code

Then re-run the flutter create flutter_app command, this time successfully, as shown in the figure

In addition to the FLUTTER_TOOL_ARGS environment variable, you can also run the DART command directly.

dart --enable-vm-service:12345 --pause_isolates_on_start --disable-service-auth-codes /Users/szw/dev_tools/flutter/packages/flutter_tools/bin/flutter_tools.dart create flutter_app
Copy the code

FLUTTER_TOOL_ARGS is also passed to DART by the FLUTTER command. The path of the flutter script is flutter/bin/flutter, and the last line of the flutter script is

"$DART" --packages="$FLUTTER_TOOLS_DIR/.packages" $FLUTTER_TOOL_ARGS "$SNAPSHOT_PATH" "$@"
Copy the code

conclusion

The methods described in this article apply not only to FLUTTER Tools, but also to other DART applications. In addition, debugging of other languages are similar, we can draw inferential conclusions.