如何往鴻蒙系統(tǒng)源碼中添加第三方軟件包
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
打 開鴻蒙系統(tǒng)的源碼,可以看到有這么一個(gè)文件夾:third_party。里面存放的是第三方的代碼。

點(diǎn)開我們可以看到有很多第三方代碼:

后續(xù)我們?nèi)绻枰到y(tǒng)中添加、移植任何開源代碼,都可以添加到這個(gè)文件夾中。接下來(lái),教大家如何添加一個(gè)自己的軟件包,名字為a_myparty。
1. 新建一個(gè)文件夾a_myparty
2. 往文件中放置軟件包源碼
這里我放在的是 myparty.c文件
3. 新建BUILD.gn文件
整個(gè)代碼目錄如下:

4. myparty.c文件內(nèi)容如下:
其實(shí),我這個(gè)只是為了演示的,所以里面代碼沒(méi)什么作用
- #include <stdio.h>
- void myparty_test(void)
- {
- printf("first myparty \r\n");
- }
5. BUILD.gn文件內(nèi)容如下:
BUILD.gn文件主要是描述了軟件包的相關(guān)信息,包括編譯哪些源文件,頭文件路徑、編譯方式(目前Hi3861 只支持靜態(tài)加載)
- import("//build/lite/config/component/lite_component.gni")
- import("//build/lite/ndk/ndk.gni")
- #這里是配置頭文件路徑
- config("a_myparty_config") {
- include_dirs = [
- ".",
- ]
- }
- #這里是配置要編譯哪些源碼
- a_myparty_sources = [
- "myparty.c",
- ]
- #這里是靜態(tài)鏈接,類似于Linux系統(tǒng)的 .a文件
- lite_library("a_myparty_static") {
- target_type = "static_library"
- sources = a_myparty_sources
- public_configs = [ ":a_myparty_config" ]
- }
- #這里是動(dòng)態(tài)加載,類似于Linux系統(tǒng)的 .so文件
- lite_library("a_myparty_shared") {
- target_type = "shared_library"
- sources = a_myparty_sources
- public_configs = [ ":a_myparty_config" ]
- }
- #這里是入口,選擇是靜態(tài)還是動(dòng)態(tài)
- ndk_lib("a_myparty_ndk") {
- if (board_name != "hi3861v100") {
- lib_extension = ".so"
- deps = [
- ":a_myparty_shared"
- ]
- } else {
- deps = [
- ":a_myparty_static"
- ]
- }
- head_files = [
- "//third_party/a_myparty"
- ]
- }
到了這里我們基本上就寫完了。
最后我們要讓這個(gè)第3放軟件包編譯到我們固件中。
6. 打開第3方軟件包功能,使其參與編譯:
打開vendor\hisi\hi3861\hi3861\BUILD.gn 文件
在下圖部分添加 "//third_party/a_myparty:a_myparty_static"
別忘了分號(hào)。。。

7. 使用
到了這里我們的第3方軟件包就添加完成了,接下來(lái)我們要在app 代碼中使用它
打開 applications\sample\wifi-iot\app\my_first_app\BUILD.gn 文件,沒(méi)有的同學(xué)請(qǐng)自己先完成hello world入門例程先。

添加 "//third_party/a_myparty" 頭文件路徑,BUILD.gn文件內(nèi)容如下:
- static_library("my_first_app") {
- sources = [
- "hello_world.c"
- ]
- include_dirs = [
- "//utils/native/liteos/include",
- "//third_party/a_myparty"
- ]
- }
打開hello_world.c文件,內(nèi)容如下:
- #include "ohos_init.h"
- #include "ohos_types.h"
- #include "stdio.h"
- //導(dǎo)入頭文件
- #include "myparty.h"
- void HelloWorld(void)
- {
- printf("%s %d \r\n", __FILE__, __LINE__);
- printf("[DEMO] Hello world.\n");
- //調(diào)用第3方軟件包 的函數(shù) myparty_test()
- myparty_test();
- }
- SYS_RUN(HelloWorld);
8.最后編譯測(cè)試即可看到打印信息:
- [DEMO] Hello world.
- first myparty
說(shuō)明添加成功。
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz