
??想了解更多關(guān)于開源的內(nèi)容,請訪問:??
??51CTO 開源基礎軟件社區(qū)??
??https://ost.51cto.com??
前言
本文將介紹如何使用cpp編寫用于小型系統(tǒng)的app。
一、ability相關(guān)介紹
Ability是應用所具備能力的抽象,也是應用程序的重要組成部分。Ability是系統(tǒng)調(diào)度應用的最小單元,是能夠完成一個獨立功能的組件。一個應用可以包含一個或多個Ability。其中ability又分為Page類型的和Service類型的,前者是為用戶提供人機交互能力的,后者是提供后臺任務機制的,簡單來講就是Page帶界面,Service不帶界面。這里將重點介紹Page類型的ability。

使用到的子系統(tǒng)有ability子系統(tǒng)、包管理子系統(tǒng)和圖形ui子系統(tǒng)。ability子系統(tǒng)是管理OpenHarmony應用運行狀態(tài)的開發(fā)框架;包管理子系統(tǒng)是OpenHarmony為開發(fā)者提供的安裝包管理框架;圖形ui子系統(tǒng)提供基礎UI組件和容器類組件。

二、簡單實現(xiàn)1、ability和abilityslice
1、ability和abilityslice
abilityslice是單個頁面及其控制邏輯的總和,是Page類型Ability特有的組件,一個Page類型的Ability可以包含多個AbilitySlice,此時,這些頁面提供的業(yè)務能力應當是高度相關(guān)的。

2、生命周期
整體流程下來大致有OnStart()、OnAvtive()、OnInactive()、OnBackground()和OnStop()五階段。abilityslice生命周期與ability相似,但是仍要區(qū)分。

3、hello world
./helloworld/
├── config.json //配置文件
├── resource //資源
└── src //主要文件
├── include
│ ├── main_ability.h
│ └── main_ability_slice.h
└── main
├── main_ability.cpp
└── main_ability_slice.cpp
首先定義并注冊ability。
// main_ability.h
#ifndef HELLO_MAIN_ABILITY_H
#define HELLO_MAIN_ABILITY_H
#include "ability_loader.h"
namespace OHOS {
class MainAbility : public Ability {
protected:
void OnStart(const Want &want) override; //Want結(jié)構(gòu)體,ability的相關(guān)信息
/*
* 由于在這里我們只要簡單的展示helloworld標簽,其它函數(shù)不需要重載。
*/
// void OnInactive() override;
// void OnActive(const Want &want) override;
// void OnBackground() override;
// void OnStop() override;
};
}
#endif
//main_ability.cpp
#include "main_ability.h"
namespace OHOS {
REGISTER_AA(MainAbility) //使用REGISTER_AA注冊ability
void MainAbility::OnStart(const Want &want)
{
printf("This is MainAbility OnStart status!\r\n");
SetMainRoute("MainAbilitySlice"); //設置主頁面為MainAbilitySlice,這要與后續(xù)的slice名字匹配
Ability::OnStart(want);
}
}

最后編寫slice界面。
//main_ability_slice.h
#ifndef HELLO_ABILITY_SLICE_H
#define HELLO_ABILITY_SLICE_H
#include "ability_loader.h"
#include "ability_manager.h"
#include "bundle_manager.h"
#include "components/ui_label.h"
namespace OHOS {
class MainAbilitySlice : public AbilitySlice { //創(chuàng)建AbilitySlice類 與上面同名
public:
MainAbilitySlice() = default;
virtual ~MainAbilitySlice();
protected:
void OnStart(const Want &want) override;
/*
* 同理
*/
// void OnInactive() override;
// void OnActive(const Want &want) override;
// void OnBackground() override;
// void OnStop() override;
};
}
#endif
//main_ability_slice.cpp
#include "main_ability_slice.h"
const int screen_width = 720;
const int screen_height = 1280;
namespace OHOS {
REGISTER_AS(MainAbilitySlice)
MainAbilitySlice::~MainAbilitySlice()
{
printf("This is ~MainAbilitySlice!\r\n");
}
void MainAbilitySlice::OnStart(const Want& want)
{
AbilitySlice::OnStart(want);
RootView* rootView_ = RootView::GetWindowRootView(); //創(chuàng)建底層界面
rootView_->SetPosition(0, 0, screen_width, screen_height);
rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Black()));
rootView_->SetFocusable(true);
rootView_->SetInterceptFocus(false);
UILabel* label = new UILabel(); //創(chuàng)建label寫入Hello World
label->SetPosition(0, 0, 720, 64);
label->SetText("Hello World!");
label->SetFont("SourceHanSansSC-Regular.otf", 64);
label->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
rootView_->Add(label); //將label放入rootView
SetUIContent(rootView_); //設置顯示RootView UI
}
}
#endif

4、config.json的編寫
//config.json
{
"app": {
"bundleName": "com.sample.hello",
"vendor": "sample",
"version": {
"code": 1,
"name": "1.0"
},
"apiVersion": {
"compatible": 3,
"target": 4
}
},
"deviceConfig": {
"default": {
}
},
"module": {
"package": "com.sample.hello",
"name": ".MyHarmonyAbilityPackage",
"deviceType": [
"phone",
"tv",
"tablet",
"pc",
"car",
"smartWatch",
"sportsWatch",
"smartVision"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "hello",
"moduleType": "entry"
},
"abilities": [ //ability配置聲明
{
"name": "MainAbility",
"label": "hello world app",
"launchType": "standard",
"type": "page",
"visible": true
}
]
}
}
三、hap編譯
1、通過BUILD.gn與系統(tǒng)一并編譯。
使用到編譯鏈中的hap_pack,添加配置 import(“//build/lite/config/hap_pack.gni”)
import("http://build/lite/config/hap_pack.gni")
shared_library("hello") {
sources = [
"src/main/main_ability.cpp",
"src/main/main_ability_slice.cpp"
] #將主要文件編譯出庫
deps = [
"${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite",
"${appexecfwk_lite_path}/frameworks/bundle_lite:bundle",
"http://foundation/graphic/ui:lite_ui",
"http://foundation/graphic/utils:lite_graphic_utils",
"http://foundation/systemabilitymgr/samgr_lite/samgr:samgr",
]
include_dirs = [
"src/include",
"${aafwk_lite_path}/interfaces/kits/ability_lite",
"${aafwk_lite_path}/interfaces/kits/want_lite",
"${appexecfwk_lite_path}/interfaces/kits/bundle_lite",
]
ldflags = [ "-shared" ]
ldflags += [ "-lstdc++" ]
ldflags += [ "-L$ohos_root_path/sysroot/usr/lib" ]
ldflags += [ "-Wl,-rpath-link=$ohos_root_path/sysroot/usr/lib" ]
ldflags += [
"-lui",
"-lability",
] #添加依賴
defines = [
"ENABLE_WINDOW=1",
"ABILITY_WINDOW_SUPPORT",
"OHOS_APPEXECFWK_BMS_BUNDLEMANAGER",
] #配置定義
}
hap_pack("hello_hap") { #打包成hap
deps = [ ":hello" ]
mode = "hap"
json_path = "config.json"
ability_so_path = "$root_out_dir/libhello.so" #編譯后的庫文件
force = "true"
cert_profile = "com.huawei.launcher_AppProvision_release.p7b" #由于不清楚獲取證書方法 先用源碼案例自帶的證書代替
resources_path = "resources"
hap_name = "hello"
}
2、 通過app_packing_tool單獨編譯
該打包工具在源碼目錄developtools/packing_tool/jar下。
主要參數(shù)如下:
命令參數(shù) | 對應的資源文件 | 說明 | 是否可缺省 |
–mode | - | 為“hap”字段,打包生成Hap | 否 |
–json-path | 清單文件config.json | - | 否 |
–ability-so-path | 主功能so文件 | - | 是 |
–out-path | - | 生成的Hap包輸出路徑,默認為當前目錄 | 是 |
具體操作:
還是得先將動態(tài)庫編譯出來。
然后將動態(tài)庫libhello.so和config.json放到一個文件夾里。
./out/
├── config.json
└── libhello.so
最后使用java -jar app_packing_tool.jar 進行打包 如下:
java -jar app_packing_tool.jar |
--mode hap |
--json-path ./config.json |
--ability-out-path ./libhello.so |
--out-path ./hello.hap
四、hap安裝
1、安裝命令bm
由于小型系統(tǒng)不支持使用HDC工具,我們需要使用到bm命令進行安裝程序。
bm set -s disable //取消簽名安裝。
bm install -p system/internal/hello.hap //使用BUILD.gn一起編譯的hap默認會在這個路徑,如果使用工具打包的,視情況填寫路徑。
2、相關(guān)參數(shù)
# bm
Usage: install hap-path [options]
Description:
--help|-h help menu
--happath|-p location of the hap to install
Usage: uninstall bundle-name [options]
Description:
--help|-h help menu
--bundlename|-n name of the bundle to uninstall
Usage: dump [options]
Option Description:
--help|-h help menu
--list|-l app list
--bundlename|-n dump installed hap's info
--metadatakey|-m dump bundleNames match metaData key
Usage: set [options]
Option Description:
--externalmode|-e status enable externalmode
--debugmode|-d status enable debugmode
--signmode|-s status enable signmode
小型系統(tǒng)的bm指令是標準系統(tǒng)的閹割版。
安裝成功后就可以打開該app,部分小型系統(tǒng)的設備屏幕沒有觸摸功能和鼠標驅(qū)動,我們可以使用aa命令來啟動app。
aa start -p com.sample.hello -n MainAbility //包名和ability名都在config.json中定義
# aa
Usage:
aa start -p bundlename -n ability_name
aa stopability -p bundlename -n ability_name
aa terminate -p bundlename
aa dump -p bundlename -n ability_name -e extra_option
aa dump -a
Options:
-h (--help) Show the help information. [eg: aa -h]
-p (--bundlename) Appoint the bundlename name. [eg: -p com.huawei]
-n (--abilityname) Appoint the ability name. [eg: -n MyAbility]
-a (--all) [Unnecessary]dump all ability info. [eg: -a]
-e (--extra) [Unnecessary]extra info when dump. [eg: -e]
Commands:
aa start Start the target ability.
aa stopability Stop the target service ability.
aa terminate Terminate the target app.
aa dump Dump ability
總結(jié)
使用cpp編寫用戶應用程序,我們可以更方便有效的調(diào)用南向接口,這將會在開發(fā)和調(diào)試的過程中給我們帶來極大的便利。
??想了解更多關(guān)于開源的內(nèi)容,請訪問:??
??51CTO 開源基礎軟件社區(qū)??
??https://ost.51cto.com??