建構及部署服務套裝組合

建立中介軟體並編寫自訂程式碼後,即可將服務套件部署為 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)
      • 權限聲明和自動化調度管理設定
      • 定義所有必要 Soong 模組的 Android.bp 檔案。

      產生的 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
    

在開發期間快速更新服務套件

如果系統映像檔包含舊版可更新服務套件 APEX (或在 /<partition>/etc/brand_new_apex/ 中預先安裝公開簽署金鑰),您就能部署程式碼或套件範圍的設定變更,不必重新建構完整系統映像檔。如要進一步瞭解全新的 APEX 金鑰,請參閱「預先安裝信任的公開金鑰」。

  • 在 ASfP 中:開啟 apex/Android.bp,按一下 apex { 區塊旁的「Run」圖示,然後選取「Run '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 執行乾淨重新啟動,並啟用分階段更新。如果之後停止 VM,請先使用 adb reboot 清除待處理的寫入作業,再執行 cvd stop,然後使用相符的啟動指令 (cvd start 或 sdv-cf start --instance_name=instance1 至 instance3) 繼續執行執行個體,這樣 /data 和磁碟疊加層就會保持不變。使用 cvd create 或 sdv-cf create 建立新執行個體 (或使用 cvd remove 清除疊加層,或重新建構完整系統映像檔),會從基準分區映像檔啟動,不含暫存的 APEX 更新。如要進一步瞭解 APEX 簽署金鑰和版本規則,請參閱「服務套件更新」。

服務套件依附元件管理

在某些情況下,您可以使用服務套件依附元件的靜態連結,減少 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"],
}

無論是哪種情況,建議您確認靜態連結至依附元件,是否能改善服務套件的記憶體用量和開始時間。

偵錯 (dumpsys)

生命週期管理工具會實作 dumpsys 介面。這個介面會向使用者顯示處於 created 或 started 狀態的服務套件清單。介面也會顯示服務套件程序的 FQIN 或 UID。

如要查看服務套件的目前狀態和 ID,請執行下列指令:

# Grant root access.
adb root

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

後續步驟

如要自動執行 vsidlc 並啟用 IDE 外掛程式來處理依附元件,請參閱「自動更新目錄和 LSP 整合」。