1//
2// Copyright (C) 2018 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17// Common defaults to define SDK level
18package {
19    default_team: "trendy_team_fwk_core_networking",
20    default_applicable_licenses: ["Android-Apache-2.0"],
21}
22
23// TODO: remove this default and replace with ConnectivityNextEnableDefaults. This will need to be
24// done separately in each branch due to merge conflicts.
25// Defaults to enable/disable java targets that depend on
26// NetworkStackNext, which uses development APIs. "enabled" may
27// have a different value depending on the branch.
28java_defaults {
29    name: "NetworkStackNextEnableDefaults",
30    enabled: true,
31}
32
33// Common defaults for NetworkStack integration tests, root tests and coverage tests
34// to keep tests always running against the same target sdk version with NetworkStack.
35java_defaults {
36    name: "NetworkStackReleaseTargetSdk",
37    min_sdk_version: "30",
38    target_sdk_version: "35",
39}
40
41java_defaults {
42    name: "NetworkStackReleaseApiLevel",
43    defaults: ["NetworkStackReleaseTargetSdk"],
44    sdk_version: "module_current",
45    libs: [
46        "framework-configinfrastructure.stubs.module_lib",
47        "framework-connectivity.stubs.module_lib",
48        "framework-connectivity-t.stubs.module_lib",
49        "framework-statsd.stubs.module_lib",
50        "framework-tethering.stubs.module_lib",
51        "framework-wifi.stubs.module_lib",
52    ],
53}
54
55// Libraries for the API shims
56java_defaults {
57    name: "NetworkStackShimsDefaults",
58    libs: [
59        "androidx.annotation_annotation",
60        "networkstack-aidl-latest",
61    ],
62    static_libs: [
63        "modules-utils-build_system",
64    ],
65    apex_available: [
66        "com.android.tethering",
67        "//apex_available:platform", // For InProcessNetworkStack
68    ],
69    min_sdk_version: "30",
70}
71
72// Common shim code. This includes the shim interface definitions themselves, and things like
73// ShimUtils and UnsupportedApiLevelException. Compiles against system_current because ShimUtils
74// needs access to all Build.VERSION_CODES.*, which by definition are only in the newest SDK.
75// TODO: consider moving ShimUtils into a library (or removing it in favour of SdkLevel) and compile
76// this target against the lowest-supported SDK (currently 30).
77java_library {
78    name: "NetworkStackShimsCommon",
79    defaults: ["NetworkStackShimsDefaults"],
80    srcs: ["apishim/common/**/*.java"],
81    sdk_version: "system_current",
82    visibility: ["//visibility:private"],
83}
84
85// Each level of the shims (29, 30, ...) is its own java_library compiled against the corresponding
86// system_X SDK. this ensures that each shim can only use SDK classes that exist in its SDK level.
87java_library {
88    name: "NetworkStackApi29Shims",
89    defaults: ["NetworkStackShimsDefaults"],
90    srcs: ["apishim/29/**/*.java"],
91    libs: [
92        "NetworkStackShimsCommon",
93    ],
94    sdk_version: "system_29",
95    visibility: ["//visibility:private"],
96}
97
98java_library {
99    name: "NetworkStackApi30Shims",
100    defaults: ["NetworkStackShimsDefaults"],
101    srcs: [
102        "apishim/30/**/*.java",
103    ],
104    libs: [
105        "NetworkStackShimsCommon",
106        "NetworkStackApi29Shims",
107    ],
108    sdk_version: "system_30",
109    visibility: ["//visibility:private"],
110    lint: {
111        strict_updatability_linting: true,
112
113    },
114}
115
116java_library {
117    name: "NetworkStackApi31Shims",
118    defaults: ["NetworkStackShimsDefaults"],
119    srcs: [
120        "apishim/31/**/*.java",
121    ],
122    static_libs: [
123        // Libs building against module API can only be linked against when building against
124        // module API (so NetworkStackApi30Shims or NetworkStackApi29Shims can't use it, since they
125        // use system_30 and system_29 respectively).
126        "net-utils-framework-common",
127    ],
128    libs: [
129        "NetworkStackShimsCommon",
130        "NetworkStackApi29Shims",
131        "NetworkStackApi30Shims",
132        "framework-connectivity.impl",
133        "sdk_module-lib_31_framework-wifi",
134    ],
135    sdk_version: "module_31",
136    visibility: ["//visibility:private"],
137}
138
139java_library {
140    name: "NetworkStackApi33Shims",
141    defaults: ["NetworkStackShimsDefaults"],
142    srcs: [
143        "apishim/33/**/*.java",
144    ],
145    libs: [
146        "NetworkStackShimsCommon",
147        "NetworkStackApi29Shims",
148        "NetworkStackApi30Shims",
149        "NetworkStackApi31Shims",
150        "sdk_module-lib_33_framework-bluetooth",
151        "framework-connectivity.impl",
152        "framework-connectivity-t.stubs.module_lib",
153        "framework-tethering.impl",
154        "sdk_module-lib_33_framework-wifi",
155    ],
156    sdk_version: "module_33",
157    visibility: ["//visibility:private"],
158}
159
160java_library {
161    name: "NetworkStackApi34Shims",
162    defaults: ["NetworkStackShimsDefaults"],
163    srcs: [
164        "apishim/34/**/*.java",
165    ],
166    libs: [
167        "NetworkStackShimsCommon",
168        "NetworkStackApi29Shims",
169        "NetworkStackApi30Shims",
170        "NetworkStackApi31Shims",
171        "NetworkStackApi33Shims",
172        "sdk_module-lib_34_framework-bluetooth",
173        "framework-connectivity.impl",
174        "framework-connectivity-t.stubs.module_lib",
175        "framework-tethering.impl",
176        "sdk_module-lib_34_framework-wifi",
177    ],
178    sdk_version: "module_34",
179    visibility: ["//visibility:private"],
180}
181
182// Shims for APIs being added to the current development version of Android. These APIs are not
183// stable and have no defined version number. These could be called 10000, but they use the next
184// integer so if the next SDK release happens to use that integer, we don't need to rename them.
185// Jarjar rules are generated based on the stable shims, which do not contain this library. As a
186// result, no static_lib that needs jarjar should be used here. In general, static_libs should not
187// be used in this library at all; instead they can be in one of the earlier, shim libraries which
188// are part of the stable shims and scanned when generating jarjar rules.
189java_library {
190    name: "NetworkStackApi35Shims",
191    defaults: [
192        "NetworkStackShimsDefaults",
193        "ConnectivityNextEnableDefaults",
194    ],
195    srcs: [
196        "apishim/35/**/*.java",
197    ],
198    libs: [
199        "NetworkStackShimsCommon",
200        "NetworkStackApi29Shims",
201        "NetworkStackApi30Shims",
202        "NetworkStackApi31Shims",
203        "NetworkStackApi33Shims",
204        "NetworkStackApi34Shims",
205        "framework-bluetooth.stubs.module_lib",
206        "framework-connectivity.impl",
207        "framework-connectivity-t.stubs.module_lib",
208        "framework-tethering.impl",
209        "framework-wifi.stubs.module_lib",
210        "android.net.ipsec.ike.stubs.module_lib",
211    ],
212    sdk_version: "module_current",
213    visibility: ["//visibility:private"],
214}
215
216// API current uses the API current shims directly.
217// The current (in-progress) shims are in the com.android.networkstack.apishim package and are
218// called directly by the networkstack code.
219java_library {
220    name: "NetworkStackApiCurrentShims",
221    defaults: [
222        "NetworkStackShimsDefaults",
223        "NetworkStackReleaseApiLevel",
224        "ConnectivityNextEnableDefaults",
225    ],
226    static_libs: [
227        "NetworkStackShimsCommon",
228        "NetworkStackApi29Shims",
229        "NetworkStackApi30Shims",
230        "NetworkStackApi31Shims",
231        "NetworkStackApi33Shims",
232        "NetworkStackApi34Shims",
233        "NetworkStackApi35Shims",
234    ],
235    visibility: [
236        "//packages/modules/Connectivity/Tethering",
237        "//packages/modules/Connectivity/service",
238        "//packages/modules/Connectivity/service-t",
239        "//packages/modules/Connectivity/tests:__subpackages__",
240    ],
241}
242
243// API stable uses jarjar to rename the latest stable apishim package from
244// com.android.networkstack.apishim.apiXX to com.android.networkstack.apishim, which is called by
245// the networkstack code.
246java_library {
247    name: "NetworkStackApiStableShims",
248    defaults: [
249        "NetworkStackShimsDefaults",
250        "NetworkStackReleaseApiLevel",
251    ],
252    static_libs: [
253        "NetworkStackShimsCommon",
254        "NetworkStackApi29Shims",
255        "NetworkStackApi30Shims",
256        "NetworkStackApi31Shims",
257        "NetworkStackApi33Shims",
258        "NetworkStackApi34Shims",
259    ],
260    jarjar_rules: "apishim/jarjar-rules-compat.txt",
261    visibility: [
262        "//packages/modules/Connectivity/Tethering",
263        "//packages/modules/Connectivity/service",
264        "//packages/modules/Connectivity/service-t",
265        "//packages/modules/Connectivity/tests:__subpackages__",
266    ],
267}
268
269// Common defaults for android libraries containing network stack code, used to compile variants of
270// the network stack in the system process and in the network_stack process
271java_defaults {
272    name: "NetworkStackAndroidLibraryDefaults",
273    srcs: [
274        ":framework-networkstack-shared-srcs",
275    ],
276    libs: [
277        "error_prone_annotations",
278        "framework-annotations-lib",
279        "unsupportedappusage",
280    ],
281    static_libs: [
282        "androidx.annotation_annotation",
283        "modules-utils-build_system",
284        "modules-utils-expresslog",
285        "modules-utils-preconditions",
286        "modules-utils-shell-command-handler",
287        "modules-utils-statemachine",
288        "netd_aidl_interface-lateststable-java",
289        "networkstack-client",
290        "net-utils-framework-common",
291        // See note on statsprotos when adding/updating proto build rules
292        "datastallprotosnano",
293        "statsprotos",
294        "captiveportal-lib",
295        "net-utils-device-common-ip",
296        "net-utils-device-common-netlink",
297        "net-utils-device-common-struct",
298        "net-utils-device-common-struct-base",
299    ],
300}
301
302// The versions of the android library containing network stack code compiled for each SDK variant.
303android_library {
304    name: "NetworkStackApiCurrentLib",
305    defaults: [
306        "NetworkStackReleaseApiLevel",
307        "NetworkStackAndroidLibraryDefaults",
308        "ConnectivityNextEnableDefaults",
309    ],
310    srcs: [
311        "src/**/*.java",
312        ":statslog-networkstack-java-gen-current",
313    ],
314    static_libs: [
315        "NetworkStackApiCurrentShims",
316    ],
317    manifest: "AndroidManifestBase.xml",
318    visibility: [
319        "//frameworks/base/tests/net/integration",
320        "//packages/modules/Connectivity/Tethering/tests/integration",
321        "//packages/modules/NetworkStack/tests/unit",
322        "//packages/modules/NetworkStack/tests/integration",
323    ],
324}
325
326android_library {
327    name: "NetworkStackApiStableLib",
328    defaults: [
329        "NetworkStackReleaseApiLevel",
330        "NetworkStackAndroidLibraryDefaults",
331    ],
332    srcs: [
333        "src/**/*.java",
334        ":statslog-networkstack-java-gen-stable",
335    ],
336    static_libs: [
337        "NetworkStackApiStableShims",
338    ],
339    manifest: "AndroidManifestBase.xml",
340    visibility: [
341        "//packages/modules/Connectivity/Tethering/tests/integration",
342        "//packages/modules/Connectivity/tests/integration",
343        "//packages/modules/NetworkStack/tests/unit",
344        "//packages/modules/NetworkStack/tests/integration",
345    ],
346}
347
348java_library {
349    name: "DhcpPacketLib",
350    defaults: ["NetworkStackReleaseApiLevel"],
351    srcs: [
352        "src/android/net/DhcpResults.java",
353        "src/android/net/dhcp/Dhcp*Packet.java",
354    ],
355    libs: [
356        "androidx.annotation_annotation",
357        "framework-annotations-lib",
358    ],
359    static_libs: [
360        "modules-utils-build",
361        "net-utils-framework-common",
362        "networkstack-client",
363        "net-utils-device-common",
364    ],
365    // If this library is ever used outside of tests, it should not use "Dhcp*Packet", and specify
366    // its contents explicitly.
367    visibility: [
368        "//packages/modules/Connectivity/Tethering/tests/integration",
369        "//packages/modules/Connectivity/tests/cts/net",
370    ],
371}
372
373java_library {
374    name: "ApfGeneratorLib",
375    defaults: ["NetworkStackReleaseApiLevel"],
376    srcs: [
377        "src/android/net/apf/ApfConstants.java",
378        "src/android/net/apf/ApfCounterTracker.java",
379        "src/android/net/apf/ApfV4Generator.java",
380        "src/android/net/apf/ApfV4GeneratorBase.java",
381        "src/android/net/apf/ApfV6Generator.java",
382        "src/android/net/apf/ApfV6GeneratorBase.java",
383        "src/android/net/apf/BaseApfGenerator.java",
384    ],
385    libs: [
386        "androidx.annotation_annotation",
387        "framework-annotations-lib",
388    ],
389    static_libs: [
390        "net-utils-framework-common",
391    ],
392    visibility: [
393        "//packages/modules/Connectivity/tests/cts/net",
394    ],
395}
396
397java_genrule {
398    name: "NetworkStackJarJarRules",
399    tool_files: [
400        // com.android.networkstack.apishim is already in the target package that is not jarjared,
401        // so it does not matter whether ApiStable or ApiCurrent is used to generate the jarjar
402        // rules. Use ApiStable to be based on most stable release configuration and be buildable
403        // in all branches.
404        ":NetworkStackApiStableLib{.jar}",
405        "jarjar-excludes.txt",
406    ],
407    tools: [
408        "jarjar-rules-generator",
409    ],
410    out: ["NetworkStackJarJarRules.txt"],
411    cmd: "$(location jarjar-rules-generator) " +
412        "$(location :NetworkStackApiStableLib{.jar}) " +
413        "--prefix com.android.networkstack " +
414        "--excludes $(location jarjar-excludes.txt) " +
415        "--output $(out)",
416    visibility: [
417        "//packages/modules/NetworkStack/tests/unit",
418        "//packages/modules/NetworkStack/tests/integration",
419        "//packages/modules/Connectivity/tests:__subpackages__",
420        "//packages/modules/Connectivity/Tethering/tests/integration",
421    ],
422}
423
424// Common defaults for compiling the actual APK, based on the NetworkStackApiXBase android libraries
425java_defaults {
426    name: "NetworkStackAppDefaults",
427    privileged: true,
428    jni_libs: [
429        "libnativehelper_compat_libc++",
430        "libnetworkstackutilsjni",
431    ],
432    // Resources already included in NetworkStackBase
433    resource_dirs: [],
434    jarjar_rules: ":NetworkStackJarJarRules",
435    use_embedded_native_libs: true,
436    optimize: {
437        ignore_warnings: false,
438        proguard_flags_files: ["proguard.flags"],
439    },
440}
441
442// Non-updatable network stack running in the system server process for devices not using the module
443android_app {
444    name: "InProcessNetworkStack",
445    defaults: [
446        "NetworkStackAppDefaults",
447        "NetworkStackReleaseApiLevel",
448        "ConnectivityNextEnableDefaults",
449    ],
450    static_libs: ["NetworkStackApiCurrentLib"],
451    certificate: "platform",
452    manifest: "AndroidManifest_InProcess.xml",
453    // InProcessNetworkStack is a replacement for NetworkStack
454    overrides: [
455        "NetworkStack",
456        "NetworkStackNext",
457    ],
458    // The InProcessNetworkStack goes together with the PlatformCaptivePortalLogin, which replaces
459    // the default CaptivePortalLogin.
460    required: [
461        "PlatformCaptivePortalLogin",
462    ],
463}
464
465// Pre-merge the AndroidManifest for NetworkStackNext, so that its manifest can be merged on top
466android_library {
467    name: "NetworkStackNextManifestBase",
468    defaults: [
469        "NetworkStackAppDefaults",
470        "NetworkStackReleaseApiLevel",
471        "ConnectivityNextEnableDefaults",
472    ],
473    static_libs: ["NetworkStackApiCurrentLib"],
474    manifest: "AndroidManifest.xml",
475}
476
477// NetworkStack build targeting the current API release, for testing on in-development SDK
478android_app {
479    name: "NetworkStackNext",
480    defaults: [
481        "NetworkStackAppDefaults",
482        "NetworkStackReleaseApiLevel",
483        "ConnectivityNextEnableDefaults",
484    ],
485    static_libs: ["NetworkStackNextManifestBase"],
486    certificate: "networkstack",
487    manifest: "AndroidManifest_Next.xml",
488    required: [
489        "privapp_whitelist_com.android.networkstack",
490    ],
491    updatable: true,
492}
493
494// Updatable network stack for finalized API
495android_app {
496    name: "NetworkStack",
497    defaults: [
498        "NetworkStackAppDefaults",
499        "NetworkStackReleaseApiLevel",
500    ],
501    static_libs: ["NetworkStackApiStableLib"],
502    certificate: "networkstack",
503    manifest: "AndroidManifest.xml",
504    required: [
505        "privapp_whitelist_com.android.networkstack",
506    ],
507    updatable: true,
508}
509
510cc_library_shared {
511    name: "libnetworkstackutilsjni",
512    srcs: [
513        "jni/network_stack_utils_jni.cpp",
514    ],
515    header_libs: [
516        "bpf_headers",
517    ],
518    sdk_version: "30",
519    min_sdk_version: "30",
520    shared_libs: [
521        "liblog",
522        "libnativehelper_compat_libc++",
523    ],
524    static_libs: [
525        "libnetjniutils",
526    ],
527
528    // We cannot use plain "libc++" here to link libc++ dynamically because it results in:
529    //   java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found
530    // even if "libc++" is added into jni_libs below. Adding "libc++_shared" into jni_libs doesn't
531    // build because soong complains of:
532    //   module NetworkStack missing dependencies: libc++_shared
533    //
534    // So, link libc++ statically. This means that we also need to ensure that all the C++ libraries
535    // we depend on do not dynamically link libc++. This is currently the case, because liblog is
536    // C-only and libnativehelper_compat_libc also uses stl: "c++_static".
537    //
538    // TODO: find a better solution for this in R.
539    stl: "c++_static",
540    cflags: [
541        "-Wall",
542        "-Werror",
543        "-Wno-unused-parameter",
544    ],
545}
546
547genrule {
548    name: "statslog-networkstack-java-gen-current",
549    tools: ["stats-log-api-gen"],
550    cmd: "$(location stats-log-api-gen) --java $(out) --module network_stack" +
551        " --javaPackage com.android.networkstack.metrics --javaClass NetworkStackStatsLog" +
552        " --minApiLevel 30",
553    out: ["com/android/networkstack/metrics/NetworkStackStatsLog.java"],
554}
555
556genrule {
557    name: "statslog-networkstack-java-gen-stable",
558    tools: ["stats-log-api-gen"],
559    cmd: "$(location stats-log-api-gen) --java $(out) --module network_stack" +
560        " --javaPackage com.android.networkstack.metrics --javaClass NetworkStackStatsLog" +
561        " --minApiLevel 30",
562    out: ["com/android/networkstack/metrics/NetworkStackStatsLog.java"],
563}
564
565version_code_networkstack_next = "300000000"
566version_code_networkstack_test = "999999999"
567
568genrule {
569    name: "NetworkStackTestAndroidManifest",
570    srcs: ["AndroidManifest.xml"],
571    out: ["TestAndroidManifest.xml"],
572    cmd: "sed -E 's/versionCode=\"[0-9]+\"/versionCode=\"" +
573        version_code_networkstack_test +
574        "\"/' $(in) > $(out)",
575    visibility: ["//visibility:private"],
576}
577
578android_app {
579    name: "TestNetworkStack",
580    defaults: [
581        "NetworkStackAppDefaults",
582        "NetworkStackReleaseApiLevel",
583    ],
584    static_libs: ["NetworkStackApiStableLib"],
585    certificate: "networkstack",
586    manifest: ":NetworkStackTestAndroidManifest",
587    required: [
588        "privapp_whitelist_com.android.networkstack",
589    ],
590}
591
592// When adding or modifying protos, the jarjar rules and possibly proguard rules need
593// to be updated: proto libraries may pull additional static libraries.
594java_library_static {
595    name: "statsprotos",
596    proto: {
597        type: "lite",
598    },
599    srcs: [
600        "src/com/android/networkstack/metrics/stats.proto",
601    ],
602    static_libs: [
603        "networkstackprotos",
604    ],
605    defaults: ["NetworkStackReleaseApiLevel"],
606}
607