# Using a Module in Hybrid Development

This topic describes how to reference a flutter_module project in an ohos project.

- ohos project: a native OpenHarmony project that can be created by DevEco Studio.
- flutter_module project: a Flutter submodule project that can be created by executing Flutter instructions.

## Environment Setup

- Use [flutter_flutter](https://gitcode.com/openharmony-tpc/flutter_flutter) to configure environment variables.
- Use the engine products generated by [flutter_engine](https://gitcode.com/openharmony-tpc/flutter_engine).(optional)‌

Use DevEco Studio to create a native OpenHarmony project named **MyApplication**.

## flutter_module Project Operations

```sh
# If in MacOS/Linux environment
# 1. Create a Flutter submodule project.
flutter create -t module my_flutter_module
# 2. Compile and generate flutter_module.har.
cd my_flutter_module
flutter build har --debug
cd -
# 3. Copy EntryAbility.ets and Index.ets to MyApplication.
cp my_flutter_module/.ohos/entry/src/main/ets/entryability/EntryAbility.ets MyApplication/entry/src/main/ets/entryability/EntryAbility.ets
cp my_flutter_module/.ohos/entry/src/main/ets/pages/Index.ets MyApplication/entry/src/main/ets/pages/Index.ets
```

## ohos Project Operations

An ohos project can reference a Flutter project in either of the following ways:

### Method 1: Referencing the HAR File

#### 1. Copy the HAR file.

```sh
# If in MacOS/Linux environment
cp -r my_flutter_module/.ohos/har/* MyApplication/har/
```

### 2. Modify the **MyApplication/oh-package.json5** file.

```json
{
  // ...
  "dependencies": {
    // If the useNormalizedOHMUrl field in the project-level build-profile.json5 file is set to true, the alias used by the dependent package in oh-package.json5 must be the same as the name of oh-package.json5 in the dependent package. Otherwise, an error will be reported during compilation.
    "@ohos/flutter_module": "har/flutter_module.har",
    "@ohos/flutter_ohos": "har/flutter.har"
  },
  "overrides": {
    // Conflicts in dependencies need to be eliminated here.
    "@ohos/flutter_ohos": "har/flutter.har",
    "plugin_xxx": "har/plugin_xxx.har"
  }
}
```

#### 3. Use DevEco Studio to configure the signature of **MyApplication**.

#### 4. Run **MyApplication**.

### Method 2: Directly Referencing the flutter_module Source Code

#### 1. Copy the flutter_module source code and Flutter-related files.

```sh
# If in MacOS/Linux environment
cp -r my_flutter_module/.ohos/flutter_module MyApplication/
mkdir -p MyApplication/flutter_module/src/main/resources/rawfile
cp -r my_flutter_module/.ohos/flutter_module/src/main/resources/rawfile/flutter_assets MyApplication/flutter_module/src/main/resources/rawfile
cp my_flutter_module/.ohos/har/flutter.har MyApplication/har/flutter.har
```

#### 2. Modify the **MyApplication/build-profile.json5** file.

```json
{
  // ...
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    },
    // The following is the content newly added:
    {
      "name": "flutter_module",
      "srcPath": "./flutter_module",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    }
  ]
}
```

#### 3. Modify the **MyApplication/oh-package.json5** file.

```json
{
  // ...
  "dependencies": {
    "flutter_module": "./flutter_module",
    "@ohos/flutter_ohos": "./har/flutter.har"
  },
  "overrides": {
    "@ohos/flutter_ohos": "./har/flutter.har"
  }
}
```

#### 4. Use DevEco Studio to configure the signature of **MyApplication**.

#### 5. Run **MyApplication**.
