獲取源碼
以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