Refactor the handling of the released-flagged-apis.txt
Previously, it was intended to just be a list of Metalava options that
are passed to Metalava when building SDK snapshots that need to control
which flagged APIs, if any, are included in the snapshot. However, that
would be quite tedious to maintain with lots of duplication and direct
dependency on Metalava making it useless for other purposes. This
change switches it to be a list of flags to release and a script will
convert that into Metalava options.
It also adds `com.android.net.flags.register_nsd_offload_engine_api` to
the set of flags which were previously released in SDK extension #10.
Ignore-AOSP-First: Needed in `udc-mainline-prod` only at the moment.
Bug: 313398274
Test: UNBUNDLED_BUILD_SDKS_FROM_SOURCE=true vendor/google/build/mainline_modules_sdks.sh --build-release=next
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:b4d70a6bdd5d35544a0f1d25adf27c0ac9d0e968)
Merged-In: I5e9c00175b8c32eea0ad7fdfb30ce19487f286cb
Change-Id: I5e9c00175b8c32eea0ad7fdfb30ce19487f286cb
diff --git a/keep-flagged-apis.sh b/keep-flagged-apis.sh
new file mode 100755
index 0000000..84f6583
--- /dev/null
+++ b/keep-flagged-apis.sh
@@ -0,0 +1,33 @@
+#!/bin/bash -e
+#
+# Copyright 2023 Google Inc. All rights reserved.
+#
+# 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
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Convert a list of flags in the input file to a list of metalava options
+# that will keep the APIs for those flags will hiding all other flagged
+# APIs.
+
+FLAGS="$1"
+
+FLAGGED="android.annotation.FlaggedApi"
+
+# Convert the list of feature flags in the input file to Metalava options
+# of the form `--hide-annotation !android.annotation.FlaggedApi("<flag>")`
+# to prevent the annotated APIs from being hidden, i.e. include the annotated
+# APIs in the SDK snapshots. This also preserves the line comments, they will
+# be ignored by Metalava but might be useful when debugging.
+sed "s|^[^#].*$|--hide-annotation '!$FLAGGED(\"\\0\")'|" $FLAGS
+
+# Hide all flagged APIs, unless listed above.
+echo "--hide-annotation $FLAGGED"