【Openharmony】【4.0R】hello程序之“hap_pack.gni”模板使用方法

【Openharmony】【4.0R】hello程序之“hap_pack.gni”模板使用方法

"hap_pack.gni"模板,用来制作鸿蒙系统应用的模板,这个模板使用起来不是很方便,因为是针对应用类型的程序,不过网上教程也是很多。
很遗憾的是我的参考教程不能复现出一个完整HAP应用的编译安装及使用,因此本文只记录模板简单使用方式。如有错误请各位大佬指正~

原文参考连接:参考代码
OH版本:4.0Release
内核版本:LiteOS-A
产品版本:qemu_small_system_demo、hispark_taurus

“hap_pack.gni”模板

模板内东西并不多,有兴趣可以自己看一下,模板位置目录"//build/lite/config/hap_pack.gni"

文件结构

testhello/
├── hello_hap
│   ├── BUILD.gn
│   ├── camera_AppProvision_Release.p7b
│   ├── config.json
│   ├── include
│   │   ├── hello_ability.h
│   │   └── hello_ability_slice.h
│   ├── resources
│   └── src
│       ├── hello_ability.cpp
│       └── hello_ability_slice.cpp

头文件

//hello_ability.h
#ifndef HELLO_ABILITY_H
#define HELLO_ABILITY_H

#include "ability_loader.h"

namespace OHOS
{
    class HelloAbility : public Ability
    {
    protected:
        void OnStart(const Want &want) override; // Want结构体,ability的相关信息
        /*
         * 由于在这里我们只要简单的展示helloworld标签,其它函数不需要重载。
         */
        // void OnInactive() override;
        // void OnActive(const Want &want) override;
        // void OnBackground() override;
        // void OnStop() override;
    };
}

#endif
//hello_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 HelloAbilitySlice : public AbilitySlice
    { // 创建AbilitySlice类 与上面同名
    public:
        HelloAbilitySlice() = default;
        virtual ~HelloAbilitySlice();

    protected:
        void OnStart(const Want &want) override;
        /*
         * 同理
         */
        // void OnInactive() override;
        // void OnActive(const Want &want) override;
        // void OnBackground() override;
        // void OnStop() override;
    };
}

动态库源文件

//hello_ability.cpp
#include "hello_ability.h"
namespace OHOS
{
    REGISTER_AA(HelloAbility) // 使用REGISTER_AA注册ability

    void HelloAbility::OnStart(const Want &want)
    {
        printf("This is HelloAbility OnStart status!\r\n");
        SetMainRoute("HelloAbilitySlice"); // 设置主页面为HelloAbilitySlice,这要与后续的slice名字匹配

        Ability::OnStart(want);
    }
}
//hello_ability_slice.cpp
#include "hello_ability_slice.h"
const int screen_width = 720;
const int screen_height = 1280;

namespace OHOS
{
    REGISTER_AS(HelloAbilitySlice)

    HelloAbilitySlice::~HelloAbilitySlice()
    {
        printf("This is ~HelloAbilitySlice!\r\n");
    }

    void HelloAbilitySlice::OnStart(const Want &want)
    {
        AbilitySlice::OnStart(want);
        RootView *rootView_ = RootView::GetWindowRootView(); // 创建底层界面
        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(); // 创建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
    }
}

BUILD.gn配置文件

import("//build/lite/config/hap_pack.gni")

shared_library("hello_happack") {
  sources = [
    "src/hello_ability.cpp",
    "src/hello_ability_slice.cpp"
  ]

  deps = [
    "${aafwk_lite_path}/frameworks/ability_lite:aafwk_abilitykit_lite",
    "${appexecfwk_lite_path}/frameworks/bundle_lite:bundle",
    "//foundation/arkui/ui_lite:ui_lite",
    "//foundation/distributeddatamgr/kv_store/interfaces/inner_api/kv_store:kv_store",
    "//foundation/graphic/surface_lite",
    "//foundation/graphic/graphic_utils_lite:utils_lite",
    "//foundation/systemabilitymgr/samgr_lite/samgr:samgr",
  ]

  include_dirs = [
    "include",
    "//foundation/arkui/ui_lite/interfaces/kits",
    "//foundation/arkui/ui_lite/interfaces/innerkits",
    "${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") {
  deps = [ ":hello_happack" ]
  mode = "hap"
  json_path = "config.json"
  ability_so_path = "$root_out_dir/libhello_happack.so"
  force = "true"
  cert_profile = "camera_AppProvision_Release.p7b"
  resources_path = "resources"
  hap_name = "hello"
  privatekey = "LYXQG HELLO HAP PACK Release"
}

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": ".HelloAbilityPackage",
        "deviceType": [
            "phone",
            "tv",
            "tablet",
            "pc",
            "car",
            "smartWatch",
            "sportsWatch",
            "smartVision"
        ],
        "distro": {
            "deliveryWithInstall": true,
            "moduleName": "hello",
            "moduleType": "entry"
        },
        "abilities": [ 
            {
                "name": "HelloAbility",
                "label": "hello",
                "launchType": "standard",
                "type": "page",
                "visible": true
            }
        ]
    }
}

添加编译选项

以hispark_taurus为例,修改"vendor/hisilicon/hispark_taurus/BUILD.gn"

group("hispark_taurus") {
  deps = [
    "hals/utils/sys_param:vendor.para",
    "init_configs:init_configs",
    "init_configs:init_configs_mksh",
    "//testhello/hello_hap:hello_hap",
  ]
}

编译后在相关产品输出路径下的“system/internal”中会出现“hello.hap”文件。

注意事项

bm aa 工具

1、如果是hispark_taurus,这些工具已经被编译,在dev_tools目录下,但是并没有在系统镜像中加入。
请参考 fs.yml配置 添加镜像目录,位置在rootfs下面就行

  fs_dir_name: rootfs
  fs_dirs:
    -
      source_dir: dev_tools
      target_dir: dev_tools
      is_strip: TRUE
      dir_mode: 555
      file_mode: 555

2、如果是qemu_small_system_demo,同样要添加镜像目录。此外还需要编译bm,aa工具,因为qemu_small_system_demo默认不编译。
在“vendor/ohemu/qemu_small_system_demo”目录下修改config.json,添加两个要编译的子系统及组件
(感觉aa是application ability简称,bm是bundlemanager简称)

      {
        "subsystem": "ability",
        "components": [
          { "component": "ability_lite", "features":[ "enable_ohos_appexecfwk_feature_ability = true" ] },
          { "component": "dmsfwk_lite", "features":[] }
        ]
      },
      {
        "subsystem": "bundlemanager",
        "components": [
          { "component": "bundle_framework_lite", "features":[] }
        ]
      },

camera_AppProvision_Release.p7b文件

cert_profile = "camera_AppProvision_Release.p7b"这里的p7b文件,目前如何产生我也不是很明白,是从代码中其它项目中随便copy过来的一个文件。正式做应用的话需要使用签名工具签名:HAP包签名工具编译方法,本文就不尝试了。

最后

实际上虽然编译出bm、aa、 hello.hap包,但是在qemu_small_system_demo、hispark_taurus中无法使用,qemu还能理解,毕竟qemu原本aa,bm工具都没有的。但是Hi3516DV300上也出错,不清楚是不是代码原因,还是3516问题。

bm set -s disable

在这里插入图片描述

bm install -p /system/intenval/hello.hap

在这里插入图片描述

aa start -p com.sample.hello -n HelloAbility

在这里插入图片描述

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值