-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathinstall-android-sdk.ps1
More file actions
221 lines (181 loc) · 7.28 KB
/
install-android-sdk.ps1
File metadata and controls
221 lines (181 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
Param(
[string] $InstallDestination = $null
)
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
$HOME_DIR = if ($env:HOME) { $env:HOME } else { $env:USERPROFILE }
$manualLocation = "$HOME_DIR/android-sdk"
if ($InstallDestination) {
$manualLocation = $InstallDestination
}
function Get-SdkManager {
param (
[string] $SdkPath,
[string] $Indent = " "
)
Write-Host "${Indent}Looking for the SDK Manager in $SdkPath..."
if (-not $SdkPath -or -not (Test-Path $SdkPath)) {
Write-Host "${Indent}No SDK Manager found."
return ""
}
$cmdline = Join-Path "$SdkPath" "cmdline-tools"
if (Test-Path $cmdline) {
Write-Host "${Indent} Found command line tools:"
$cmdline | Get-ChildItem | ForEach-Object {
Write-Host "${Indent} $_"
}
} else {
Write-Host "${Indent} No command line tools found."
return ""
}
Write-Host "${Indent} Selecting the latest command line tools..."
$sdkExt = if (-not $IsMacOS -and -not $IsLinux) { ".bat" } else { "" }
$versions = Get-ChildItem (Join-Path "$SdkPath" "cmdline-tools")
$latest = ($versions | Select-Object -Last 1)[0]
Write-Host "${Indent} Latest command line tools found at $latest."
$sdkmanager = Join-Path "$latest" "bin" "sdkmanager$sdkExt"
if (-not (Test-Path $sdkmanager)) {
Write-Host "${Indent}No SDK Manager found."
return ""
}
Write-Host "${Indent}Found the SDK Manager at $sdkmanager."
return $sdkmanager
}
function Get-Adb {
param (
[string] $SdkPath,
[string] $Indent = " "
)
Write-Host "${Indent}Looking for ADB in $SdkPath..."
$platformtools = Join-Path "$SdkPath" "platform-tools"
if (Test-Path $platformtools) {
Write-Host "${Indent} Found platform tools: $platformtools"
} else {
Write-Host "${Indent} No platform tools found."
return ""
}
$adbExt = if (-not $IsMacOS -and -not $IsLinux) { ".exe" } else { "" }
$adb = Get-ChildItem $platformtools | Where-Object { $_.Name -eq "adb$adbExt" }
if (-not $adb) {
Write-Host "${Indent}No ADB found."
return ""
}
Write-Host "${Indent}Found ADB at $adb."
return $adb
}
function Get-AndroidSdk {
param (
[string] $SdkPath,
[string] $PathType,
[string] $Indent = " "
)
Write-Host "${Indent}Looking for the Android SDK in $SdkPath ($PathType)..."
if (-not $SdkPath -or -not (Test-Path $SdkPath)) {
Write-Host "${Indent}No Android SDK found."
return ""
}
$sdkmanager = Get-SdkManager -SdkPath "$SdkPath"
if (-not (Test-Path $sdkmanager)) {
Write-Host "${Indent}No SDK Manager found, not going to use this one."
return ""
}
$adb = Get-Adb -SdkPath "$SdkPath"
if (-not (Test-Path $adb)) {
Write-Host "${Indent}No ADB found, not going to use this one."
return ""
}
Write-Host "${Indent}Using the Android SDK at $SdkPath."
return "$SdkPath"
}
Write-Host "Looking for an existing Android SDK..."
# try use some environment variables
$sdk = Get-AndroidSdk -SdkPath "$env:ANDROID_SDK_ROOT" -PathType "ANDROID_SDK_ROOT"
if (-not $sdk -and "$env:ANDROID_HOME") {
$sdk = Get-AndroidSdk -SdkPath "$env:ANDROID_HOME" -PathType "ANDROID_HOME"
}
# look in program files from VS
if (-not $sdk -and -not $IsMacOS -and -not $IsLinux) {
$pfsdk = Join-Path "${env:ProgramFiles(x86)}" "Android" "android-sdk"
$sdk = Get-AndroidSdk -SdkPath "$pfsdk" -PathType "Program Files"
}
# look in the previous location
if (-not $sdk) {
$sdk = Get-AndroidSdk -SdkPath "$manualLocation" -PathType "Previous install location"
}
# Nothing was found, so we need to install the SDK
if (-not $sdk) {
Write-Host "No Android SDK found, will download and install the latest..."
$sdk = $manualLocation
Write-Host " Install destination is $sdk."
# detect
Write-Host " Getting the latest command line tool info..."
$repoUrl = "https://dl.google.com/android/repository/repository2-1.xml"
[xml] $repoXml = Invoke-WebRequest -Uri $repoUrl
$stable = $repoXml.DocumentElement.remotePackage | Where-Object { $_.path -eq "cmdline-tools;latest" -and $_.channelRef.ref -eq "channel-0" }
$platform = if ($IsMacOS) { "macosx" } elseif ($IsLinux) { "linux" } else { "windows" }
$archive = $stable.archives.archive | Where-Object { $_."host-os" -eq $platform }
$filename = $archive.complete.url
Write-Host " Latest command line tools are $filename..."
$url = "https://dl.google.com/android/repository/$filename"
$sdkTemp = "$HOME_DIR/android-sdk-temp"
$zip = "$sdkTemp/$filename"
# download
if (-not (Test-Path $zip)) {
Write-Host " Downloading SDK ($url) to '$zip'..."
New-Item -ItemType Directory -Force -Path "$sdkTemp" | Out-Null
(New-Object System.Net.WebClient).DownloadFile("$url", "$zip")
}
# extract
if (Test-Path "$sdkTemp/extracted") {
Write-Host " Removing old extracted SDK from $sdkTemp/extracted..."
Remove-Item -Recurse -Force "$sdkTemp/extracted"
}
if (Test-Path "$sdk") {
Write-Host " Removing old SDK from $sdk..."
Remove-Item -Recurse -Force "$sdk"
}
Write-Host " Extracting SDK to $sdkTemp/extracted..."
try {
if ($IsLinux -or $IsMacOS) {
unzip -q "$zip" -d "$sdkTemp/extracted"
if (-not $?) {
throw " Failed to extract the SDK."
}
} else {
[System.IO.Compression.ZipFile]::ExtractToDirectory("$zip", "$sdkTemp/extracted")
}
} catch {
Write-Host " Failed to extract the SDK, deleting the file as it may be corrupt..."
Remove-Item -Force "$zip"
exit 1
}
Write-Host " Moving command line tools to $sdk/cmdline-tools/latest..."
New-Item -ItemType Directory -Force -Path "$sdk/cmdline-tools/latest" | Out-Null
Move-Item "${sdkTemp}/extracted/cmdline-tools/*" "$sdk/cmdline-tools/latest/"
Write-Host "Installation complete."
}
$sdkmanager = Get-SdkManager -SdkPath "$sdk" -Indent ""
$adb = Get-Adb -SdkPath "$sdk" -Indent ""
Write-Host "Using Android SDK at $sdk."
Write-Host ""
Write-Host "Setting environment variables..."
# make sure that the SDK is in:
# - ANDROID_HOME and ANDROID_SDK_ROOT for native things
# - AndroidSdkDirectory for .NET for Android
# - ANDROID_SDK_MANAGER_PATH for the path to the SDK Manager
Write-Host "Setting environment variable ANDROID_HOME=$sdk"
Write-Host "Setting environment variable ANDROID_SDK_ROOT=$sdk"
Write-Host "Setting environment variable AndroidSdkDirectory=$sdk"
Write-Host "Setting environment variable ANDROID_SDK_MANAGER_PATH=$sdkmanager"
Write-Host "Setting environment variable ANDROID_ADB_PATH=$adb"
Write-Host "##vso[task.setvariable variable=ANDROID_SDK_ROOT;]$sdk";
Write-Host "##vso[task.setvariable variable=ANDROID_HOME;]$sdk";
Write-Host "##vso[task.setvariable variable=AndroidSdkDirectory;]$sdk";
Write-Host "##vso[task.setvariable variable=ANDROID_SDK_MANAGER_PATH;]$sdkmanager";
Write-Host "##vso[task.setvariable variable=ANDROID_ADB_PATH;]$adb";
$env:ANDROID_SDK_ROOT = $sdk
$env:ANDROID_HOME = $sdk
$env:AndroidSdkDirectory = $sdk
$env:ANDROID_SDK_MANAGER_PATH = $sdkmanager
$env:ANDROID_ADB_PATH = $adb
exit $LASTEXITCODE