# Usage Guide for Codegen ## Introduction `CodeGen` is a tool within React Native designed for simplifying and automating the process of code generation. It aids developers in generating bridge code, making it easier for React Native applications to interact with native code. Support for codegen has been implemented in Harmony OS RN, and most third-party libraries (Arkts) have also incorporated support. Therefore, before utilizing these third-party libraries, it's necessary to generate the bridging code via codegen; otherwise, an error like the following may occur: ``` cannot find module '@rnoh/react-native-openharmony/generated/ts' or its corresponding type declarations. ``` ## Usage ### Run the codegen script specified in `MyProject/package.json` of the RN project > [!TIP] Ensure that the third-party libraries are correctly installed before running! Configure the codegen script: ```json ... "scripts": { ... "codegen": "react-native codegen-harmony --cpp-output-path ./harmony/entry/src/main/cpp/generated --rnoh-module-path ./harmony/entry/oh_modules/@rnoh/react-native-openharmony" }, ... ``` Parameters for codegen-harmony: - `rnoh-module-path`: The relative path within the native project to the RNOH SDK module (if installed via a har package, it must point to the installed location, e.g., xxx/oh_modules/@rnoh/react-native-openharmony) - `cpp-output-path`: Specifies the relative path of the directory where generated C++ files will be stored; defaults to ./harmony/entry/src/main/cpp/generated - `project-root-path`: The relative path to the root directory of the native project Run codegen ```bash npm run codegen ``` After execution, C++ code will be generated in the path specified by the `cpp-output-path` parameter (if not specified, it will generate at the default path), and ArkTS/TS code will be generated in the `rnoh-module-path`(must be generated under the RN SDK). **Precautions** The code generated by the script must be included in the project; otherwise, an error like the following may occur: ``` [ERROR] Tried to remove files in path/generated and that path is outside current working directory ``` Therefore, it is recommended to place the native project inside the `RN` project. ``` . ├── ReactNativeProject // RN project │   ├── android │   ├── ios │   ├── harmony // HarmonyOS native project ``` ### Configure CMakeLists and introduce RNOHGeneratedPackage (C++) Open `entry/src/main/cpp/CMakeLists.txt` and add: ```diff ... # RNOH_BEGIN: add_package_subdirectories add_subdirectory("${OH_MODULE_DIR}/rnoh-sample-package/src/main/cpp" ./sample-package) # RNOH_END: add_package_subdirectories + file(GLOB GENERATED_CPP_FILES "./generated/*.cpp") add_library(rnoh_app SHARED + ${GENERATED_CPP_FILES} "./PackageProvider.cpp" "${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp" ) target_link_libraries(rnoh_app PUBLIC rnoh) # RNOH_BEGIN: link_packages target_link_libraries(rnoh_app PUBLIC rnoh_sample_package) # RNOH_END: link_packages ... ``` open `entry/src/main/cpp/PackageProvider.cpp` and add: ```diff #include "RNOH/PackageProvider.h" + #include "generated/RNOHGeneratedPackage.h" using namespace rnoh; std::vector> PackageProvider::getPackages(Package::Context ctx) { return { + std::make_shared(ctx), }; } ```