Skip to content

Commit 8066613

Browse files
committed
Add script to create an asset-only bundle
1 parent 437afcd commit 8066613

1 file changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2023 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# -------------------------------------------------------------------------------------
18+
#
19+
# Creates an asset-only bundle with the given asset packs.
20+
# Each asset pack should be a .zip with a manifest/AndroidManifest.xml file and an assets folder.
21+
# Specify:
22+
# - The path for the android SDK (typically ~/Android/Sdk)
23+
# - The path to a deploy jar of bundletool
24+
# - The asset-only bundle version tag and app versions
25+
# - The list of asset packs to add
26+
# - The output file
27+
# - the keystore information to sign the output bundle with your Play Console upload key
28+
29+
USAGE='''
30+
.../create_asset_only_bundle.sh
31+
--android-sdk=${HOME}/Android/Sdk
32+
--bundletool=/google/bin/releases/bundletool/public/bundletool-all.jar
33+
--output=output_directory/output_bundle.aab
34+
--packdir=dir/containing/my/packs/
35+
--packs=assetpack1,assetpack2
36+
--tmpdir=/tmp/my-assetonly-tmp-dir/
37+
--app-versions=10,12
38+
--version-tag=mynewassets
39+
[--ks=dir/to/keystore]
40+
[--key=your-key]
41+
[--ks_password=your-pw]
42+
'''
43+
44+
for i in "$@"
45+
do
46+
case $i in
47+
--android-sdk=*)
48+
ANDROID_SDK="${i#*=}"
49+
shift # past argument=value
50+
;;
51+
-b=*|--bundletool=*)
52+
BUNDLETOOL="${i#*=}"
53+
shift # past argument=value
54+
;;
55+
--packdir=*)
56+
PACKDIR="${i#*=}"
57+
shift # past argument=value
58+
;;
59+
-p=*|--packs=*)
60+
PACKS="${i#*=}"
61+
shift # past argument=value
62+
;;
63+
-o=*|--output=*)
64+
OUTPUT="${i#*=}"
65+
shift # past argument=value
66+
;;
67+
--app-versions=*)
68+
APP_VERSIONS="${i#*=}"
69+
shift # past argument=value
70+
;;
71+
--version-tag=*)
72+
VERSION_TAG="${i#*=}"
73+
shift # past argument=value
74+
;;
75+
--tmpdir=*)
76+
TMPDIR="${i#*=}"
77+
shift # past argument=value
78+
;;
79+
--ks=*)
80+
KEYSTORE="${i#*=}"
81+
shift # past argument=value
82+
;;
83+
--key=*)
84+
KEY="${i#*=}"
85+
shift # past argument=value
86+
;;
87+
--ks_password=*)
88+
KEY_PASSWORD="${i#*=}"
89+
shift # past argument=value
90+
;;
91+
*)
92+
echo "$USAGE"
93+
exit 1
94+
;;
95+
esac
96+
done
97+
98+
if [[ -z "$ANDROID_SDK" ]] \
99+
|| [[ -z "$BUNDLETOOL" ]] \
100+
|| [[ -z "$PACKDIR" ]] \
101+
|| [[ -z "$PACKS" ]] \
102+
|| [[ -z "$OUTPUT" ]] \
103+
|| [[ -z "$APP_VERSIONS" ]] \
104+
|| [[ -z "$VERSION_TAG" ]] \
105+
|| [[ -z "$TMPDIR" ]];
106+
then
107+
echo "$USAGE"
108+
exit 1
109+
fi
110+
111+
WD="$TMPDIR/assetonly_tmp"
112+
mkdir -p "${WD}"
113+
114+
PACK_LIST=$(echo "$PACKS" | tr -s ',' ' ')
115+
116+
BUNDLE_CONFIG='{
117+
"asset_modules_config": {
118+
"app_version": ['${APP_VERSIONS}'],
119+
"asset_version_tag": "'${VERSION_TAG}'"
120+
},
121+
"type": "ASSET_ONLY"
122+
}'
123+
124+
echo "Preparing packs..."
125+
126+
for PACK in $PACK_LIST
127+
do
128+
PACKNAME=$(basename -- "$PACK")
129+
PACKNAME=${PACKNAME%.*}
130+
unzip -q "${PACKDIR}/${PACK}.zip" -d "$WD/$PACKNAME"
131+
mv "$WD/$PACKNAME/manifest/AndroidManifest.xml" manifest.xml
132+
"$ANDROID_SDK/build-tools/28.0.3/aapt2" link \
133+
--proto-format \
134+
--output-to-dir \
135+
-o "$WD/$PACKNAME/manifest" \
136+
--manifest manifest.xml \
137+
-I "${ANDROID_SDK}/platforms/android-28/android.jar"
138+
rm "$WD/$PACKNAME/manifest/resources.pb"
139+
rm manifest.xml
140+
done
141+
142+
cd "$WD"
143+
144+
for MODULE in *
145+
do
146+
cd "$MODULE"
147+
zip -q -r -0 "../$MODULE.zip" *
148+
cd ..
149+
done
150+
151+
MODULES=$(ls *.zip)
152+
# Replacing spaces and line breaks with comma.
153+
MODULES=$(echo "$MODULES" | tr -s ' \n' ',')
154+
# Removing trailing comma if present.
155+
MODULES=${MODULES%,}
156+
157+
echo "Preparing BundleConfig..."
158+
BUNDLE_CONFIG_FILE="$WD/BundleConfig.json"
159+
echo "${BUNDLE_CONFIG}" > "$BUNDLE_CONFIG_FILE"
160+
161+
echo "Running 'bundletool build-bundle --modules=$MODULES --output=$OUTPUT" \
162+
"--config=$BUNDLE_CONFIG_FILE'"
163+
java -jar "$BUNDLETOOL" build-bundle \
164+
--modules="$MODULES" \
165+
--output="$OUTPUT" \
166+
--config="$BUNDLE_CONFIG_FILE"
167+
168+
cd ..
169+
rm -rf assetonly_tmp
170+
171+
if [[ ! -z "$KEYSTORE" ]]; then
172+
echo "Signing"
173+
jarsigner -keystore "$KEYSTORE" \
174+
-storepass "$KEY_PASSWORD" \
175+
"$OUTPUT" \
176+
"$KEY"
177+
fi
178+
179+
echo "Done"

0 commit comments

Comments
 (0)