在开发过程中启动组件

出于测试和调试目的,ffx component 命令可以在设备上快速启动动态组件实例

概念

设备上的组件实例通常使用组件清单进行声明,该清单静态定义设备上组件的拓扑和功能。但是,静态声明组件并不是在设备上创建组件实例的唯一方式。您还可以使用 ffx component 命令在运行时在设备上创建动态组件实例。

一个重要的区别是,Fucsia 会限制所有动态组件实例在组件集合下运行。集合充当动态组件实例的容器。因此,一个集合下组件实例的功能受到该集合能够公开和提供的功能的限制。

虽然可以使用组件清单来声明新的组件集合(类似于声明静态组件实例),但 Fuchsia 提供了许多常规用途的预定义集合。例如,ffx-laboratory 是这些预定义集合之一。

启动组件

如需创建新的动态组件实例,您首先需要运行 ffx component create,以向设备上的组件实例树添加新组件。添加组件后,您可以运行 ffx component start 在设备上启动该组件。

如需在设备上启动新的动态组件实例,请执行以下操作:

  1. 创建一个新的组件实例:

    ffx component create <TARGET_MONIKER> <COMPONENT_URL>
    

    替换以下内容:

    • TARGET_MONIKER:新组件实例的目的地名称。该名称必须在路径上包含一个组件集合。
    • COMPONENT_URL:组件的资源位置。

    以下示例为 hello-world-cpp.cm 组件创建了一个新的组件实例,并将其名称指定为 /core/ffx-laboratory:hello-world

    $ ffx component create /core/ffx-laboratory:hello-world fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
    URL: fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
    Moniker: /core/ffx-laboratory:hello-world
    Creating component instance...
    
  2. 启动组件实例:

    ffx component start <TARGET_MONIKER>
    

    TARGET_MONIKER 替换为第 1 步中使用的别名。

    以下示例从 /core/ffx-laboratory:hello-world 启动组件实例:

    $ ffx component start /core/ffx-laboratory:hello-world
    Moniker: /core/ffx-laboratory:hello-world
    Starting component instance...
    

在 ffx-laboratory 下启动组件

启动动态组件实例通常需要运行一系列 ffx component createffx component start 命令(请参阅启动组件)。不过,ffx component run 命令可以在单个命令行中启动动态组件实例。

如需快速启动 ffx-laboratory 集合下的组件,请运行以下命令:

ffx component run /core/ffx-laboratory:<NAME> <COMPONENT_URL>

COMPONENT_URL 替换为组件的资源位置,并将 NAME 替换为 ffx-laboratory 集合中组件实例的名称。

以下示例在设备上启动 hello-world-cpp.cm 组件:

$ ffx component run /core/ffx-laboratory:hello-world-cpp fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
URL: fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
Moniker: /core/ffx-laboratory:hello-world-cpp
Creating component instance...
Starting component instance...

本质上,ffx component run 命令会执行以下步骤:

  1. 运行 ffx component create 以在 ffx-laboratory 集合下创建一个新的组件实例,并使用组件名称作为目标名称。
  2. 运行 ffx component start 以在设备上启动组件实例。

例如,在上述示例中运行 ffx component run /core/ffx-laboratory:hello-world-cpp fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm 等同于运行以下命令:

$ ffx component create /core/ffx-laboratory:hello-world-cpp fuchsia-pkg://fuchsia.com/hello-world-cpp#meta/hello-world-cpp.cm
$ ffx component start /core/ffx-laboratory:hello-world-cpp

停止组件

如需停止设备上正在运行的组件实例,请运行以下命令:

ffx component stop <TARGET_MONIKER>

TARGET_MONIKER 替换为组件实例的别名。

以下示例会将组件实例停止到 /core/ffx-laboratory:hello-world

$ ffx component stop /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Stopping component instance...

销毁组件

如需从设备上的组件实例树中移除动态组件实例,请运行以下命令:

ffx component destroy <TARGET_MONIKER>

TARGET_MONIKER 替换为组件实例的别名。

以下示例会移除位于 /core/ffx-laboratory:hello-world 的组件实例:

$ ffx component destroy /core/ffx-laboratory:hello-world
Moniker: /core/ffx-laboratory:hello-world
Destroying component instance...