构建和部署服务包

创建中间件并编写自定义代码后,您可以将服务软件包部署为 APEX 文件。

  1. 创建 SDV 配置文件:

    Rust

    1. 通过运行以下命令生成 SDV 配置文件:

      vsidlc -c /path/to/catalog -o /path/to/output --apex
      

      运行带有 --apex 标志的 vsidlc 会自动生成所有必要的配置文件,包括:

      • APEX 清单 (apex_manifest.json)
      • SDV 元数据清单 (sdv_service_bundles_manifest.textproto)
      • 链接器配置 (linker.config.json)
      • SELinux 文件上下文 (file_contexts)
      • 公钥和私钥(.avbpubkey 和 .pem)
      • 权限声明和编排配置
      • 一个 Android.bp 文件,用于定义所有必要的 Soong 模块。

      生成的 apex/Android.bp 会设置 updatable: true、min_sdk_version: "35" 和 product_specific: true,以便您在 /data/apex 中暂存独立的 APEX 更新(在设备重新启动后激活),并从 Android Studio for Platform 部署迭代更新。如需了解详情,请参阅更新管理器。

      您可以在以下位置找到生成的配置:

      /path/to/output/apex/
      

      和

      /path/to/output/configs/
      

    C++

    1. 创建一个 SDV 元数据清单文件 service_bundle.manifest.textproto,其中包含以下基本执行元数据字段:

      • 根据命名规范确定的服务名称
      • 服务包版本的整数和字符串表示
      • 之前创建的库的路径

      准备必要的服务包元数据是服务包打包和部署的强制性要求。

      # proto-file: //system/software_defined_vehicle/core_services/service_bundles_registry/proto/sdv_service_bundles_manifest.proto
      # proto-message: SdvServiceBundleManifestEntry
      
      # SDV service bundle metadata definition with mandatory fields.
      sdv_service_bundle_metadata {
        # Service bundle name.
        name: "ServiceBundleName"
        # Service bundle integer version.
        version_number: 42
        # Service bundle version as a string.
        version_name: "42.alpha"
        # Service bundle library path.
        native_library_path: "lib64/libservice_bundle.so"
      }
      
    2. 创建新的链接器配置文件 linker.config.json:

      { "visible": true }
      
    3. 创建包含以下字段的新 apex_manifest.json 文件:

      • 唯一的 SDV 软件包名称
      • APEX 的版本
      • 对 SDV Comms 库的依赖
      {
        "name": "com.sdv.oem.apex_and_module_name",
        "version": 1,
        "requireNativeLibs": [
            "libsdv_comms_ctx_ffi.so",
            "libsdv_comms_dt_ffi.so",
            "libsdv_comms_id_ffi.so",
            "libsdv_comms_sd_ffi.so"
        ]
      }
      
    4. 创建公钥 apex_name.apex_key.avbpubkey 和私钥 apex_name.apex_key.pem。

    5. 创建新的 SELinux 文件上下文 apex_file_contexts。

      (/.*)?    u:object_r:system_file:s0
      

      如需了解详情,请参阅安全上下文和类别。

    6. 在新的或现有的 Android.bp 文件中创建预构建目标:

      // SDV manifest file prebuilt.
      prebuilt_etc {
          name: "com.sdv.oem.apex_and_module_name.service_bundles_manifest",
          // The source filename of the manifest file.
          src: "service_bundle.manifest.textproto",
          // The name of the installed file needs be `sdv_service_bundles_manifest.textproto`.
          filename: "sdv_service_bundles_manifest.textproto",
          // Disable direct installation of the prebuilt to one of the partitions.
          // The manifest is used only inside of an apex.
          installable: false,
      }
      // The linker config to allow libraries for the apex to be linkable.
      linker_config {
          name: "com.sdv.oem.apex_and_module_name.linker_config",
          src: "linker.config.json",
          // Disable direct installation of the prebuilt to one of the partitions.
          // The linker configuration is used only inside of an apex.
          installable: false,
      }
      // Key to be used for signing the apex.
      apex_key {
          name: "apex_name.apex_key",
          public_key: "apex_name.apex_key.avbpubkey",
          private_key: "apex_name.apex_key.pem",
      }
      
    7. 在新的或现有的 Android.bp 文件中创建新的 APEX 模块。 使用 SDV 软件包名称作为 APEX 模块名称。

      apex {
          name: "com.sdv.oem.apex_and_module_name",
          // The service bundle(s) to be included in the apex.
          native_shared_libs: [
              "libservice_bundle",
          ],
          prebuilts: [
              // SDV service bundles manifest file.
              "com.sdv.oem.apex_and_module_name.service_bundles_manifest",
              // Linker configuration to enable loading library from apex.
              "com.sdv.oem.apex_and_module_name.linker_config",
          ],
          // Json manifest file describes metadata of the APEX package.
          // The 'name' field from the JSON is used as a mounting point.
          manifest: "apex_manifest.json",
          // Setting the security contexts to files in this APEX bundle.
          file_contexts: "apex_file_contexts",
          // Name of the apex_key module that provides the private key to sign the APEX bundle.
          key: "apex_name.apex_key",
          // Non-updatable manual C++ example (updated through A/B system updates).
          updatable: false,
          // Service bundle package is installed into /product partition.
          product_specific: true,
      }
      
  2. 如需在 SDV 映像中包含新的 makefile,请将新的 APEX 模块添加到产品 makefile 中的 PRODUCT_PACKAGES。如需查看示例,请参阅 sdv_samples_core_services.mk。

  3. 如果您使用的是 Cuttlefish,请运行以下命令来启动设备:

    sdv-cf create --instance_name=instance1
    
  4. 在系统启动时运行服务包:

    # Grant root access.
    adb root
    # Launch the new service bundle.
    adb shell sdv_service_bundle start \
      local-vm:com.sdv.oem.apex_and_module_name.ServiceBundleName/instance-1
    

开发期间快速更新服务包

当系统映像包含可更新服务 bundle APEX 的早期版本(或在 /<partition>/etc/brand_new_apex/ 中预安装其公开签名密钥)时,您可以部署代码或 bundle 范围的配置更改,而无需重新构建完整的系统映像。如需详细了解全新的 APEX 密钥,请参阅预安装可信公钥。

  • 在 ASfP 中:打开 apex/Android.bp,点击 apex { 代码块旁边的运行图标,然后选择运行“Deploy APEX_NAME”。如需了解详情,请参阅部署 SDV 服务软件包。
  • 通过命令行 (sdv_core_cf):对于具有 updatable: true 的 APEX(例如由 vsidlc --apex 生成的 APEX),在更新预安装的 APEX 时(例如从 1 更新到 2),请在 apex_manifest.json 中递增 "version",仅构建 APEX 模块,并在运行 adb root 后将其推送到 /data/mydir/(与 /data/local/tmp/ 不同,/data/mydir/ 会收到可由更新管理器读取的 system_data_file SELinux 标签)。使用预安装的 sdv_update_manager_client 示例工具(对于 product_specific: true APEX,使用 $OUT/product/apex/;对于 /system 和 /system_ext APEX,使用 $OUT/system/apex/ 和 $OUT/system_ext/apex/)暂存、激活、检查和提交更新:

    m APEX_MODULE_NAME
    adb root && adb wait-for-device
    adb shell mkdir -p /data/mydir
    adb push $OUT/product/apex/APEX_MODULE_NAME.apex /data/mydir/
    adb shell /apex/com.android.sdv.sample.update_manager.client/bin/sdv_update_manager_client \
      prepare service-bundle --boot_attempts 2 \
      --apex_path /data/mydir/APEX_MODULE_NAME.apex
    adb shell /apex/com.android.sdv.sample.update_manager.client/bin/sdv_update_manager_client activate
    adb reboot
    adb wait-for-device && adb root && adb wait-for-device
    adb shell /apex/com.android.sdv.sample.update_manager.client/bin/sdv_update_manager_client status
    # After verifying the bundle is running and state is ACTIVATE_POST_REBOOT_COMPLETE:
    adb shell /apex/com.android.sdv.sample.update_manager.client/bin/sdv_update_manager_client commit
    

    使用 adb reboot 执行正常重启并激活暂存的更新。 如果您稍后停止虚拟机,请在运行 cvd stop 之前使用 adb reboot 刷新待处理的写入操作,然后使用匹配的启动命令(cvd start 或 sdv-cf start --instance_name=instance1 到 instance3)恢复实例,以便 /data 和磁盘叠加层保持不变。使用 cvd create 或 sdv-cf create 创建新实例(或使用 cvd remove 清除叠加层或重新构建完整系统映像)会从基准分区映像启动,而不会应用暂存的 APEX 更新。如需详细了解 APEX 签名密钥和版本规则,请参阅服务 bundle 更新。

服务包依赖项管理

在某些情况下,您可以使用服务包依赖项的静态链接来减小 APEX 大小和服务包内存占用,并缩短服务包的启动时间。这些情况包括 APEX 具有:

  • 单项服务套餐
  • 多个服务包,它们不共享许多通用依赖项

如需启用静态链接,请将以下属性添加到服务软件包库定义中:

// Service bundle library.
rust_ffi_shared {
  // See rust_ffi_shared basic template.
  ...
  // uses static linking for the dependencies
  prefer_rlib: true,
  // needed to correctly resolve commstack deps
  ld_flags: ["-Wl,--no-as-needed"],
}

对于每种情况,我们都建议您确认静态链接到依赖项是否可以改善服务 bundle 的内存占用和开始时间。

调试 (dumpsys)

生命周期管理器会实现 dumpsys 接口。此界面会向用户显示处于 created 或 started 状态的服务包列表。该界面还会显示服务包进程的 FQIN 或 UID。

如需查看服务包的当前状态和标识符,请执行以下操作:

# Grant root access.
adb root

# Execute dumpsys to see service bundles state
adb shell dumpsys google.sdv.lifecycle.ILifecycleManager/default

后续步骤

如需自动执行 vsidlc 并使 IDE 插件能够处理依赖项,请参阅自动目录更新和 LSP 集成。