• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Customizing Ark Bytecode During Compilation
2
3You can modify Ark bytecode files using the customization capabilities provided by the ArkTS compilation toolchain.
4
5## Configuration
6
7Develop a dynamic library file to manipulate ARK bytecode files. In the **build-profile.json5** file of the project, add the [transformLib option](arkoptions-guide.md) and set its value to the path of the dynamic library. The compiler loads the dynamic library at the specified time and executes the **Transform** method in the library.
8
9## Execution
10
11If **transformLib** is configured and the dynamic library file is successfully loaded, the compiler generates an ARK bytecode file to the default location. It then calls the **Transform** method in the dynamic library, passing the path of the ARK bytecode file as a parameter. The **Transform** method, which includes developer-defined modification logic, is used to regenerate the Ark bytecode file. The operation of updating the bytecode file to disk is executed by the user.
12
13Below is a template for a dynamic library, which you can use to implement the specific logic for the **Transform** method as required.
14
15## How to Develop
16
171. Create the source code for the dynamic library.
18
19   example.cpp:
20
21   ```c++
22   /**
23    * @brief Entry method for modifying the ARK bytecode file.
24    * @param abc_path Path for storing the ARK bytecode file to be processed.
25    */
26   extern "C" int Transform(const char *abc_path)
27   {
28       // You can read the ARK bytecode file corresponding to abc_path, modify related data based on the ARK bytecode format, and generate a new ARK bytecode file.
29       return 0;
30   }
31   ```
32
332. Use a C language compiler (g++) to compile the dynamic library.
34
35   Windows:
36
37   ```
38   g++ --shared -o example.dll example.cpp
39   ```
40
41   Linux:
42
43   ```
44   g++ --shared -o example.so example.cpp
45   ```
46
47   macOS:
48
49   ```
50   g++ --shared -o example.so example.cpp
51   ```
52
533. In DevEco Studio, configure the **transformLib** option in the **build-profile.json5** file. (The following uses the Windows environment as an example.)
54
55   Set the option to the path of the link library file generated in step 2. In this example, the path is in the **dll** directory.
56
57   ![image_0000002079773605](figures/image_0000002079773605.png)
58
594. Recompile the project to customize the Ark bytecode.
60