同时在多个连接的设备/仿真器上运行flutter应用

时间:2019-02-05 04:37:58

标签: flutter

如何在多个设备上同时运行我的flutter应用程序,而无需执行以下顺序过程:选择设备->运行,选择其他设备->运行等?

使用: Android Studio 3.2.1 颤动1.0.0 Dart 2.1.0

4 个答案:

答案 0 :(得分:4)

有许多方法可以执行此操作,如先前所回答。如果您使用VS Code而不是Android Studio作为Flutter IDE,这就是使用VSC启动配置和任务从一次启动并发运行并为所有设备启用热重载的方式。

如果执行flutter run -d all时遇到问题,这是另一种解决方案,可以让您指定应运行的设备。 运行flutter devices时,请确保指定的设备可用。

您当前的launch.json文件可能看起来像这样:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flutter",
            "type": "dart",
            "request": "launch",
            "flutterMode": "debug"        
        }
    ]
}

设置

您将需要更新此launch.json文件,并在应用程序根目录中的同一tasks.json文件夹中创建.vscode

Application folder file structure once the VSC config files are created

仅将以下代码粘贴到launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flutter-All",
            "preLaunchTask": "Flutter-Launch-All",
            "type": "dart",
        },
        {
            "name": "Flutter-iOS",
            "preLaunchTask": "Flutter-Launch-iOS",
            "type": "dart",
        },
        {
            "name": "Flutter-Android",
            "preLaunchTask": "Flutter-Launch-Android",
            "type": "dart",
        },
        {
            "name": "Flutter-Web",
            "preLaunchTask": "Flutter-Launch-Web",
            "type": "dart",
        }
    ],
}

仅将以下代码粘贴到tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Flutter-Launch-All",
      "dependsOn": [
        "Flutter-Launch-iOS",
        "Flutter-Launch-Android",
        "Flutter-Launch-Web"
      ]
    },
    {
      "label": "Flutter-Launch-iOS",
      "type": "shell",
      "command": "flutter run -d 'iPhone 11' "
    },
    {
      "label": "Flutter-Launch-Android",
      "type": "shell",
      "command": "flutter run -d 'AOSP on IA Emulator' "
    },
    {
      "label": "Flutter-Launch-Web",
      "type": "shell",
      "command": "flutter run -d 'Chrome' "
    }
  ]
}

相应地替换设备名称(“ iPhone 11”,“ IA Emulator上的AOSP”,“ Chrome”)。

启动所有设备

按F5键。

您已完成。

如果Start Debugging的F5快捷键对您不起作用,请导航至侧面板上的Debug & Run,然后选择刚刚创建的Flutter-All配置,然后运行。

Debug & Run Menu > Configuration Selection

然后您将看到终端窗口出现,并且将能够在运行的各个热重装会话之间切换(如Tasks在其自己的Shell中)。

Individual Terminal Sessions with each Flutter Device running concurrently

某些背景

我们通过任务上的dependsOn选项使用“复合任务”,而不是用于配置的“化合物”。

由于不可能同时启动配置,而只能依次启动,因此我们使用可以同时运行的任务。

因此,“ Flutter-All”配置将执行iOS,Android和Web配置的任务。

如果使用化合物,则需要在下一次运行之前完成配置,这不是我们想要的。 使用Tasks,我们可以选择按顺序执行它们,但是默认情况下,使用dependsOn选项时,它们将同时执行。

//Do not use this unless you want to use Configurations only by testing them sequentially and not tasks
"compounds": [
    {
        "name": "Flutter-All",
        "configurations": ["Flutter-iOS", "Flutter-Android", "Flutter-Web"],
    }
]

答案 1 :(得分:2)

在终端中运行命令:

flutter run -d all 

创建脚本(例如,root中的runall.sh):

#!/usr/bin/env bash
flutter run -d all

并转到“运行”->“编辑配置”。按左上角的“ +”->选择“重击”。然后设置:

  • 名称:runall
  • 脚本:[runall.sh脚本的路径]
  • 解释器路径:/ bin / bash

在运行图标旁边选择“ runall”而不是“ main.dart”。现在,执行运行(也通过快捷方式)将在所有设备上运行应用程序。

缺点:您必须在运行终端中输入“ r”,然后输入Enter进行热重载。图标和快捷方式不起作用。但是,热重加载会在所有设备上执行。

现在只是一种解决方法。我很确定flutter插件很快就会解决这个问题。

答案 2 :(得分:0)

您始终可以使用外部工具来监视文件并触发热装。

Flutter支持某些信号以本地触发热重装

  --pid-file  Specify a file to write the process id to. You can send SIGUSR1 to trigger a hot reload and SIGUSR2 to trigger a hot restart.

这是一个快速的示例:

#!/usr/bin/env bash

set -euo pipefail

# Remove previous pid files
rm -f /tmp/flutter.pid

# Run in a loop a hot reload call in a subshell
(while true
do
    # Wait for flutter to start before monitoring pid
    while [[ ! -f /tmp/flutter.pid ]]; do sleep 1; done;

    # Send hot reload signal when files change
    find lib/ -name '*.dart' | entr -n -d -p kill -USR1 $(cat /tmp/flutter.pid)
done) &

# Run all devices under 1 pid
flutter run -d all --pid-file /tmp/flutter.pid

来自https://medium.com/@kikap/how-to-automatically-hot-reload-flutter-when-dart-source-files-change-6e8fdb523004

的想法

有关输入的更多详细信息:http://eradman.com/entrproject/entr.1.html

答案 3 :(得分:0)

如果不想每次都直接使用命令行,则可以执行以下解决方法:

  1. 下载bashsupport pluginlink
  2. 创建新的configuration的bash,将脚本字段保留为空,然后在interperter options单元格中插入:flutter run -d all。它应该类似于:enter image description here
  3. 如果第二步不起作用,请在根项目中创建一个类似于run_all.sh的文件调用。并在其中放置下几行(假设bin/bash是您bash的路径):

    #!/bin/bash flutter run -d all

    在您的终端中输入:chmod 755 run_all.sh

    在您的run_all.shconfiguration中将bin/bash指定为您的interprter path。 从flutter run -d all中删除interperter options

    其外观应类似于:enter image description here

相关问题