赞
踩
上一篇:OpenHarmonyOs / LiteOs-a 驱动开发
本文瞎逼介绍一下在 Ubuntu 20.04 系统进行 OpenHarmonyOs / LiteOs-a 应用开发。
开发板用的是润和的 ipcamera_hispark_taurus,芯片是 HI3516DV300。
前一篇文章文末写了个测试的应用程序是没有界面的。
这篇文章准备整两个有界面的应用。
先挖个坑,以后慢慢填,也可能不写了。
OpenHarmonyOs 应用开发的的官方文档 链接
应用2的目的是使用 Ability 框架实现 UI。
应用2是源码自带的示例应用,是用 C++ 写的,用到 OpenHarmonyOs 的 Ability 框架。
关于 Ability,可以参考 这里。还没有投时间研究暂时还没有什么总结,后期如果有再补上。
代码路径在
//foundation/aafwk/aafwk_lite/frameworks/ability_lite/example/
添加编译规则,改这个文件
//build/lite/components/aafwk.json
添加带 “+” 号后面的内容
{ "components": [ { "component": "aafwk_lite", "description": "", "optional": "true", "dirs": [ "foundation/aafwk/aafwk_lite" ], "targets": [ "//foundation/aafwk/aafwk_lite/frameworks/ability_lite:aafwk_abilitykit_lite", + "//foundation/aafwk/aafwk_lite/frameworks/ability_lite/example:hiability", ... ], ... } ] }
修改文件
//foundation/aafwk/aafwk_lite/frameworks/ability_lite/example/BUILD.gn
删除“-”后面的内容,添加带 “+” 号后面的内容
include_dirs = [
...
"${aafwk_lite_path}/interfaces/innerkits/abilitymgr_lite",
"${aafwk_lite_path}/interfaces/kits/ability_lite",
"${aafwk_lite_path}/interfaces/kits/want_lite",
- "${appexefwk_lite_path}/interfaces/kits/bundle_lite",
- "${appexefwk_lite_path}/utils/bundle_lite",
+ "//foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite",
+ "//foundation/appexecfwk/appexecfwk_lite/utils/bundle_lite",
"//foundation/communication/ipc_lite/interfaces/kits",
"//foundation/graphic/utils/interfaces/innerkits",
"//foundation/graphic/ui/interfaces/kits",
编译系统
新建目录 example,将编译生成的 libhiability.so 文件和应用目录下的 config.json 拷贝进 example 目录
mkdir /mnt/hgfs/share/example
cp out/hispark_taurus/ipcamera_hispark_taurus/dev_tools/example/libhiability.so /mnt/hgfs/share/example
cp foundation/aafwk/aafwk_lite/frameworks/ability_lite/example/config.json /mnt/hgfs/share/example
(/mnt/hgfs/share/ 为虚拟机和 Win10 共享文件夹路径,配置见 这里 的 “前期准备”小节)
然后在 example 内新建路径用来存放应用图标。(也可以不用图标,安装完应用后图标是透明的)
mkdir /mnt/hgfs/share/example/assets/entry/resources/base/media/ -p
在此路径新建一个名为 icon.png 的 png 图片作为应用图标。
可以用 Win10 的画图工具瞎逼画一个,比如:
在这里先修改一下 config.json 文件,这里有个语法错误,会导致安装的时候失败:
resultMessage is ERR_APPEXECFWK_INSTALL_FAILED_PARSE_PROFILE_ERROR
修改:
"deviceConfig": {
"default": {
"keepAlive": false
- },
+ }
},
将 example 里的内容打包压缩成 zip 格式文件(注意不是压缩 example 目录),改名为 example.hap
可以用 sd 卡 copy 到开发板安装,也可以连网线配置网络文件系统利用共享目录 copy 文件安装。
这里用到 sd 卡安装。sd 卡格式化为 fat32,将 example.hap 和编译生成的目录
//out/hispark_taurus/ipcamera_hispark_taurus/dev_tools/
一起拷贝进 sd 卡,然后将 sd 卡插入开发板。
可以在串口终端查看新生成的设备节点,多了一个分区 mmcblk1p0
ls /dev/mmc
mmcblk0p2 mmcblk0p1 mmcblk0p0 mmcblk1p0 mmcblk1 mmcblk0
在串口终端输入命令将 mmcblk1p0 分区挂载到 /sdcard 目录
mount /dev/mmcblk1p0 /sdcard vfat
mount ok
进入 /sdcard 目录,先将应用签名机制 disable 掉,然后安装应用
cd /sdcard
./dev_tools/bin/bm set -s disable
./dev_tools/bin/bm install -p example.hap
安装完成后桌面显示应用的图标
查看包名
cat foundation/aafwk/aafwk_lite/frameworks/ability_lite/example/config.json
包名为这行
"bundleName": "com.huawei.hiability",
运行:点击桌面新生成的图标,或者命令
./dev_tools/bin/aa start -p com.huawei.hiability -n MainAbility
?怎么是这个叼样
应该是应用 UI 布局有问题,修改一下代码,并且添加按键和点击响应
文件
//foundation/aafwk/aafwk_lite/frameworks/ability_lite/example/entry/src/main/cpp/main_ability_slice.cpp
修改
#include "main_ability_slice.h" #include "ability_manager.h" -#include "components/ui_label.h" -#include "components/ui_label_button.h" #include "next_ability_slice.h" ... constexpr static int BUTTON1_POSITION_X = 50; constexpr static int BUTTON1_POSITION_Y = 50; +constexpr static int BUTTON2_POSITION_X = 150; +constexpr static int BUTTON2_POSITION_Y = 150; constexpr static int BUTTON_WIDTH = 200; -constexpr static int BUTTON_HEIGHT = 30; +constexpr static int BUTTON_HEIGHT = 100; constexpr static int ROOT_VIEW_POSITION_X = 0; constexpr static int ROOT_VIEW_POSITION_Y = 0; -constexpr static int ROOT_VIEW_WIDTH = 300; -constexpr static int ROOT_VIEW_HEIGHT = 500; +constexpr static int ROOT_VIEW_WIDTH = 960; +constexpr static int ROOT_VIEW_HEIGHT = 480; constexpr static uint8_t ROOT_VIEW_OPACITY = 255; constexpr static uint8_t FONT_ID = 10; ... void MainAbilitySlice::OnStart(const Want &want) { AbilitySlice::OnStart(want); - auto button1 = new UILabel(); + button1 = new UILabel(); button1->SetPosition(BUTTON1_POSITION_X, BUTTON1_POSITION_Y); button1->SetText("鸿蒙应用框架 Main AS"); button1->Resize(BUTTON_WIDTH, BUTTON_HEIGHT); ... button1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Yellow().full); button1->SetStyle(STYLE_BACKGROUND_OPA, ROOT_VIEW_OPACITY); + auto onClick1 = [this] (UIView& view, const Event& event) -> bool { + static bool a = false; + if (a) + button1->SetText("AAAAAAAAAAA"); + else + button1->SetText("BBBBBBBBBBB"); + a = !a; + return true; + }; + auto exitListener_1 = new EventListener(onClick1, nullptr); + button1->SetOnClickListener(exitListener_1); + button1->SetTouchable(true); + + button2 = new UILabel(); + button2->SetPosition(BUTTON2_POSITION_X, BUTTON2_POSITION_Y); + button2->SetText("鸿蒙应用框架 Main AS"); + button2->Resize(BUTTON_WIDTH, BUTTON_HEIGHT); + button2->SetFontId(FONT_ID); + + button2->SetStyle(STYLE_TEXT_COLOR, Color::Black().full); + button2->SetStyle(STYLE_TEXT_OPA, ROOT_VIEW_OPACITY); + button2->SetStyle(STYLE_BACKGROUND_COLOR, Color::Yellow().full); + button2->SetStyle(STYLE_BACKGROUND_OPA, ROOT_VIEW_OPACITY); + + auto onClick2 = [this] (UIView& view, const Event& event) -> bool { + TerminateAbility(); + return true; + }; + auto exitListener_2 = new EventListener(onClick2, nullptr); + button2->SetOnClickListener(exitListener_2); + button2->SetTouchable(true); + rootView_ = RootView::GetWindowRootView(); rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y); rootView_->Resize(ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT); rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Olive().full); rootView_->Add(button1); + rootView_->Add(button2); SetUIContent(rootView_); } ... void MainAbilitySlice::OnStop() { printf("MainAbilitySlice::OnStop\n"); + delete button1; + delete button2; AbilitySlice::OnStop(); DeleteViewChildren(rootView_); }
文件
//foundation/aafwk/aafwk_lite/frameworks/ability_lite/example/entry/src/main/cpp/main_ability_slice.h
修改
#include "ability_loader.h"
#include "event_listener.h"
+#include "components/ui_label.h"
+#include "components/ui_label_button.h"
...
class MainAbilitySlice : public AbilitySlice {
...
private:
+ UILabel *button1;
+ UILabel *button2;
RootView *rootView_ { nullptr };
};
重新编译打包运行,效果
点击一下按钮
关闭应用
./dev_tools/bin/aa stopability -p com.huawei.hiability -n MainAbility
卸载应用
./dev_tools/bin/bm uninstall -n com.huawei.hiability
应用3的目的是实现 js 调用 C++ 接口。
UI 用 html / css / js 语言写。流程为 js 通过系统的 ACE 机制调 C++ 库,然后 C++ 再调内核接口。
UI 开发可以参考文档 这里,可能有的功能只支持标准系统。
需要用到鸿蒙的 DevEco 软件
官网获取 DevEco
DevEco 安装(略)
SDK 安装(略)
打开 DevEco
点 file → new → new project
选择 [Lite]Empty Ability,点 Next
填写项目信息,Project type 选择 Application,API版本选 API Version 7,然后点 Finish
然后会在路径
C:\Users\Admin\DevEcoStudioProjects\MyApplication
下建立项目。
源码目录下将
//foundation/ace/ace_engine_lite/frameworks/examples/music/src/main/js/default
复制到所建项目的路径
\\MyApplication\entry\src\main\js\
覆盖里面的原 default 目录
在 SDK 目录下新建文件
\\SDK\js\3.0.0.1\api\common\@ohos.sample.d.ts
新添加一个 js 接口
export default class Sample {
static testCallFunc(): void;
}
修改文件
\\MyApplication\entry\src\main\js\default\pages\index\index.js
也可以在 DevEco 打开修改
添加新模块和新接口 sample.testCallFunc()
-import audio from '@system.audio'
+import audio from '@ohos.multimedia.audio'
import file from '@system.file'
import app from '@system.app'
+import sample from '@ohos.sample'
...
playMusic() {
console.log("playMusic:start");
+ sample.testCallFunc();
...
这样应用就可以直接使用这个接口了。点 Build → Build Hap(s) / APP(s) → Build Hap(s)
编译完了之后选择
\\MyApplication\entry\build\outputs\hap\debugentry-debug-lite-unsigned.hap
或者在 DevEco 左边框框的点右键 debugentry-debug-lite-unsigned.hap → Open In → Explorer
copy 到 sdcard。
C++ 的接口在例程源码已经实现
//foundation/ace/ace_engine_lite/frameworks/src/core/modules/sample_module.cpp
还需要打开宏开关 ENABLE_MODULE_REQUIRE_TEST。在
//foundation/ace/ace_engine_lite/frameworks/targets/liteos_a/acelite_config.h
添加宏
+#ifndef ENABLE_MODULE_REQUIRE_TEST
+#define ENABLE_MODULE_REQUIRE_TEST 1
+#endif
在
/foundation/ace/ace_engine_lite/frameworks/src/core/modules/sample_module.cpp
添加一句 log
JSIValue SampleModule::TestCallFunc(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
{
+ HILOG_ERROR(HILOG_MODULE_ACE, "SampleModule: TestCallFunc called");
return JSI::CreateUndefined();
}
然后编译系统。刷机。开机后安装 sdcard 里的 debugentry-debug-lite-unsigned.hap
打开,点击播放按钮
串口终端打印 log
OHOS # 01-01 00:02:05.421 17 105 D 03B00/JS-3RD-APP: [Console Error] [Console Debug] playMusic:start
01-01 00:02:05.421 17 105 E 03900/ACE: SampleModule: TestCallFunc called
ACE 机制有缘再捋捋。
等有能力讲讲内核逻辑或者系统移植 [凋谢]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。