#!/bin/bash -eux # # Copyright (C) 2023 The Android Open Source Project # # 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. # # This script is used to build widevine apex bundle readonly -a ARCHS=( "arm" "arm64" "arm64only" "x86" "x86_64" "x86_64only" ) function die() { format=$1; shift; printf >&2 "$format\n" "$@"; exit 1; } # $@: additional build targets # inputs: none # outputs: ${DIST_DIR}/${arch}/${apex}-base.zip function build_modules() { # keep DIST_DIR to override for each arch local dist_dir="${DIST_DIR}" for arch in "${ARCHS[@]}"; do # override DIST_DIR for each arch DIST_DIR="${dist_dir}/${arch}" \ TARGET_PRODUCT="widevine_generic" \ TARGET_BASE_PRODUCT="build/target/product/module_${arch}.mk" \ build/soong/soong_ui.bash --make-mode "$@" \ ALWAYS_EMBED_NOTICES=true \ apps_only \ dist \ lint-check done # restore DIST_DIR DIST_DIR="${dist_dir}" } # $1: apex # inputs: ${DIST_DIR}/${arch}/${apex}-base.zip # outputs: ${DIST_DIR}/${apex}.aab function build_bundle() { local -r apex=$1 # collect base.zip files local -a zips for arch in "${ARCHS[@]}"; do zips+=("${DIST_DIR}/${arch}/${apex}-base.zip") done local -ra zips # extract bundle_config.json from one of zip files unzip -o -d "${TMPDIR}/${apex}" "${zips[0]}" bundle_config.json # merge base.zip files local -r base_zip="${TMPDIR}/${apex}/base.zip" ${MERGE_ZIPS} \ -ignore-duplicates \ -stripFile bundle_config.json \ "${base_zip}" \ "${zips[@]}" # Use prebuilt bundletool JAR # build bundle "${JAVA_EXE}" -jar "${BUNDLETOOL_PREBUILT_JAR}" build-bundle \ --overwrite \ --modules "${base_zip}" \ --config "${TMPDIR}/${apex}/bundle_config.json" \ --output "${DIST_DIR}/${apex}.aab" } # inputs: ${DIST_DIR}/${arch}/${apex}-base.zip # outputs: ${DIST_DIR}/${apex}.aab function build_bundles() { # Assume TARGET_BUILD_APPS is set in the environment local app for app in ${TARGET_BUILD_APPS}; do build_bundle "${app}" done } # inputs: ${DIST_DIR}/${apex}.aab # outputs: ${DIST_DIR}/dev_keys_signed/${apex}/${apex}.apks function sign_bundles() { ${DEV_SIGN_BUNDLE} \ --java_binary_path "${JAVA_EXE}" \ --input_dir "${DIST_DIR}" \ --output_dir "${DIST_DIR}/dev_keys_signed" } function configure_build() { # Assign to a variable and eval that, since bash ignores any error status from # the command substitution if it's directly on the eval line. local -r vars="$(TARGET_PRODUCT='' build/soong/soong_ui.bash --dumpvars-mode \ --vars="ANDROID_JAVA_HOME TMPDIR OUT_DIR TARGET_BUILD_APPS DIST_DIR")" eval "${vars}" # Define prebuilt bundletool path and JAVA_EXE # Assume exactly one JAR matches local -a jar_matches=(prebuilts/bundletool/bundletool-all-*.jar) declare -gr BUNDLETOOL_PREBUILT_JAR="${jar_matches[0]}" declare -gr JAVA_EXE="${ANDROID_JAVA_HOME}/bin/java" # Keep declarations for built tools declare -gr MERGE_ZIPS=${OUT_DIR}/host/linux-x86/bin/merge_zips declare -gr DEV_SIGN_BUNDLE=${OUT_DIR}/host/linux-x86/bin/dev_sign_bundle } function main() { [[ -e "build/make/core/Makefile" ]] || die "$0 must be run from the top of the Android source tree." configure_build build_modules merge_zips dev_sign_bundle build_bundles # use prebuilt bundletool & merge_zips sign_bundles # use built dev_sign_bundle } main