HarmonyOS Flutter Practice: 09-Existing Flutter projects support HarmonyOS
It turns out that the project developed with Flutter needs to adapt to HarmonyOS.
HarmonyOS Flutter in action: Existing Flutter projects support HarmonyOS
background
It turns out that the project developed with Flutter needs to adapt to HarmonyOS.
Environment setup
See the article [HarmonyOS Flutter Adaptation Guide] to set up a development environment and use fvm to manage multiple versions of SDKs.
Plugins are plugins that rely on native platforms,
components 是平台无关的组件,
common is domain objects, widgets, service classes, extensions, etc., platform-agnostic, and all of them are pure Dart code.
Apps are app coats that are packaged to different platforms by combining different modules.
Use Melos to manage multi-package repositories.
Among them, the projects under apps are the entrance projects that need to be packaged into various platforms and apps. It mainly contains project configuration code, module dependency configuration, and specific platform adaptation code.
Create a new HarmonyOS project in the apps directory, and first run the shell project in HarmonyOS to ensure that there are no problems. Add the dependencies in turn, first with a package written in pure dart, and then with a package that depends on native code/plugins. Pay attention to adding dependencies one by one, do not add too many dependencies at a time, so as to facilitate troubleshooting and positioning problems.
To solve the version dependency problem, the HarmonyOS Flutter project currently needs to rely on version 3.7, if the original project uses a lower version, you can upgrade the original project SDK dependency to 3.7, if the original project SDK version is higher than 3.7, there are two solutions: one is to downgrade the original project SDK dependency to 3.7, and the other is to use a multi-branch solution.
Platform-specific engineering
Create a new project in the apps directory, which runs the HarmonyOS platform adaptation and packaging.
flutter create --platforms ohos ohos_app
The directory structure looks like this:
You can setup on your VS Code folding organising or on the File Explorer of Windows for the DevEco Studio IDE project.
As you can see, the project is just a shell project, without too much code, mainly for some specific configurations of the project, such as themes, routes, etc., as well as the App entrance initialization configuration.
Edit the pubspec.yaml file to add component and module dependencies.
Configure HarmonyOS adaptation for specific plug-ins
Some third-party plug-ins and other libraries that plug-ins depend on, if they are not compatible with HarmonyOS, you can configure the HarmonyOS version through override
dependency_overrides:
# ohos
path_provider:
git:
url: "https://gitee.com/openharmony-sig/flutter_packages.git"
path: "packages/path_provider/path_provider"
Compile and run
Run the Flutter project, view the relevant logs and running interface, and deal with the issues that arise individually.
To view the logs, you can view the Flutter project logs in the IDE debug console where you run Flutter, and you can use the command or DevEco to view the system logs.hdc hilog
[flutter hongmeng technology exchange]
At present, Flutter HarmonyOS has been run through, but there are still some potential pitfalls that need to be solved and dealt with