1#!/bin/bash 2 3# Copyright (C) 2021 The Android Open Source Project 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 17readme() { 18 echo ''' 19Add an AVD image to Android SDK dir. So you can use team in Android Studio AVD manager. e.g. 20OEM_AVD_ZIP="$HOME/Downloads/sdk-repo-linux-system-images-eng.USER.zip" \ 21 VARIANT="avd_car" \ 22 API_LEVEL=30 \ 23 ./add_avd_img.sh 24''' 25} 26 27if [[ -z $VARIANT || -z $API_LEVEL || -z $OEM_AVD_ZIP ]]; then 28 readme 29 exit 30fi 31 32MY_NAME=$0 33SCRIPT_NAME=${MY_NAME##*/} 34SCRIPT_DIR=${MY_NAME%/$SCRIPT_NAME} 35echo Running from $SCRIPT_DIR 36 37# set up for Linux or macOS 38OS="$(uname -s)" 39echo "Running on $OS" 40if [[ $OS == "Linux" ]]; then 41 export ANDROID_SDK_DIR="$HOME/Android/Sdk" 42 SED_I_CMD="sed -i " 43elif [[ $OS == "Darwin" ]]; then 44 export ANDROID_SDK_DIR="$HOME/Library/Android/sdk" 45 SED_I_CMD="sed -i ''" 46else 47 echo "ERROR: this does not work on $OS" 48 exit 49fi 50echo "ANDROID_SDK_DIR=$ANDROID_SDK_DIR" 51 52PKG_XML_TEMPLATE="$SCRIPT_DIR/resource/package_template.xml" 53if [[ ! -e $PKG_XML_TEMPLATE ]]; then 54 echo "Make sure there is package.xml template at: $PKG_XML_TEMPLATE." 55 exit 56fi 57echo "PKG_XML_TEMPLATE=$PKG_XML_TEMPLATE" 58 59SYS_IMG_DIR="$ANDROID_SDK_DIR/system-images" 60if [[ ! -d $SYS_IMG_DIR ]]; then 61 echo "ERROR: can not find the AVD system images dir: $SYS_IMG_DIR" 62 echo "Try to reinstall Android Studio & SDK as https://developer.android.com/studio" 63fi 64 65# expected system-images folder format: android-apiLevel/variant/arch/ 66AVD_DIR="$ANDROID_SDK_DIR/system-images/android-$API_LEVEL/$VARIANT" 67if [[ -d $AVD_DIR ]]; then 68 echo "NOTE: $AVD_DIR exists already. Delete it if to replace: rm -r $AVD_DIR" 69else 70 mkdir -p $AVD_DIR 71 unzip $OEM_AVD_ZIP -d $AVD_DIR 72fi 73 74ABI=$(ls $AVD_DIR) 75if [[ $ABI != "x86" && $ABI != "x86_64" ]]; then 76 echo "ERROR: AVD image zip file format incorrect as $AVD_DIR should contain x86\ or x86_64\ as: https://source.android.com/devices/automotive/start/avd#pack-an-avd-image-zip-file " 77 exit 78fi 79echo "ABI=$ABI" 80 81PKG_XML_FILE="$AVD_DIR/$ABI/package.xml" 82if [[ -e $PKG_XML_FILE ]]; then 83 echo "Delete $PKG_XML_FILE for this setup." 84 rm $PKG_XML_FILE 85fi 86 87echo "Generate $PKG_XML_FILE accorindly" 88sed "s/Pkg.Path/system-images;android-$API_LEVEL;$VARIANT;$ABI/" $PKG_XML_TEMPLATE > $PKG_XML_FILE 89$SED_I_CMD "s/AndroidVersion.ApiLevel/$API_LEVEL/" $PKG_XML_FILE 90$SED_I_CMD "s/SystemImage.TagId/android-automotive/" $PKG_XML_FILE 91$SED_I_CMD "s/SystemImage.TagDisplay/$VARIANT/" $PKG_XML_FILE 92$SED_I_CMD "s/SystemImage.Abi/$ABI/" $PKG_XML_FILE 93$SED_I_CMD "s/Addon.VendorId/$VARIANT/" $PKG_XML_FILE 94$SED_I_CMD "s/Addon.VendorDisplay/$VARIANT/" $PKG_XML_FILE 95$SED_I_CMD "s/Display.Name/$VARIANT/" $PKG_XML_FILE 96 97echo 98echo "Restart Android Studio & select [System Image] in [Virtual Device Configuration], [AVD Manager]" 99echo "ADDED: Hardware Category: Automotive, API Level: $API_LEVEL, ABI: $ABI, Target: $VARIANT" 100echo 101