DEV Community

flfljh
flfljh

Posted on

Detailed Guide to Developing Flutter Plugins for HarmonyOS

Detailed Guide to Developing Flutter Plugins for HarmonyOS

0. Environment Setup

Prerequisite: Configure HarmonyOS Flutter environment
Reference: HarmonyOS Flutter Environment Setup Guide
Note: Downloading and compiling the engine is time-consuming. Pre-built engines are available upon request.


1. Obtain Original Plugin Code

git clone <original_plugin_repository>
Enter fullscreen mode Exit fullscreen mode

2. Generate OHOS Directory

# Option 1: Direct generation (may fail)
flutter create -t plugin --platforms ohos

# Option 2: Create demo project and copy
flutter create -t plugin --platforms ohos demo
cp -r demo/ohos ./
Enter fullscreen mode Exit fullscreen mode

3. Configure Local Dependency

Add local dependency in pubspec.yaml:

dependencies:
  your_plugin:
    path: ../path_to_plugin
Enter fullscreen mode Exit fullscreen mode

iwEcAqNwbmcDAQTRCQAF0QS4BrDBDM9ETs38UAcIagyL5gAAB9IGLKvECAAJomltCgAL0gALGaw.png_620x10000q90

4. Build Plugin Package

flutter build hap \
  --local-engine-src-path /path/to/ohos_flutter/src \
  --local-engine ohos_release_arm64
Enter fullscreen mode Exit fullscreen mode

Successful Output:

√ Built build/hap/release/entry-release.hap (XX.XMB)
Enter fullscreen mode Exit fullscreen mode

5. Verify Generated Files

After successful build:

1

6. Locate Plugin HAR Package

Plugin output directory:

your_plugin/ohos/build/outputs/
└── har
    └── your_plugin_ohos.har  # Plugin package
Enter fullscreen mode Exit fullscreen mode

2

7. Add HAR to Demo Project

Copy HAR to demo project:

bash

cp your_plugin_ohos.har demo_portal/module/ohos/libs/
Enter fullscreen mode Exit fullscreen mode

3

8. Configure HAR Dependency

4

9. Initialize Plugin

5

10. Test Plugin Functionality

6

7

11. Develop Native Implementation

Follow these steps for native development:

  1. Analyze existing iOS/Android native logic
  2. Create HarmonyOS equivalent in ohos/src/main/cpp/
  3. Implement platform interface:cpp

Top comments (0)