1# JNI
2As a general rule, jarjar every static library dependency used in a mainline module into the
3modules's namespace (especially if it is also used by other modules)
4
5Fully-qualified name of java class needs to be hard-coded into the JNI .so, because JNI_OnLoad
6does not take any parameters. This means that there needs to be a different .so target for each
7post-jarjared package, so for each module.
8
9This is the guideline to provide JNI library shared with modules:
10
11* provide a common java library in frameworks/libs/net with the Java class (e.g. BpfMap.java).
12
13* provide a common native library in frameworks/libs/net with the JNI and provide the native
14  register function with class_name parameter. See register_com_android_net_module_util_BpfMap
15  function in frameworks/libs/net/common/native/bpfmapjni/com_android_net_module_util_BpfMap.cpp
16  as an example.
17
18When you want to use JNI library from frameworks/lib/net:
19
20* Each module includes the java library (e.g. net-utils-device-common-bpf) and applies its jarjar
21  rules after build.
22
23* Each module creates a native library in their directory, which statically links against the
24  common native library (e.g. libnet_utils_device_common_bpf), and calls the native registered
25  function by hardcoding the post-jarjar class_name. Linkage *MUST* be static because common
26  functions in the file (e.g., `register_com_android_net_module_util_BpfMap`) will appear in the
27  library (`.so`) file, and different versions of the library loaded in the same process by
28  different modules will in general have different versions. It's important that each of these
29  libraries loads the common function from its own library. Static linkage should guarantee this
30  because static linkage resolves symbols at build time, not runtime.