获取源码
以OpenHarmony5.0.3为例
repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-5.0.3-Release --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
搭建环境
安装必要的工具和命令
apt-get install -y apt-utils binutils bison flex bc build-essential make mtd-utils gcc-arm-linux-gnueabi u-boot-tools python3.9 python3-pip git zip unzip curl wget gcc g++ ruby dosfstools mtools default-jre default-jdk scons python3-distutils perl openssl libssl-dev cpio git-lfs m4 ccache zlib1g-dev tar rsync liblz4-tool genext2fs binutils-dev device-tree-compiler e2fsprogs git-core gnupg gnutls-bin gperf lib32ncurses5-dev libffi-dev zlib* libelf-dev libx11-dev libgl1-mesa-dev lib32z1-dev xsltproc x11proto-core-dev libc6-dev-i386 libxml2-dev lib32z-dev libdwarf-dev apt-get install -y grsync xxd libglib2.0-dev libpixman-1-dev kmod jfsutils reiserfsprogs xfsprogs squashfs-tools pcmciautils quota ppp libtinfo-dev libtinfo5 libncurses5 libncurses5-dev libncursesw5 libstdc++6 gcc-arm-none-eabi vim ssh locales doxygenapt-get install -y libxinerama-dev libxcursor-dev libxrandr-dev libxi-devpip3 install --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple requests setuptools pymongo kconfiglib pycryptodome ecdsa ohos-build pyyaml prompt_toolkit==1.0.14 redis json2html yagmail python-jenkins pip3 install esdk-obs-python --trusted-host pypi.org pip3 install six --upgrade --ignore-installed six
编译
安装编译器及二进制工具
build/prebuilts_download.sh
编译
以DAYU200(RK3568)为例
./build.sh --product-name rk3568 --ccache --no-prebuilt-sdk
烧录系统
以DAYU200(RK3568)为例,其编译生成的镜像文件在下面位置,需要将其拷贝到window系统然后使用RK提供的RKDevTool进行烧录
out/rk3568/packages/phone/images
调试命令
hdc命令
hdc(Harmony Device Connector)是Openharmony标准的设备连接与调试工具,功能类似于 Android 的 adb。它用于开发过程中与设备(或模拟器)进行通信,包括文件传输、应用安装、日志查看、命令执行等
OpenHarmony device connector(HDC) ...---------------------------------global commands:-----------------------------------h/help [verbose] - Print hdc help, 'verbose' for more other cmds-v/version - Print hdc version-t connectkey - Use device with given connect key---------------------------------component commands:-------------------------------
session commands(on server):list targets [-v] - List all devices status, -v for detailstart [-r] - Start server. If with '-r', will be restart serverkill [-r] - Kill server. If with '-r', will be restart serverservice commands(on daemon):target mount - Set /system /vendor partition read-writewait - Wait for the device to become availabletarget boot [-bootloader|-recovery] - Reboot the device or boot into bootloader\recovery.target boot [MODE] - Reboot the into MODE.smode [-r] - Restart daemon with root permissions, '-r' to cancel rootpermissionstmode usb - Reboot the device, listening on USBtmode port [port] - Reboot the device, listening on TCP port---------------------------------task commands:-------------------------------------
file commands:file send [option] local remote - Send file to devicefile recv [option] remote local - Recv file from deviceoption is -a|-s|-z-a: hold target file timestamp-sync: just update newer file-z: compress transfer-m: mode syncforward commands:fport localnode remotenode - Forward local traffic to remote devicerport remotenode localnode - Reserve remote traffic to local hostnode config name format 'schema:content'examples are below:tcp:<port>localfilesystem:<unix domain socket name>localreserved:<unix domain socket name>localabstract:<unix domain socket name>dev:<device name>jdwp:<pid> (remote only)fport ls - Display forward/reverse tasksfport rm taskstr - Remove forward/reverse task by taskstringapp commands:install [-r|-s] src - Send package(s) to device and install themsrc examples: single or multiple packages and directories(.hap .hsp)-r: replace existing application-s: install shared bundle for multi-appsuninstall [-k] [-s] package - Remove application package from device-k: keep the data and cache directories-s: remove shared bundledebug commands:hilog [-h] - Show device log, -h for detailshell [COMMAND...] - Run shell command (interactive shell if no command given)bugreport [FILE] - Return all information from the device, stored in file if FILE is specifiedjpid - List PIDs of processes hosting a JDWP transporttrack-jpid [-a|-p] - Track PIDs of debug processes hosting a JDWP transport-a: include debug and release processes-p: don't display debug and release tagssecurity commands:keygen FILE - Generate public/private key; key stored in FILE and FILE.pub
hdc list targets
列出当前已连接的设备,相当于 adb devices
hdc shell
打开设备终端,进入设备的命令行环境.相当于 adb shell
hdc file send
向设备上传文件,相当于 adb push
hdc file recv
从设备下载文件相当于 adb pull
hdc install
安装 .hap 应用包到设备,相当于adb install
hdc uninstall
卸载应用(根据包名),相当于adb uninstall
hdc stdio
用于将本地标准输入输出重定向到远程设备,适合做调试时查看 log 或与远程进程交互
hdc start
启动 hdc 服务
hdc kill
停止 hdc 服务
hdc -t 192.168.1.100:8710 shell
通过网络链接调试设备,行当于adb connect & adb shell
hilog
hilog 是 OpenHarmony 中用于日志系统的命令行工具,类似于 Android 的 logcat。它可以帮助开发者在调试过程中查看系统运行日志、定位问题,是系统调试的重要工具之一
hilog -x
格式化日志输出,更易读
#过滤日志级别,常用级别debug,info,warn,error,fatal,
hilog -x -L error
#or
hilog -x -L E # Error#过滤特定 tag 标签
hilog -x -T mytag#过滤多个 tag
hilog -x -T tag1 -T tag2#保存日志到文件
hilog -x > /data/logs/hilog.txt#清除日志缓存
hilog -c
其他
hb命令
hb 是OpenHarmony提供的构建工具命令行工具(类似于 Android 的 mm 或 make),用于配置、构建、打包、发布和安装 OpenHarmony 工程
hb set
配置构建环境,包括选择产品和构建目标板
usage: hb set [option]optional arguments:-h, --help show this help message and exit-p PRODUCT_NAME, --product-name PRODUCT_NAMEDefault:''. Help:build a specified product. You could use this option like this: 1.'hb set --product-name rk3568@hihope' 2.'hb set --product-name rk3568' 3.'hb set'[graphical ui]--all [ALL] Default:False. Help:Use a graphical interface to customize most compile-time parameters
hb env
查看当前 hb 环境的配置,包括构建路径、目标、构建类型等
usage: hb env [option]optional arguments:-h, --help show this help message and exit-s SSHKEY, --sshkey SSHKEYDefault:''. Help: set sshkey-l [CLEAN], --clean [CLEAN]Default:false. Help: clean all env args--check [CHECK] Default:True. Help:check that the current running environment contains all dependencies--install [INSTALL] Default:False. Help:install all dependent software for compiling L0, L1 and L2 products-i [INDEP_BUILD], --indep-build [INDEP_BUILD]Default:false. Help: switch to independent build--part PART Default:''. Help:Specify component name, run 'hb env --part {part_name}'--target-cpu {arm,arm64,x86_64,x64,mipsel,riscv64,loongarch64}Default:'arm'. Help:Specifies the desired cpu architecture for the build, each may support different cpu architectures, run 'hb env --target-cpu {cpu_architectures}' to set cpu architectures--target-os {android,ohos,mac,linux,windows}Default:''. Help:Specifies the desired os type for the build, each may support different os type, run 'hb env --target-os {os_type}' to setos type
hb build
执行构建操作,生成可执行镜像文件
usage: hb build [option]optional arguments:-h, --help show this help message and exit--target-cpu {arm,arm64,x86_64,x64,mipsel,riscv64,loongarch64}Default:''. Help:Specifies the desired cpu architecture for the build, each may support different cpu architectures, run 'hb set --all' tolist product all supported cpu architectures--target-os {android,ios}Default:''. Help:Specifies the desired os type for the build, each may support different os type, run 'hb set --all' to list product allsupported os type-p PRODUCT_NAME, --product-name PRODUCT_NAMEDefault:''. Help:Build a specified product. You could use this option like this: 1.'hb build --product-name rk3568@hihope' 2.'hb build--product-name rk3568'--rename-last-log [RENAME_LAST_LOG]Default:True. Help:You can use it to decide whether to keep the last build log--log-mode {normal,silent}Default:'normal'. Help:You can use this option to determine whether to use single-line refresh log mode--precise-branch PRECISE_BRANCHDefault:'dayu200_tdd'. Help:You can use this option to select the dayu200_tdd branch--ccache [CCACHE] Default:True. Help:Enable ccache, this option could improve compilation speed. --stat-ccache can summary the cache data--xcache [XCACHE] Default:False. Help:Enable xcache, this option could improve compilation speed. --stat-ccache can summary the cache data--enable-pycache [ENABLE_PYCACHE]Default:False. Help:Enable pycache, This option can improve the execution speed of python files--jobs JOBS Deprecated, please do not use this option--disable-part-of-post-build [DISABLE_PART_OF_POST_BUILD [DISABLE_PART_OF_POST_BUILD ...]]Deprecated, please do not use this option-T [BUILD_TARGET [BUILD_TARGET ...]], --build-target [BUILD_TARGET [BUILD_TARGET ...]]Default:[]. Help:You use this option to specify a single compilation target, and use 'hb tool --ls' to list all build target--ninja-args [NINJA_ARGS [NINJA_ARGS ...]]Default:[]. Help:You can use it to pass parameters for the ninja phase, but you need to follow the specified command format. eg. --ninja-args=-dkeeprsp-f [FULL_COMPILATION], --full-compilation [FULL_COMPILATION]Default:[]. Help:You can use it to start full code compilation. The default compilation target is images. Use this option to add 'make_all'and 'make_test' to the build process.--strict-mode [STRICT_MODE]Default:False. Help:Check all produce of each phase to early terminates a potentially problematic compilation.--scalable-build [SCALABLE_BUILD]Default:False. Help:Select whether to read information from parts.json generate by preload--build-example [BUILD_EXAMPLE]Default:False. Help:Select whether to read information from subsystem_config_example.json--build-platform-name BUILD_PLATFORM_NAMEDefault:'phone'. Help:Name of the compilation platform. The current optional value is 'phone'--build-xts [BUILD_XTS]Default:False. Help:Select whether to load the components included in the subsystem xts--ignore-api-check [IGNORE_API_CHECK [IGNORE_API_CHECK ...]]Default:[]. Help:Skip the check of some subsystems--load-test-config [LOAD_TEST_CONFIG]Default:True. Help:Select whether to load the test field in bundle.json, that is, whether to call the test case--skip-partlist-check [SKIP_PARTLIST_CHECK]Default:False. Help:Skip the subsystem and component check in partlist file--build-type {release,profile,debug}Default:'release'. Help:Specify compile release or debug version--log-level {info,debug}Default:'INFO'. Help:Specify the log level during compilation. you can select two levels: debug, info. In debug mode, it show all commandlines while building, including cxx, link, solink, etc.--export-para [EXPORT_PARA [EXPORT_PARA ...]]Deprecated, please do not use this option--test [TEST [TEST ...]]Default:[]. Help:You can use it to choose test type. eg. --test xts--gn-args [GN_ARGS [GN_ARGS ...]]Default:[]. Help:Specify gn build arguments, you could use this option like this 'hb build --gn-args is_debug=true'--gn-flags [GN_FLAGS [GN_FLAGS ...]]Default:[]. Help:Specify gn build arguments, you could use this option like this 'hb build --gn-flags "--export-compile-commands"-c COMPILER, --compiler COMPILERDeprecated, please do not use this option--fast-rebuild [FAST_REBUILD]Default:False. Help:You can use it to skip prepare, preloader, gn_gen steps so we can enable it only when there is no change for gn relatedscript--root-perf-main {root,main,root_main}Default:root. Help:different kinds of root packages--runtime-mode {release,debug,profile}Default:release. Help:runtime mode--check-compilation-parameters [CHECK_COMPILATION_PARAMETERS]Default:false. Help:check compilation parameters--keep-ninja-going [KEEP_NINJA_GOING]Default:False. Help:When you need to debug one specific target, you can use this option to keep ninja going to skip some possible error until1000000 jobs fail--build-only-load [BUILD_ONLY_LOAD]Default:False. Help:Stop build until load phase ends--build-only-gn [BUILD_ONLY_GN]Default:False. Help:Stop build until gn phase ends--build-variant {user,root}Default:'root'. Help:specifies device operating mode--device-type DEVICE_TYPEDefault:'default'. Help:specifies device type--disable-package-image [DISABLE_PACKAGE_IMAGE]deprecated, please do not use this option--archive-image [ARCHIVE_IMAGE]Default:False. Help:archive image when build product complete--patch [PATCH] Default:False. Help: Apply patches as per configuration in patch.yml, and handle rollback if needed.--rom-size-statistics [ROM_SIZE_STATISTICS]Default:False. Help:statistics on the actual rom size for each compiled component--stat-ccache [STAT_CCACHE]Default:True. Help:summary ccache hitrate, and generate ccache.log in ${HOME}/.ccache dir--get-warning-list [GET_WARNING_LIST]Default:True. Help:You can use it to collect the build warning and generate WarningList.txt in output dir--generate-ninja-trace [GENERATE_NINJA_TRACE]Default:True. Help:Count the duration of each ninja thread and generate the ninja trace file(build.trace.gz)--compute-overlap-rate [COMPUTE_OVERLAP_RATE]Default:True. Help:Compute overlap rate during the post build--clean-args [CLEAN_ARGS]Default:True. Help:clean all args that generated by this compilation while compilation finished--deps-guard [DEPS_GUARD]Default:True. Help:simplify code, remove concise dependency analysis, and speed up rule checking
hb clean
清除构建产物,释放空间,类似于 make clean
usage: hb clean [option]optional arguments:-h, --help show this help message and exit--clean-all [CLEAN_ALL]Default:False. Help:clean all producer--clean-args [CLEAN_ARGS]Default:True. Help:clean all args that generated by last compilation--clean-out-product [CLEAN_OUT_PRODUCT]Default:False. Help:clean out/{product} directory that generated by last compilation--clean-ccache [CLEAN_CCACHE]Default:False. Help:clean .ccache directory which is in OHOS root . WARNING, clean ccache may cause something
hb 其他辅助命令
#管理或调用某些构建工具,比如 GN/Ninja、代码检查等
hb tool#独立构建指定模块(不需要全量编译)
hb indep_build#安装构建好的文件到目标设备(例如通过 hdc 工具),默认会推送 out 目录下的产物(如 *.hsp)到设备上安装运行
hb install#打包构建产物,生成系统镜像(如 system.img, userdata.img 等)
hb package#发布构建产物(通常到本地发布目录或镜像服务器等)
hb publish#更新项目依赖,例如拉取 third_party 代码,或更新子模块
hb update#向远程设备推送编译产物,等价于 hdc file send
hb push