xref: /aosp_15_r20/prebuilts/sdk/create-android-plus-updatable-jar.sh (revision 344a7f5ef16c479e7a7f54ee6567a9d112f9e72b)
1*344a7f5eSAndroid Build Coastguard Worker#!/usr/bin/env bash
2*344a7f5eSAndroid Build Coastguard Worker#
3*344a7f5eSAndroid Build Coastguard Worker# Copyright 2024 - The Android Open Source Project
4*344a7f5eSAndroid Build Coastguard Worker#
5*344a7f5eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*344a7f5eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*344a7f5eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*344a7f5eSAndroid Build Coastguard Worker#
9*344a7f5eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*344a7f5eSAndroid Build Coastguard Worker#
11*344a7f5eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*344a7f5eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*344a7f5eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*344a7f5eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*344a7f5eSAndroid Build Coastguard Worker# limitations under the License.
16*344a7f5eSAndroid Build Coastguard Worker#
17*344a7f5eSAndroid Build Coastguard Worker
18*344a7f5eSAndroid Build Coastguard Workerset -e
19*344a7f5eSAndroid Build Coastguard Worker
20*344a7f5eSAndroid Build Coastguard Worker# This script was created to fix up some issues with the android.jar
21*344a7f5eSAndroid Build Coastguard Worker# files for `module-lib` and `system-server` in historical releases
22*344a7f5eSAndroid Build Coastguard Worker# 30 to 34. Modifying binary files is potentially dangerous so this was
23*344a7f5eSAndroid Build Coastguard Worker# created and put under version control to make it easy for someone to
24*344a7f5eSAndroid Build Coastguard Worker# verify the results if needed.
25*344a7f5eSAndroid Build Coastguard Worker
26*344a7f5eSAndroid Build Coastguard Worker# The basic function is:
27*344a7f5eSAndroid Build Coastguard Worker# * Save away a copy of the original android.jar, if not already
28*344a7f5eSAndroid Build Coastguard Worker#   present.
29*344a7f5eSAndroid Build Coastguard Worker# * Create a new android.jar that combines the original android.jar
30*344a7f5eSAndroid Build Coastguard Worker#   with the jars from the missing mainline modules.
31*344a7f5eSAndroid Build Coastguard Worker# * Add a README.md to explain what happened.
32*344a7f5eSAndroid Build Coastguard Worker
33*344a7f5eSAndroid Build Coastguard WorkerSOURCE=${BASH_SOURCE[0]}
34*344a7f5eSAndroid Build Coastguard WorkerSOURCE_DIR=$(dirname $SOURCE)
35*344a7f5eSAndroid Build Coastguard Worker
36*344a7f5eSAndroid Build Coastguard Worker# Change directory to top.
37*344a7f5eSAndroid Build Coastguard Workercd $SOURCE_DIR/../..
38*344a7f5eSAndroid Build Coastguard WorkerTOP=$PWD
39*344a7f5eSAndroid Build Coastguard Workerecho "Changed directory to $TOP; all paths are relative to that."
40*344a7f5eSAndroid Build Coastguard WorkerSOURCE_DIR="prebuilts/sdk"
41*344a7f5eSAndroid Build Coastguard Worker
42*344a7f5eSAndroid Build Coastguard Workerif [[ $# == 0 ]]; then
43*344a7f5eSAndroid Build Coastguard Worker  echo "$SOURCE <version>+" >&2
44*344a7f5eSAndroid Build Coastguard Worker  exit 1
45*344a7f5eSAndroid Build Coastguard Workerfi
46*344a7f5eSAndroid Build Coastguard Worker
47*344a7f5eSAndroid Build Coastguard WorkerVERSIONS=$@
48*344a7f5eSAndroid Build Coastguard WorkerSURFACES=(
49*344a7f5eSAndroid Build Coastguard Worker  module-lib
50*344a7f5eSAndroid Build Coastguard Worker  system-server
51*344a7f5eSAndroid Build Coastguard Worker)
52*344a7f5eSAndroid Build Coastguard Worker
53*344a7f5eSAndroid Build Coastguard Workerfunction find_updatable_modules() {
54*344a7f5eSAndroid Build Coastguard Worker  typeset SURFACE=$1
55*344a7f5eSAndroid Build Coastguard Worker
56*344a7f5eSAndroid Build Coastguard Worker  # Scan the prebuilts/sdk numbered directories in the specified surface for
57*344a7f5eSAndroid Build Coastguard Worker  # jars. Exclude the following jars:
58*344a7f5eSAndroid Build Coastguard Worker  # 1. Any jar starting with android. That will exclude android.net.ipsec.ike
59*344a7f5eSAndroid Build Coastguard Worker  #    as well but that will be added explicitly below.
60*344a7f5eSAndroid Build Coastguard Worker  # 2. core-for-system-modules.jar as that is not an updatable module.
61*344a7f5eSAndroid Build Coastguard Worker  # 3. art as while that is an updatable module it must already be included in
62*344a7f5eSAndroid Build Coastguard Worker  #    the android.jar otherwise it would be unusable.
63*344a7f5eSAndroid Build Coastguard Worker  # 4. runtime-i18n as that is not updatable and must be included as it is
64*344a7f5eSAndroid Build Coastguard Worker  #    needed by art.
65*344a7f5eSAndroid Build Coastguard Worker  find $SOURCE_DIR -maxdepth 3 -type f -name \*.jar | \
66*344a7f5eSAndroid Build Coastguard Worker    grep -vE "/(android.*|core-for-system-modules|art|runtime-i18n)\.jar$" | \
67*344a7f5eSAndroid Build Coastguard Worker    grep -E "/[1-9][0-9]*/$SURFACE" | \
68*344a7f5eSAndroid Build Coastguard Worker    xargs -n1 basename | \
69*344a7f5eSAndroid Build Coastguard Worker    sort -u
70*344a7f5eSAndroid Build Coastguard Worker}
71*344a7f5eSAndroid Build Coastguard Worker
72*344a7f5eSAndroid Build Coastguard Worker# Add module-lib modules that do not fit into the standard naming pattern.
73*344a7f5eSAndroid Build Coastguard WorkerMODULE_LIB_MODULES=(
74*344a7f5eSAndroid Build Coastguard Worker  android.net.ipsec.ike
75*344a7f5eSAndroid Build Coastguard Worker)
76*344a7f5eSAndroid Build Coastguard Worker
77*344a7f5eSAndroid Build Coastguard Worker# Add additional module-lib updatable modules.
78*344a7f5eSAndroid Build Coastguard Workermapfile -O 1 -t MODULE_LIB_MODULES < <(find_updatable_modules module-lib)
79*344a7f5eSAndroid Build Coastguard Worker
80*344a7f5eSAndroid Build Coastguard Worker# Find all the system-server updatable modules.
81*344a7f5eSAndroid Build Coastguard Workermapfile -t SYSTEM_SERVER_MODULES < <(find_updatable_modules system-server)
82*344a7f5eSAndroid Build Coastguard Worker
83*344a7f5eSAndroid Build Coastguard Workerfor VERSION in $VERSIONS
84*344a7f5eSAndroid Build Coastguard Workerdo
85*344a7f5eSAndroid Build Coastguard Worker  if ((VERSION < 30)); then
86*344a7f5eSAndroid Build Coastguard Worker    echo "$SOURCE: invalid version: $VERSION must be >= 30" >&2
87*344a7f5eSAndroid Build Coastguard Worker    exit 1
88*344a7f5eSAndroid Build Coastguard Worker  fi
89*344a7f5eSAndroid Build Coastguard Worker
90*344a7f5eSAndroid Build Coastguard Worker  VERSION_DIR=$SOURCE_DIR/$VERSION
91*344a7f5eSAndroid Build Coastguard Worker  if [ ! -d $VERSION_DIR ]; then
92*344a7f5eSAndroid Build Coastguard Worker    echo "$SOURCE: invalid version: $VERSION" >&2
93*344a7f5eSAndroid Build Coastguard Worker    exit 1
94*344a7f5eSAndroid Build Coastguard Worker  fi
95*344a7f5eSAndroid Build Coastguard Worker
96*344a7f5eSAndroid Build Coastguard Worker  for SURFACE in ${SURFACES[@]}
97*344a7f5eSAndroid Build Coastguard Worker  do
98*344a7f5eSAndroid Build Coastguard Worker    SURFACE_DIR=$VERSION_DIR/$SURFACE
99*344a7f5eSAndroid Build Coastguard Worker    if [ ! -d $SURFACE_DIR ]; then
100*344a7f5eSAndroid Build Coastguard Worker      echo "$SOURCE: $SURFACE_DIR does not exist" >&2
101*344a7f5eSAndroid Build Coastguard Worker      exit 1
102*344a7f5eSAndroid Build Coastguard Worker    fi
103*344a7f5eSAndroid Build Coastguard Worker
104*344a7f5eSAndroid Build Coastguard Worker    MODULE_LIB_DIR=$VERSION_DIR/module-lib
105*344a7f5eSAndroid Build Coastguard Worker
106*344a7f5eSAndroid Build Coastguard Worker
107*344a7f5eSAndroid Build Coastguard Worker    ANDROID_JAR=$SURFACE_DIR/android.jar
108*344a7f5eSAndroid Build Coastguard Worker
109*344a7f5eSAndroid Build Coastguard Worker    NEW_JAR=$SURFACE_DIR/android-plus-updatable.jar
110*344a7f5eSAndroid Build Coastguard Worker    rm -f $NEW_JAR
111*344a7f5eSAndroid Build Coastguard Worker
112*344a7f5eSAndroid Build Coastguard Worker    echo
113*344a7f5eSAndroid Build Coastguard Worker    echo "Merging the following jars into $NEW_JAR:"
114*344a7f5eSAndroid Build Coastguard Worker    echo "  $ANDROID_JAR"
115*344a7f5eSAndroid Build Coastguard Worker    JARS=$ANDROID_JAR
116*344a7f5eSAndroid Build Coastguard Worker
117*344a7f5eSAndroid Build Coastguard Worker    # Add all the module-lib jars to the list of jars to merge.
118*344a7f5eSAndroid Build Coastguard Worker    # These are always added, even for the system-server as the
119*344a7f5eSAndroid Build Coastguard Worker    # system-server directory only includes service-* modules but the
120*344a7f5eSAndroid Build Coastguard Worker    # system-server API surface is a super set of module-lib so should
121*344a7f5eSAndroid Build Coastguard Worker    # include everything that is in the module-lib updatable modules
122*344a7f5eSAndroid Build Coastguard Worker    # too.
123*344a7f5eSAndroid Build Coastguard Worker    for MODULE in ${MODULE_LIB_MODULES[@]}
124*344a7f5eSAndroid Build Coastguard Worker    do
125*344a7f5eSAndroid Build Coastguard Worker      MODULE_JAR=$MODULE_LIB_DIR/$MODULE
126*344a7f5eSAndroid Build Coastguard Worker      if [ -f $MODULE_JAR ]; then
127*344a7f5eSAndroid Build Coastguard Worker        echo "  $MODULE_JAR"
128*344a7f5eSAndroid Build Coastguard Worker        JARS="$JARS $MODULE_JAR"
129*344a7f5eSAndroid Build Coastguard Worker      fi
130*344a7f5eSAndroid Build Coastguard Worker    done
131*344a7f5eSAndroid Build Coastguard Worker
132*344a7f5eSAndroid Build Coastguard Worker    if [[ $SURFACE == "system-server" ]]; then
133*344a7f5eSAndroid Build Coastguard Worker      # Add all the system-server jars to the list of jars to merge.
134*344a7f5eSAndroid Build Coastguard Worker      for MODULE in ${SYSTEM_SERVER_MODULES[@]}
135*344a7f5eSAndroid Build Coastguard Worker      do
136*344a7f5eSAndroid Build Coastguard Worker        MODULE_JAR=$SURFACE_DIR/$MODULE
137*344a7f5eSAndroid Build Coastguard Worker        if [ -f $MODULE_JAR ]; then
138*344a7f5eSAndroid Build Coastguard Worker          echo "  $MODULE_JAR"
139*344a7f5eSAndroid Build Coastguard Worker          JARS="$JARS $MODULE_JAR"
140*344a7f5eSAndroid Build Coastguard Worker        fi
141*344a7f5eSAndroid Build Coastguard Worker      done
142*344a7f5eSAndroid Build Coastguard Worker    fi
143*344a7f5eSAndroid Build Coastguard Worker
144*344a7f5eSAndroid Build Coastguard Worker    # Merge all the zips ignoring duplicates (should only be
145*344a7f5eSAndroid Build Coastguard Worker    # META-INF/MANIFEST.MF) created a sorted (-s) jar (-j).
146*344a7f5eSAndroid Build Coastguard Worker    merge_zips -ignore-duplicates -j -s $NEW_JAR $JARS
147*344a7f5eSAndroid Build Coastguard Worker
148*344a7f5eSAndroid Build Coastguard Worker    README=$SURFACE_DIR/README.md
149*344a7f5eSAndroid Build Coastguard Worker    cat > $README <<EOF
150*344a7f5eSAndroid Build Coastguard Worker## Updated since finalization
151*344a7f5eSAndroid Build Coastguard Worker
152*344a7f5eSAndroid Build Coastguard WorkerThe android-plus-updatable.jar has been added to this directory after
153*344a7f5eSAndroid Build Coastguard Workerfinalization as the android.jar in this directory does not include updatable
154*344a7f5eSAndroid Build Coastguard Workermodules. That is because doing so in the source would lead to dependency cycles
155*344a7f5eSAndroid Build Coastguard Workerand the prebuilts have to match the source structure so that apps can build
156*344a7f5eSAndroid Build Coastguard Workeragainst either prebuilts or sources.
157*344a7f5eSAndroid Build Coastguard Worker
158*344a7f5eSAndroid Build Coastguard WorkerThe lack of updatable modules in android.jar caused problems for the
159*344a7f5eSAndroid Build Coastguard Workerapi-version.xml generation as that requires a single jar containing all the
160*344a7f5eSAndroid Build Coastguard WorkerAPIs for each surface of each API version. See b/337836752 for more
161*344a7f5eSAndroid Build Coastguard Workerinformation.
162*344a7f5eSAndroid Build Coastguard WorkerEOF
163*344a7f5eSAndroid Build Coastguard Worker
164*344a7f5eSAndroid Build Coastguard Worker    # Adding the new files to git.
165*344a7f5eSAndroid Build Coastguard Worker    (cd $SOURCE_DIR; git add ${NEW_JAR#$SOURCE_DIR/} ${README#$SOURCE_DIR/})
166*344a7f5eSAndroid Build Coastguard Worker
167*344a7f5eSAndroid Build Coastguard Worker  done
168*344a7f5eSAndroid Build Coastguard Workerdone
169