How do I create a Cordova app for HarmonyOS?
Short answer: The CPF-Cordova project brings Apache Cordova to HarmonyOS. You can create a project with the hcordova command-line tool, or integrate the cordova-openharmony framework manually into a DevEco Studio project and copy your web assets (www) into it.
What is CPF-Cordova?
CPF-Cordova is the Cordova Special Interest Group under the Cross-Platform Framework PMC. Its goal is to let web developers use existing HTML, CSS and JavaScript skills, and to adapt existing Android and iOS Cordova projects with minimal effort. It contains:
- cordova-openharmony: the framework for HarmonyOS.
- hcordova: a CLI for project creation.
- About 82 plugin repositories: camera, geolocation, media, Alipay and WeChat payments, Bluetooth, NFC, databases and more.
Everything is licensed under Apache License 2.0.
How do I use the hcordova CLI?
hcordova is based on Cordova and targets cordova@13.0.0 compatibility. It needs Node.js 16 or later (18 LTS recommended) and Git. For the ohos platform you also need DevEco Studio or the command-line tools.
npm install -g hcordova
hcordova -v
hcordova create <directory> <package-name> <app-name>
hcordova platform add ohos
hcordova plugin add <plugin-name>
Other commands: hcordova platform ls, hcordova platform remove <platform>, hcordova plugin ls and hcordova plugin remove <plugin-name>. You can also add the browser platform and run hcordova run browser to test in a browser. The README targets Android 7.0+, iOS 12.0+ and HarmonyOS 5.0+.
I could not find a documented build command for the ohos platform in the README, so check the repository for the current build and run steps.
How do I integrate the framework manually?
Requirements for cordova-openharmony: SDK API 12 or later (tested on 5.0.0), DevEco Studio 5.0 or later (tested on 6.0.13.200), ROM 5.1 or later, OpenHarmony 6.0 or later for the emulator, and OpenSSL integrated before compilation. See How to install DevEco Studio.
-
In DevEco Studio, create a project with the Empty Ability template. See Your first ArkTS app.
-
Download the Cordova HAR package and extract it as a
cordovadirectory in the main project. -
From the
entrydirectory, run:ohpm install ../cordova -
Add the cordova module to
entry/build-profile.json5. -
Copy your Android
assets/wwwfolder toentry/src/main/resources/rawfile/. -
Call
webview.WebviewController.initializeWebEngine()inEntryAbility.onCreate(). -
Edit
Index.etsto import and use theMainPagecomponent.
Then build and run from DevEco Studio. The README describes this flow through the IDE and gives no separate CLI build commands.
How do plugins work?
Plugins follow Cordova standards. To write your own, extend the CordovaPlugin class in ArkTS and register it in Index.ets through the cordovaPlugs array parameter. The native layer is C++ in src/main/cpp/ (networking, SSL, cookies, plugin management), with the ArkTS UI layer in ets/.
The framework also supports multiple webviews, native embedding, custom scheme registration, and loading online and offline content at the same time.
Next steps
Frequently asked questions
What is CPF-Cordova?
It is the Cordova SIG under the Cross-Platform Framework PMC. It maintains cordova-openharmony (the framework), hcordova (a CLI) and about 82 plugin repositories, all under Apache License 2.0.
What is hcordova?
A command-line tool based on Cordova, targeting cordova@13.0.0 compatibility, that creates projects and manages platforms (ohos, android, ios) and plugins. Install it with npm install -g hcordova.
What are the requirements for the framework?
According to the cordova-openharmony README: SDK API 12 or later (tested on 5.0.0), DevEco Studio 5.0 or later, ROM 5.1 or later, OpenHarmony 6.0 or later for the emulator, and OpenSSL integrated before compilation.
Can I reuse my Android or iOS Cordova project?
Largely yes. The framework's interfaces are compatible with the Android and iOS versions, and the integration steps include copying the Android assets/www folder into the HarmonyOS project.
How do I write a custom plugin?
Extend the CordovaPlugin class in ArkTS and register the plugin in Index.ets through the cordovaPlugs array parameter.