blob: bef8db4744a126fbc03e8d0bf6407cdb42e5e734 [file] [log] [blame]
Alan Viverettea693ece2020-11-17 16:23:42 -05001#!/usr/bin/env bash
Alan Viverettea693ece2020-11-17 16:23:42 -05002
Jeff Gastond25425c2020-12-02 09:53:57 -05003function usage() {
Jeff Gaston616585c2021-12-10 12:27:27 -05004 echo "Usage: studiow [--clean] [--reinstall] [--profile] <project subset>"
Jeff Gaston1db6bb82020-12-22 13:12:23 -05005 echo
6 echo "OPTIONS"
7 echo " --clean"
8 echo " Clear (with backup) generated files (settings, caches, etc) before launching"
9 echo
10 echo " --reinstall"
11 echo " Remove and re-download Studio itself. Also implies --clean"
Jeff Gaston616585c2021-12-10 12:27:27 -050012 echo " --profile"
13 echo " Enables profiling of Studio"
Jeff Gastond25425c2020-12-02 09:53:57 -050014 echo
15 echo "Project subsets:"
16 echo " m, main"
Jeff Gaston4ac74c32021-01-29 12:12:20 -050017 echo " Open the project subset main: non-Compose Jetpack libraries"
Jeff Gastond25425c2020-12-02 09:53:57 -050018 echo
19 echo " c, compose"
Jeff Gaston4ac74c32021-01-29 12:12:20 -050020 echo " Open the project subset compose"
Jeff Gastond25425c2020-12-02 09:53:57 -050021 echo
22 echo " f, flan"
Jeff Gaston4ac74c32021-01-29 12:12:20 -050023 echo " Open the project subset flan: Fragment, Lifecycle, Activity, and Navigation"
Jeff Gastond25425c2020-12-02 09:53:57 -050024 echo
Gyumin Simff92d182021-02-02 19:27:59 +090025 echo " media"
26 echo " Open the project subset media: Media, Media2, and MediaRouter"
27 echo
Flavio Lerda726911b2021-01-18 18:15:06 +000028 echo " w, wear"
29 echo " Open the project subset for Wear OS libraries"
30 echo
Jeff Gastond25425c2020-12-02 09:53:57 -050031 echo " a, all"
Jeff Gaston4ac74c32021-01-29 12:12:20 -050032 echo " Open the project subset all"
Jeff Gastond25425c2020-12-02 09:53:57 -050033 echo
34 exit 1
35}
36
Jeff Gaston1db6bb82020-12-22 13:12:23 -050037cd "$(dirname $0)"
38
39subsetArg=""
40clean=false
41reinstall=false
42projectSubset=""
Jeff Gaston616585c2021-12-10 12:27:27 -050043profile=false
Jeff Gaston1db6bb82020-12-22 13:12:23 -050044while [ "$1" != "" ]; do
45 arg="$1"
46 shift
47 # parse options
48 if [ "$arg" == "--clean" ]; then
49 clean=true
50 continue
51 fi
52 if [ "$arg" == "--reinstall" ]; then
53 clean=true
54 reinstall=true
55 continue
56 fi
Jeff Gaston616585c2021-12-10 12:27:27 -050057 if [ "$arg" == "--profile" ]; then
58 profile=true
59 continue
60 fi
Jeff Gaston1db6bb82020-12-22 13:12:23 -050061 # parse arguments
62 subsetArg="$arg"
63 newSubset=""
64 if [ "$subsetArg" == "m" -o "$subsetArg" == "main" ]; then
Jeff Gaston4ac74c32021-01-29 12:12:20 -050065 newSubset=main
Jeff Gaston1db6bb82020-12-22 13:12:23 -050066 fi
67 if [ "$subsetArg" == "c" -o "$subsetArg" == "compose" ]; then
Jeff Gaston4ac74c32021-01-29 12:12:20 -050068 newSubset=compose
Jeff Gaston1db6bb82020-12-22 13:12:23 -050069 fi
70 if [ "$subsetArg" == "f" -o "$subsetArg" == "flan" ]; then
Jeff Gaston4ac74c32021-01-29 12:12:20 -050071 newSubset=flan
Jeff Gaston1db6bb82020-12-22 13:12:23 -050072 fi
Gyumin Simff92d182021-02-02 19:27:59 +090073 if [ "$subsetArg" == "media" ]; then
74 newSubset=media
75 fi
Flavio Lerda726911b2021-01-18 18:15:06 +000076 if [ "$subsetArg" == "w" -o "$subsetArg" == "wear" ]; then
77 newSubset=wear
78 fi
Jeff Gaston1db6bb82020-12-22 13:12:23 -050079 if [ "$subsetArg" == "a" -o "$subsetArg" == "all" ]; then
Jeff Gaston4ac74c32021-01-29 12:12:20 -050080 newSubset=all
Jeff Gaston1db6bb82020-12-22 13:12:23 -050081 fi
82 if [ "$newSubset" == "" ]; then
83 echo "Unrecognized argument: '$subsetArg'"
84 usage
85 fi
86 if [ "$projectSubset" != "" ]; then
87 echo "Unrecognized argument '$subsetArg', cannot specify project subset more than once"
88 usage
89 fi
90 projectSubset=$newSubset
91done
92
93if [ "$projectSubset" == "" ]; then
94 echo "Project subset is required"
Jeff Gastond25425c2020-12-02 09:53:57 -050095 usage
96fi
97
Jeff Gaston1db6bb82020-12-22 13:12:23 -050098export ANDROIDX_PROJECTS=$projectSubset
99
100# ensures the nonexistence of a file or directory, and makes a backup
101function remove() {
102 path="$1"
103 backup="$(dirname $path)/studio-backup/$(basename $path)"
104 if [ -e "$path" ]; then
105 echo "Moving $path to $backup"
Flavio Lerda833bae42021-11-11 15:55:34 +0000106 rm -rf "$backup"
Jeff Gaston1db6bb82020-12-22 13:12:23 -0500107 mkdir -p "$(dirname $backup)"
108 mv "$path" "$backup"
109 fi
110}
111
112if [ "$reinstall" == "true" ]; then
113 # remove Studio itself so Gradle will re-download it
Flavio Lerda833bae42021-11-11 15:55:34 +0000114 rm -rf studio
Jeff Gastond25425c2020-12-02 09:53:57 -0500115fi
Jeff Gaston1db6bb82020-12-22 13:12:23 -0500116
Jeff Gaston616585c2021-12-10 12:27:27 -0500117if [ "$profile" == "true" ]; then
118 PROFILE_FILE=/tmp/report.json
119 traceConfig="$(cd development/studio && pwd)/profile.config"
120 rm -f "$PROFILE_FILE"
121 echo "Profile file will be $PROFILE_FILE , which will be able to be loaded into chrome://tracing"
122 echo
123 echo "If you find that too many or too few function calls are included in the trace, modify $traceConfig"
124 echo
125 tracerJar="$(cd ../../prebuilts/androidx/external/com/android/tools/tracer/agent && pwd)/trace_agent.jar"
126 # Make sure to set _JAVA_OPTIONS before starting Gradle
127 export _JAVA_OPTIONS="$_JAVA_OPTIONS -javaagent:${tracerJar}=${traceConfig}"
128fi
129
Jeff Gaston1db6bb82020-12-22 13:12:23 -0500130if [ "$clean" == "true" ]; then
131 # remove studio-specific caches
132
133 # make backups of files that users might have customized
134 remove ~/.AndroidStudioAndroidX
135 remove ~/.AndroidStudioAndroidXPlayground
136 remove ~/.android
137 # delete (without backup) files that users won't have customized
138 git clean -fdX .idea/
Jeff Gaston616585c2021-12-10 12:27:27 -0500139
Jeff Gaston1db6bb82020-12-22 13:12:23 -0500140 # remove gradle caches too and build
141 ./cleanBuild.sh -y studio
142else
Jeff Gaston616585c2021-12-10 12:27:27 -0500143 # If not a clean launch, then a Gradle daemon might be running.
144 # If profiling, we need to stop the Gradle daemon to make sure any changes to the
145 # profiling properties will be used.
146 if [ "$profile" == "true" ]; then
147 ./gradlew --stop
148 fi
149
Jeff Gaston1db6bb82020-12-22 13:12:23 -0500150 # ask gradle to launch studio
Aurimas Liutikas0db05722021-07-20 13:06:27 -0700151 ./gradlew :studio
Jeff Gaston1db6bb82020-12-22 13:12:23 -0500152fi