Table of Contents

Introduction

Caution

Before the project is ready for production, it may contain unstable APIs and features, including breaking changes. Please use with caution.

Environment Setup

Note

The following methods apply to Windows. We have not yet tested this workflow on Linux/macOS.

  • Zig provides linker/sysroot support for OpenHarmony-NET.PublishAotCross to allow cross-compilation for linux-x64/linux-arm64/linux-musl-x64/linux-musl-arm64 targets on Windows
# Install Zig on Windows via winget
winget install zig.zig
Warning

It is not recommended to add clang to the environment variables, as it may conflict with the build process.

  • LLVM is needed for the compilation process that uses llvm-objcopy.exe, ensure it exists in your environment variables

  • .NET 9.0 SDK or greater

  • Latest version of DevEco Studio

  • Optional Visual Studio 2022

  • Optional JetBrains Rider

Build and Run

  • Pull the latest code from OpenHarmony.Avalonia, ensure to clone with the --recursive option to include submodules
git clone https://github.com/OpenHarmony-NET/OpenHarmony.Avalonia.git --recursive # Include submodules
  • Enter the project directory OpenHarmony.Avalonia
|-- Directory.Build.props
|-- OHOS_Project
|-- OpenHarmony.Avalonia.sln
|-- README.md
|-- Src
|-- ThirdParty

3 directories, 3 files

Then execute the following command or use Visual Studio 2022 to open the OpenHarmony.Avalonia.sln solution, right-click on the Entry project and publish

Note

arm64-v8a is suitable for physical devices, while x86_64 is for emulators. Please choose according to your device type.

dotnet publish  ./Src/Entry/Entry.csproj -c Release -r linux-musl-arm64 -p:PublishAot=true  -o OHOS_Project/entry/libs/arm64-v8a

Or select PublishArm64.pubxml in the publish page, then click the publish button

  • Open DevEco Studio, open the OHOS_Project directory, then click the run button. DevEco Studio will automatically install and run the application on your physical device/emulator
Note

Running on a physical device may require signing and other procedures, consistent with normal HarmonyOS software development

Run Your Own Project

  • Add your project reference to Entry (i.e., the Entry project references your project)

And replace this namespace with your own software's namespace

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Avalonia.OpenHarmony;
using OpenHarmony.NDK.Bindings.Native;
- using AOOH_Gallery;
+ using YourProjectNamespace;

namespace Entry;

Also change the App class's project affiliation

    try
    {
        Ace.OH_NativeXComponent_RegisterOnFrameCallback(component, &OnSurfaceRendered);
        if (XComponents.TryGetValue((nint)component, out var xComponent))
        return;
+            xComponent = new AvaloniaXComponent<App>((nint)component, (nint)window);
        XComponents.Add((nint)component, xComponent);
        xComponent.OnSurfaceCreated();
    }