blob: b2e68807b315029d88cdcfa6045ab723f778d51e [file] [log] [blame]
Jeff Gaston259a9052018-10-19 14:55:19 -04001#!/bin/bash
2set -e
Dustin Lamb8289c82019-06-25 15:48:18 -07003set -m
Jeff Gaston259a9052018-10-19 14:55:19 -04004
5# This is a wrapper script that runs the specific version of Android Studio that is recommended for developing in this repository.
6# (This serves a similar purpose to gradlew)
7
8
9function getStudioUrl() {
Jeff Gaston149350d2019-03-08 20:32:29 -050010 propertiesFile="${scriptDir}/buildSrc/studio_versions.properties"
11 version="$(grep "studio_version=" ${propertiesFile} | sed 's/[^=]*=//')"
12 ideaMajorVersion="$(grep "idea_major_version=" ${propertiesFile} | sed 's/[^=]*=//')"
13 buildNumber="$(grep "studio_build_number=" ${propertiesFile} | sed 's/[^=]*=//')"
Jeff Gaston259a9052018-10-19 14:55:19 -040014 osName="$1"
Louis Pullen-Freilichc5750d82019-04-23 14:23:03 +010015 extension=""
16 if [ "${osName}" == "linux" ]; then
17 extension="tar.gz"
18 else
19 extension="zip"
20 fi
21 studioUrl="https://dl.google.com/dl/android/studio/ide-zips/${version}/android-studio-ide-${ideaMajorVersion}.${buildNumber}-${osName}.${extension}"
Jeff Gaston259a9052018-10-19 14:55:19 -040022 echo "${studioUrl}"
23}
24
Ashley Rosebf3111f2019-07-02 17:20:26 -040025acceptsLicenseAgreement="false"
Ashley Roseb77fc622019-07-03 14:35:32 -040026runStudio="true"
Jeff Gaston259a9052018-10-19 14:55:19 -040027scriptDir="$(cd $(dirname $0) && pwd)"
Alan Viverette2fa738f2018-11-28 16:38:53 -050028projectDir=$scriptDir
Jeff Gaston259a9052018-10-19 14:55:19 -040029tempDir="${scriptDir}/studio"
30function getOsName() {
31 unameOutput="$(uname)"
32 osName=""
33 if [ "${unameOutput}" == "Linux" ]; then
34 osName="linux"
35 else
36 osName="mac"
37 fi
38 echo "${osName}"
39}
40osName="$(getOsName)"
41studioUrl="$(getStudioUrl $osName)"
42studioDestName="$(basename ${studioUrl})"
43studioZipPath="${tempDir}/${studioDestName}"
Louis Pullen-Freilichc5750d82019-04-23 14:23:03 +010044studioUnzippedPath="$(echo ${studioZipPath} | sed 's/\.zip$//; s/\.tar.gz$//')"
Jeff Gaston259a9052018-10-19 14:55:19 -040045
Ashley Rosebf3111f2019-07-02 17:20:26 -040046function parseOptions() {
47 while :; do
48 case "$1" in
49 -y|--accept-license-agreement)
50 acceptsLicenseAgreement="true"
51 ;;
52 --update-only)
Ashley Roseb77fc622019-07-03 14:35:32 -040053 runStudio="false"
Ashley Rosebf3111f2019-07-02 17:20:26 -040054 ;;
55 *)
56 break
57 esac
58
59 shift
60 done
61}
62
Ashley Roseb77fc622019-07-03 14:35:32 -040063
Jeff Gaston259a9052018-10-19 14:55:19 -040064function downloadFile() {
65 fromUrl="$1"
66 destPath="$2"
67 tempPath="${destPath}.tmp"
Louis Pullen-Freilichc5750d82019-04-23 14:23:03 +010068 if [ -f "${destPath}" ]; then
69 read -r -n 1 -p "File already exists. Do you want to delete and re-download? [Y/n]? " reply
70
71 if [ ! -z "${reply}" ]; then
72 # Fix missing newline
73 echo
74 fi
75
76 case "${reply}" in
77 [yY]|"")
78 rm "${destPath}"
79 ;;
80 *)
81 esac
82 fi
83
84 if [ -f "${destPath}" ]; then
85 echo "Using existing file from ${destPath}"
86 else
87 echo "Downloading ${fromUrl} to ${destPath}"
88 curl "${fromUrl}" > "${tempPath}"
89 mv "${tempPath}" "${destPath}"
90 fi
Jeff Gaston259a9052018-10-19 14:55:19 -040091}
92
Ashley Rose37f957b2019-02-12 16:07:39 -050093function findStudioMacAppPath() {
94 echo "$(find "${studioUnzippedPath}" -type d -depth 1 -name "Android Studio*.app")"
95}
96
97function getLicensePath() {
98 if [ "${osName}" == "mac" ]; then
99 appPath="$(findStudioMacAppPath)"
100 echo "${appPath}/Contents/Resources/LICENSE.txt"
101 else
102 echo "${studioUnzippedPath}/android-studio/LICENSE.txt"
103 fi
104}
105
Jeff Gaston259a9052018-10-19 14:55:19 -0400106function checkLicenseAgreement() {
107 # TODO: Is there a more official way to check that the user accepts the license?
Ashley Rose09aa4d02019-02-12 16:40:54 -0500108
109 licenseAcceptedPath="${studioUnzippedPath}/STUDIOW_LICENSE_ACCEPTED"
110
111 if [ ! -f "${licenseAcceptedPath}" ]; then
Ashley Rosebf3111f2019-07-02 17:20:26 -0400112 if [ "${acceptsLicenseAgreement}" == "true" ]; then
Ashley Rose09aa4d02019-02-12 16:40:54 -0500113 touch "${licenseAcceptedPath}"
114 else
Ashley Rose7d9c4102019-02-19 11:52:14 -0500115 read -r -n 1 -p "Do you accept the license agreement at $(getLicensePath) [Y/n]? " reply
116
117 if [ ! -z "${reply}" ]; then
Ashley Roseb77fc622019-07-03 14:35:32 -0400118 # Fix missing newline
Ashley Rose7d9c4102019-02-19 11:52:14 -0500119 echo
120 fi
121
122 case "${reply}" in
123 [yY]|"")
124 touch "${licenseAcceptedPath}"
125 ;;
126 *)
127 exit 1
128 ;;
129 esac
Ashley Rose09aa4d02019-02-12 16:40:54 -0500130 fi
Jeff Gaston259a9052018-10-19 14:55:19 -0400131 fi
132}
133
Sam Gilbertc547ea82019-06-14 14:58:35 -0400134# Temporary fix. Remove this after fixing b/135183535
135function updateJvmHeapSize() {
136 if [ "${osName}" == "mac" ]; then
137 sed -i '' 's/-Xmx.*/-Xmx8g/' "$(findStudioMacAppPath)/Contents/bin/studio.vmoptions"
138 else
139 sed -i 's/-Xmx.*/-Xmx8g/' "${studioUnzippedPath}/android-studio/bin/studio64.vmoptions"
140 sed -i 's/-Xmx.*/-Xmx4g/' "${studioUnzippedPath}/android-studio/bin/studio.vmoptions"
141 fi
142}
143
Jeff Gaston259a9052018-10-19 14:55:19 -0400144function updateStudio() {
145 # skip if already up-to-date
Louis Pullen-Freilichc5750d82019-04-23 14:23:03 +0100146 if stat "${studioUnzippedPath}" >/dev/null 2>/dev/null; then
Jeff Gaston259a9052018-10-19 14:55:19 -0400147 # already up-to-date
148 return
149 fi
150
151 mkdir -p "${tempDir}"
152 downloadFile "${studioUrl}" "${studioZipPath}"
153 echo
154
155 echo "Removing previous installations"
156 ls "${tempDir}" | grep -v "^${studioDestName}\$" | sed "s|^|${tempDir}/|" | xargs rm -rf
157
158 echo "Unzipping"
Louis Pullen-Freilichc5750d82019-04-23 14:23:03 +0100159 if [ ${studioZipPath: -7} == ".tar.gz" ]; then
160 mkdir "${studioUnzippedPath}"
161 tar -xvf "${studioZipPath}" -C "${studioUnzippedPath}"
162 else
163 unzip "${studioZipPath}" -d "${studioUnzippedPath}"
164 fi
Jeff Gaston259a9052018-10-19 14:55:19 -0400165}
166
Jeff Gaston57575ca2019-05-03 13:50:07 -0400167function ensureLocalPropertiesUpdated() {
168 testPath="${projectDir}/local.properties"
169 populaterCommand="./gradlew help"
170 if [ ! -f "${testPath}" ]; then
171 cd "$scriptDir"
172 echo "Creating $testPath by running '$populaterCommand'"
173 eval $populaterCommand
174 fi
175}
176
Louis Pullen-Freilich1b57674a2019-06-13 19:51:30 +0100177# ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED environment variable prevents Studio from showing IDE
178# inspection warnings for nullability issues, if the context is deprecated
179# This environment variable is consumed by InteroperabilityDetector.kt
180
Jeff Gaston259a9052018-10-19 14:55:19 -0400181function runStudioLinux() {
182 studioPath="${studioUnzippedPath}/android-studio/bin/studio.sh"
183 echo "$studioPath &"
Alan Viverette6ed162a2018-11-30 14:35:55 -0500184 env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \
185 STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \
Louis Pullen-Freilich1b57674a2019-06-13 19:51:30 +0100186 ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED="true" \
Alan Viverette6ed162a2018-11-30 14:35:55 -0500187 "${studioPath}" "${projectDir}" &
Jeff Gaston259a9052018-10-19 14:55:19 -0400188}
189
190function runStudioMac() {
Ashley Rose37f957b2019-02-12 16:07:39 -0500191 appPath="$(findStudioMacAppPath)"
192 echo "open ${appPath}"
Alan Viverette6ed162a2018-11-30 14:35:55 -0500193 env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \
194 STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \
Louis Pullen-Freilich1b57674a2019-06-13 19:51:30 +0100195 ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED="true" \
Ashley Rose37f957b2019-02-12 16:07:39 -0500196 open -a "${appPath}" "${projectDir}"
Jeff Gaston259a9052018-10-19 14:55:19 -0400197}
198
199function runStudio() {
Sam Gilbertc547ea82019-06-14 14:58:35 -0400200 updateJvmHeapSize
Chris Craikb84f61b2018-10-31 14:18:06 -0700201 if [ "${osName}" == "mac" ]; then
Jeff Gaston259a9052018-10-19 14:55:19 -0400202 runStudioMac
Chris Craikb84f61b2018-10-31 14:18:06 -0700203 else
204 runStudioLinux
Jeff Gaston259a9052018-10-19 14:55:19 -0400205 fi
206}
207
208function main() {
Ashley Roseb77fc622019-07-03 14:35:32 -0400209 parseOptions "$@"
Jeff Gaston259a9052018-10-19 14:55:19 -0400210 updateStudio
Ashley Roseb77fc622019-07-03 14:35:32 -0400211 if [ "${runStudio}" == "true" ]; then
Ashley Rosebf3111f2019-07-02 17:20:26 -0400212 checkLicenseAgreement
213 ensureLocalPropertiesUpdated
214 runStudio
215 fi
Jeff Gaston259a9052018-10-19 14:55:19 -0400216}
217
Ashley Roseb77fc622019-07-03 14:35:32 -0400218main "$@"