OpenHarmony Tripartite library porting

Getting your Trinity Audio player ready...

Tutorial on tripartite library porting process for OpenHarmony systems.

1. Download the native library

When downloading the native library, do not download the master branch, but download the latest version branch.

2. Generate a Makefile

Downloading different native third-party libraries may provide different compilation methods, according to the README file of the native third-party library, you can know how to compile, the following two compilation methods:

Living In Harmony is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.


1.cmake build
In the root directory of the downloaded method library, CMakeLists exists.txt file
#Create a new build directory
mkdir mybuild && cd mybuild
Through cmake.. Build, at this time, the Makefile file will be generated according to the default configuration options
Through cmake-gui.. Build, the build optional pop-up box will pop up at this time, and the Makefile file will be generated with optional configuration build
Generally, is used to build, and the and files exist in the native library.
A Makefile will be generated at this time

3. Analyze the dependence of the library on external libraries

Analyze the log information in the process of generating the Makefile and find the dependencies of this library on other libraries. During the log analysis process, the following situations generally exist:
1. The library name cannot be found in the log. When some third-party libraries generate Makefile files, if the corresponding library can be found in the system, it will not be printed in the log. This kind of situation will be transplanted to OpenHarmony. The compilation process will be reflected in errors. Typically, the header file path cannot be found or the function symbol is missing when linking;
2. Found or not found libraries can be found in the log. By default, these libraries do not exist on OpenHarmony, so these libraries need to be analyzed.
1. os-developed libraries, which can be found in the third_party directory of OpenHarmony;
2. Undeveloped libraries and libraries that are in the plan can be transplanted first, reported and the workload re-evaluated;
3. Undeveloped libraries and libraries that are not planned. Analyze these libraries. Generally, these libraries are divided into two categories. ① There is an impact on the library function, ② There is no impact on the library function. In this part, you need to find the library-related Local analysis code, if it does not affect the function, you can consider ignoring it, otherwise report it and re-evaluate the workload problem;
Each dependent third-party library generally has a corresponding macro definition to control the compilation of the code in the library. During the process of transplanting the third-party library, if there are many library dependencies, you need to pay attention to the switches of these macro definitions.

You can refer to minizip library dependency analysis


By analyzing the Makefile,
1. Find the source code required to generate the library, header file path, cflags/cflags_cc, macro definitions, and dependent libraries
2. Find the source code required to generate test/demo, header file path, cflags/cflags_cc, macro definitions, and dependent libraries


The results obtained from the analysis are compiled into the OHOS system, and refer to the compilation and construction guide on the official website.

Explain with zxing-cpp-1.2.0, download zxing-cpp-1.2.0 from github and put it in the third_party of OpenHarmony-3.2-Beta1. Warehouse address.

Modify the following file to add zxing-cpp-1.2.0 to the RK3568 board:


Add code:

      "subsystem": "zxing-cpp-1.2.0",
      "components": [
          "component": "zxing-cpp-1.2.0",
          "features": []

Modify the following file to add zxing-cpp-1.2.0 to the subsystem:


Add code:

 "zxing-cpp-1.2.0": {
    "path": "third_party/zxing-cpp-1.2.0",
    "name": "zxing-cpp-1.2.0"

Create zxing-cpp-1.2.0 to create the compilation component


Add code:

  "subsystem": "zxing-cpp-1.2.0",
  "parts": {
    "zxing-cpp-1.2.0": {
      "module_list": [
      "inner_kits": [
      "system_kits": [
      "test_list": [

Create a GNN file of zxing-cpp-1.2.0 test:


Add the code as follows:

#Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.


    cflags_cc = [

config("libnl_config") {
    include_dirs = [

ohos_executable("ZXingReader") {
    sources = [
    public_configs = [ ":libnl_config" ]
    deps = [
    configs = [ ":public" ]
    part_name = "zxing-cpp-1.2.0"

ohos_executable("ZXingWriter") {
    sources = [
    public_configs = [ ":libnl_config" ]
    deps = [
    configs = [ ":public" ]
    part_name = "zxing-cpp-1.2.0"

Create a dynamic library GNN file for zxing-cpp-1.2.0:


Add the code as follows:

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation version 2.1
# of the License.
# Copyright(c) 2021-2022 Huawei Device Co., Ltd.

if (!defined(ohos_lite)) {
    cflags_cc = [

if (!defined(ohos_lite)) {
  config("libnl_config") {
    include_dirs = [

ohos_shared_library("zxing_share") {
    public_configs = [ ":libnl_config" ]
    sources = [
    defines = [

    configs = [ ":public" ]
    part_name = "zxing-cpp-1.2.0"

6. Test on the board

Enter HB SET, select RK3568, and then enter HB Build to compile.

For the programming program, refer to RK3568 programming

After the compilation is completed on the PC side, two test programs will be generated: ZXingReader and ZXingWriter, and you can run the program directly to view the functions:


Writing a shell script through the program’s functions is actually running all the functions supported by the program (file zxing-cpp-1.2.0/test/

Verification on the PC side

Run the following without error:


OpenHarmony3.2_Beta verify the following in the RK3568 board:


7. API interface export

Run this API interface export script on the PC side, and use the dynamic library SO to run the export script.

1. Create allHeads.h, which contains the header files exported from all libraries. zxing-cpp has two test programs, ZXingReader and ZXingWriter, and the header files of the used are as follows:


2. Add the allDySos directory, which places all generated dynamic libraries:


3. Add the allTests directory, which places all the generated test files:


4. Create a new automated test script, the script code is as follows, and when executing, you need to pass the parameters of the compilation header file to the script, such as ./ -I The path where the header file is located -D macro definition (the parameters in cflags/cflags_cc when compiling all dynamic libraries).

where cxx=”1″ indicates that the export is based on the cpp file:


mixed_api() {
    while read line
        if [ $cxx != "1" -o "$4" == "1" ];then
            tmp=`echo $line | sed "s/(.*$//g" | sed "s/^.*:://g"`
        res=`cat $2 |grep "$tmp" -F`
        if [ "$res" != "" ];then
            echo "$line" >> $3
    done < $1

remove_standard_api() {
    while read line
        tmp1=`echo $line |grep ".*#.*\/usr\/"`
        tmp2=`echo $line |grep ".*#.*[0-9].*\".*\""`
        if [ "$tmp1" != "" ];then
        elif [ "$tmp2" != "" ];then
        elif [ $flag == "1" ];then
            echo "$line" >> $2
    done < $1


if [ $cxx == "1" ];then

if [ ! -e $src1 ];then
    echo "$src1 is not exist!"
    exit 1

if [ ! -e $src2 ];then
    echo "$src2 is not exist!"
    exit 1

if [ ! -e $src3 ];then
    echo "$src3 is not exist!"
    exit 1

rm -rf $src1.i* $src2.txt $src3.txt export_api.txt tested_api.txt

$CC -E $* $src1 -o $src1.i

if [ "$?" != "0" ];then
    exit 1

remove_standard_api $src1.i $src1.i.tmp

nm $src2/* $flags_D | sort -u | grep " T " | sed "s/^.* T //g" >> $src2.txt
nm $src3/* $flags_DU | sort -u | sed "s/^.* U //g" >> $src3.txt

mixed_api $src2.txt $src1.i.tmp export_api.txt 0
mixed_api export_api.txt $src3.txt  tested_api.txt 1

rm -rf $src1.i* $src2.txt $src3.txt

In the export zxing-cpp field, enter the following command:

./ -I../core/src/ -I../thirdparty/stb/ -DZXing_EXPORTS

Generate export_api.txt (export SO external API interface) and testd_api.txt (export SO external export API interface used by export test program).


8. Go to the warehouse

At present, the sig repository is not ready, after the migration is completed, temporarily upload the personal repository, the uploaded content includes the native library code (except for the part that needs to modify the native library code, nothing else can be uploaded, such as the intermediate file generated during compilation),, README.Opensource, all API interface export_api.txt exported from the library, and the tested API interface tested_api.txt

README. Opensource, the format is as follows:

        "Name"            : "zxing-cpp",
        "License"         : "Apache License"
        "License File"    : "LICENSE",
        "Version Number"  : "1.2.0",
        "Owner"           : "",
        "Upstream URL"    : "",
        "Description"     : "ZXing-C++ ("zebra crossing") is an open-source, multi-format 1D/2D barcode image processing library implemented in C++"