Open hongmeng system source, you can see that there is such a folder: third_party. It contains code from a third party.

We can see that there is a lot of third-party code:

If we need to add or migrate any open source code to the system, we can add it to this folder. Next, I’ll show you how to add a package of your own called a_myparty.

  1. Create a new folder a_myparty

  2. Place the package source code in the file

Here I put myparty.c

  1. Create a new build. gn file

The entire code directory is as follows:

  1. The myparty.c file contains the following contents:

Actually, I’m just doing this for demonstration purposes, so there’s nothing in there, right

#include <stdio.h>

void myparty_test(void)

{

printf("first myparty \r\n");
Copy the code

}

  1. The content of the build. gn file is as follows:

The build. gn file mainly describes the information about the software package, including which source files to compile, header file path, and compilation method (Hi3861 only supports static loading at present).

import(“//build/lite/config/component/lite_component.gni”)

import(“//build/lite/ndk/ndk.gni”)

Configure the header file path

config(“a_myparty_config”) {

include_dirs = [

    ".",

]
Copy the code

}

Configure which source code to compile

a_myparty_sources = [

"myparty.c",
Copy the code

]

Here is a static link, similar to a.a file on Linux

lite_library(“a_myparty_static”) {

target_type = "static_library"

sources = a_myparty_sources

public_configs = [ ":a_myparty_config" ]
Copy the code

}

# this is a dynamically loaded.so file similar to Linux

lite_library(“a_myparty_shared”) {

target_type = "shared_library"

sources = a_myparty_sources

public_configs = [ ":a_myparty_config" ]
Copy the code

}

Select static or dynamic

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" ]Copy the code

}

So we’re almost done here.

Finally we need to compile this third package into our firmware.

  1. Turn on the third party package function to enable it to participate in compilation:

Open the file vendor\hisi\hi3861\hi3861\ build.gn

Add “//third_party/a_myparty:a_myparty_static” to the following section

Don’t forget the semicolon…

  1. use

At this point our third package is complete, and we need to use it in our app code

Open applications\sample\wifi-iot\app\my_first_app\BUILD.

Add the “//third_party/a_myparty” header to the build. gn file as follows:

static_library(“my_first_app”) { sources = [ “hello_world.c”

]
include_dirs = [
    "//utils/native/liteos/include",
    "//third_party/a_myparty"
]
Copy the code

}

Open the hello_world.c file as follows:

#include “ohos_init.h”

#include “ohos_types.h”

#include “stdio.h”

// Import the header file

#include “myparty.h”

void HelloWorld(void)

{

printf("%s %d \r\n", __FILE__, __LINE__); printf("[DEMO] Hello world.\n"); Myparty_test () myparty_test();Copy the code

}

SYS_RUN(HelloWorld);

8. After compiling the test, you can see the printed message:

[DEMO] Hello world.

first myparty

The IP address is successfully added.