• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 '''
19Create an AVD. e.g.
20  AVD_IMG_NAME="sdk_gcar" \
21  API_LEVEL=30 \
22  DEFAULT_DEVICE_XML="./resource/devices.xml" \
23  ./create_avd.sh
24'''
25}
26
27# Export a variable=default_value if it's empty, e.g.
28# exportVar "VAR" "defaultValue"
29exportVar()
30{
31  var=$1
32  defaultValue=$2
33  if [[ -z "${!var}" ]]; then
34    export "${var}=$defaultValue"
35    echo "${var}=$defaultValue"
36  fi
37}
38
39# Export a variable with the value of an XML element in a file as, e.g.
40# getValue "TAG_ID" "d:id" "./resource/devices.xml"
41getValue() {
42  var=$1
43  elementTag=$2
44  file=$3
45  #e.g.        <d:id>aosp_car_10_landscape</d:id>
46  value=$(sed -n -e "s/.*<${elementTag}>\(.*\)<\/${elementTag}>.*/\1/p" "${file}")
47  export "${var}=${value}"
48  echo "${var}=${value}"
49}
50
51if [[ -z "${AVD_IMG_NAME}" || -z "${API_LEVEL}" ]]; then
52  readme
53  exit
54fi
55
56MY_NAME=$0
57SCRIPT_NAME="${MY_NAME##*/}"
58SCRIPT_DIR="${MY_NAME%/$SCRIPT_NAME}"
59echo "Running from ${SCRIPT_DIR}"
60
61# set up for Linux or macOS
62OS="$(uname -s)"
63echo "Running on $OS"
64if [[ $OS == "Linux" ]]; then
65  export ANDROID_SDK_ROOT="${HOME}/Android/Sdk"
66  SED_I_CMD="sed -i "
67elif [[ $OS == "Darwin" ]]; then
68  export ANDROID_SDK_ROOT="${HOME}/Library/Android/sdk"
69  SED_I_CMD="sed -i ''"
70else
71  echo "ERROR: this does not work on $OS"
72  exit
73fi
74echo "ANDROID_SDK_ROOT=${ANDROID_SDK_ROOT}"
75
76if [[ -z ${AVD_IMG_DIR} ]]; then
77  AVD_IMG_DIR="${ANDROID_SDK_ROOT}/system-images/android-${API_LEVEL}/${AVD_IMG_NAME}"
78fi
79echo "AVD_IMG_DIR=${AVD_IMG_DIR}"
80
81if [[ ! -e "${AVD_IMG_DIR}" ]]; then
82  echo "ERROR: no AVD image found at ${AVD_IMG_DIR}"
83  exit
84fi
85
86ABI=$(ls "${AVD_IMG_DIR}")
87if [[ "${ABI}" != "x86" && "${ABI}" != "x86_64" ]]; then
88  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 "
89  exit
90fi
91echo "ABI=${ABI}"
92
93# sdk_gcar_30
94AVD_NAME="${AVD_IMG_NAME}_${API_LEVEL}"
95AVD_INI="${HOME}/.android/avd/${AVD_NAME}.ini"
96AVD_PATH_REL="avd/${AVD_NAME}.avd"
97AVD_PATH="${HOME}/.android/${AVD_PATH_REL}"
98AVD_CONFIG_INI="${AVD_PATH}/config.ini"
99
100if [[ -e "${AVD_PATH}" ]]; then
101  echo "NOTE: ${AVD_PATH} exists. Delete them if to recreate, e.g."
102  echo "rm -r ${AVD_PATH} || rm ${AVD_INI}"
103  exit
104fi
105
106# It should be aligned with avdmanager.
107# May use https://developer.android.com/studio/command-line/avdmanager, but
108# 1. more setup steps required for users & 2. slow on parsing image folders
109# So here just add  the bare minimum set up.
110echo "Create ${AVD_INI}"
111echo "avd.ini.encoding=UTF-8
112path=${AVD_PATH}
113path.rel=${AVD_PATH_REL}
114target=android-${API_LEVEL}" >> "${AVD_INI}"
115
116echo
117mkdir -p "${AVD_PATH}"
118USERDATA_DIR="${AVD_PATH}/userdata.img"
119echo "Create Avd Userdata at ${USERDATA_DIR}"
120cp "${AVD_IMG_DIR}/${ABI}/userdata.img" "${USERDATA_DIR}"
121
122DEVICE_XML="${AVD_IMG_DIR}/${ABI}/devices.xml"
123if [[ ! -e "${DEVICE_XML}" ]]; then
124  echo "NOTE: No ${DEVICE_XML} in the AVD image."
125  if [[ ! -e "${DEFAULT_DEVICE_XML}" ]]; then
126    echo "ERROR: Need to set DEFAULT_DEVICE_XML at least."
127    exit
128  else
129    echo "COPY: ${DEFAULT_DEVICE_XML} ${DEVICE_XML}"
130    cp "${DEFAULT_DEVICE_XML}" "${DEVICE_XML}"
131    echo "NOTE: Please restart Android Studio, so the new devics.xml will be properly loaded."
132  fi
133fi
134echo "DEVICE_XML=${DEVICE_XML}"
135getValue "DEVICE_PROFILE_NAME" "d:id" "${DEVICE_XML}"
136getValue "MANUFACTURER" "d:manufacturer" "${DEVICE_XML}"
137getValue "ORIENTATION" "d:screen-orientation" "${DEVICE_XML}"
138getValue "TAG_ID" "d:tag-id" "${DEVICE_XML}"
139getValue "SKIN" "d:skin" "${DEVICE_XML}"
140
141
142if [[ "${ORIENTATION}" == "land" ]]; then
143  ORIENTATION="landscape"
144fi
145
146echo "Create ${AVD_CONFIG_INI}"
147exportVar "TAG_ID" "android-automotive"
148# MANUFACTURER & DEVICE_PROFILE_NAME need to match those in devices.xml
149exportVar "MANUFACTURER" "Android"
150exportVar "DEVICE_PROFILE_NAME" "aosp_car_10_landscape"
151exportVar "ORIENTATION" "landscape"
152exportVar "NCORE" 4
153exportVar "RAM" "4096"
154exportVar "HEAP" "576"
155exportVar "DATA" "6G"
156
157echo "avd.ini.encoding=UTF-8
158AvdId=${AVD_NAME}
159avd.ini.displayname=${AVD_NAME}
160tag.display=${AVD_NAME}
161tag.id=${TAG_ID}
162image.sysdir.1=system-images/android-${API_LEVEL}/${AVD_IMG_NAME}/${ABI}/
163abi.type=${ABI}
164hw.arc=false
165hw.device.manufacturer=${MANUFACTURER}
166hw.device.name=${DEVICE_PROFILE_NAME}
167hw.cpu.arch=${ABI}
168hw.cpu.ncore=${NCORE}
169hw.gpu.enabled=yes
170hw.gpu.mode=auto
171hw.keyboard=yes
172hw.initialOrientation=${ORIENTATION}
173hw.ramSize=${RAM}
174vm.heapSize=${HEAP}
175disk.dataPartition.size=${DATA}" > "${AVD_CONFIG_INI}"
176
177if [[ -z "${SKIN}" ]]; then
178  skinSettings="showDeviceFrame=no"
179else
180  skinSettings="showDeviceFrame=yes
181skin.dynamic=yes
182skin.name=${SKIN}
183skin.path=${AVD_IMG_DIR}/${ABI}/skins/${SKIN}"
184fi
185echo "${skinSettings}" >> "${AVD_CONFIG_INI}"
186