目录

Independent Process Development Guide

Overview

In multi-module application development scenarios, developers often face issues such as mutual interference between modules and overall crashes caused by exception propagation. The process isolation mechanism allows developers to configure different functional modules to run in independent processes, achieving process isolation between modules and improving application stability and security. This mechanism is applicable to scenarios that require module isolation, fault isolation, and security isolation.

The APIs of this module can be used only in the Stage model.

Static Specification of Process

[h2]Scenario Introduction

Static specification of process applies to scenarios where process allocation can be determined at compile time, for example, isolating certain functional components into an independent process to improve application stability and security. When multiple UIAbilities or EmbeddedUIExtensionAbilities in the same application need to run in different processes, developers can statically specify the process to run by configuring the process field in the abilities tag or extensionAbilities tag of the module.json5 configuration file. When starting a UIAbility or EmbeddedUIExtensionAbility, the system allocates the process based on the process field.

Typical scenarios include:

[h2]Constraints

[h2]How to Develop

This sample code uses UIAbility as an example to describe how to configure static process specification. In the module.json5 configuration file of the UIAbility that needs to run in an independent process, add the process field to the corresponding ability under the abilities tag. The process field value starts with ":", indicating an application private process. Multiple abilities configured with the same process field value will run in the same process. In the following example, EntryAbility does not have the process field configured and runs in the default process. EntryAbility1 and EntryAbility2 both have the process field configured as ":processTag", so they will run in the same independent process, with the process name "application bundle name:processTag":

{
  "module": {
// ...
    "abilities": [
// ...
      {
        "name": "EntryAbility1",
        "srcEntry": "./ets/entryability1/EntryAbility1.ets",
// ...
        "process": ":processTag"
      },
      {
        "name": "EntryAbility2",
        "srcEntry": "./ets/entryability2/EntryAbility2.ets",
// ...
        "process": ":processTag"
      }
    ],
// ...
  }
}

Dynamic Specification

[h2]Scenario Introduction

Dynamic specification applies to scenarios where the running process is dynamically determined at runtime, for example, dynamically isolating certain components into an independent process to improve application stability and security. For UIAbility instances, developers need to set the isolationProcess field to true in the abilities tag corresponding to the UIAbility. When the system starts the UIAbility instance, the Main Control Process triggers the onNewProcessRequest callback method to return a string that identifies the independent process. The name of the independent process where the UIAbility runs is concatenated as "bundle name:string returned by the onNewProcessRequest callback method". If the returned string has been created by the developer before, the process corresponding to that identifier is reused; otherwise, a new process is created.

Typical scenarios include:

[h2]Constraints

[h2]How to Develop

This sample code uses UIAbility as an example to describe how to develop dynamic specification.

  1. Configure dynamic process specification.

    Perform two configurations in the module.json5 configuration file: first, specify the AbilityStage source file path through srcEntry under configuration file tags; then, add the isolationProcess field and set it to true for the UIAbility that requires dynamic process specification under the abilities tag.

    In the following example, the UIAbility instance named EntryAbility1 has isolationProcess set to true, and its running process is dynamically specified by the string returned by the onNewProcessRequest callback method of AbilityStage.

    {
      "module": {
    // ...
        "srcEntry": "./ets/MyAbilityStage/MyAbilityStage.ets",
    // ...
        "abilities": [
    // ...
          {
            "name": "EntryAbility1",
            "srcEntry": "./ets/entryability1/EntryAbility1.ets",
    // ...
            "isolationProcess": true
          }
        ],
    // ...
      }
    }
  2. Implement the onNewProcessRequest callback method of AbilityStage.

    In the AbilityStage source file specified by srcEntry, implement the onNewProcessRequest callback method. When the system starts a UIAbility instance with isolationProcess set to true, it triggers this callback method, and then dynamically specifies the independent process in which the UIAbility instance runs based on the string returned by this method.

    import AbilityStage from '@ohos.app.ability.AbilityStage';
    
    export default class MyAbilityStage extends AbilityStage {
    // ...
      onNewProcessRequest(want: Want): string {
        //Return the developer-defined string. The independent process name concatenation rule is: bundle name:developer-defined string.
        return 'testKey';
      }
    }

Module Independent Process

[h2]Scenario Introduction

For an application with multiple HAPs, different HAPs may carry different business functions. When the business of each HAP is relatively independent and needs to run in isolation, developers can configure module independent processes so that the UIAbility of different HAPs runs in different processes (the process name format is "application bundle name:module name"), thereby achieving business isolation and preventing exceptions in one module from affecting the running of other modules.

Typical scenarios include:

[h2]Constraints

[h2]How to Develop

When the UIAbility of the same HAP needs to run in an independent process, add the isolationMode field under the configuration file tag in the module.json5 configuration file of the HAP module, and set it to "isolationOnly" or "isolationFirst".

The following example uses the entry module configured with isolationOnly mode as an example:

{
  "module": {
    "name": "entry",
// ...
    "isolationMode": "isolationOnly",
// ...
    "abilities": [
// ...
    ],
    "extensionAbilities": [
// ...
    ]
  }
}