-
Notifications
You must be signed in to change notification settings - Fork 71
/
install_minimum_android_sdk_prerequisites.py
168 lines (142 loc) · 6.47 KB
/
install_minimum_android_sdk_prerequisites.py
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
from __future__ import print_function
import os
import os.path
import shutil
import subprocess
import sys
import platform
#
# Script Compatibility:
# - Python 3.9.6
#
# Run script from command line with:
# python install_minimum_android_sdk_prerequisites.py
#
SUPPORTED_PYTHON_PLATFORMS = ['Windows', 'Linux', 'Darwin']
# Version numbers, SHA256 and URLs taken from
## https://developer.android.com/studio
ANDROID_SDK_TOOLS_VERSION = '11076708'
ANDROID_SDK_TOOLS_SHASUM_LINUX = '2d2d50857e4eb553af5a6dc3ad507a17adf43d115264b1afc116f95c92e5e258'
ANDROID_SDK_TOOLS_SHASUM_WINDOWS = '4d6931209eebb1bfb7c7e8b240a6a3cb3ab24479ea294f3539429574b1eec862'
ANDROID_SDK_VERSION = '35'
def fail(message, *args, **kwargs):
print((message % args).format(**kwargs))
sys.exit(1)
def which_raw(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def which(program):
if (sys.platform == 'win32'):
which_result = which_raw(program + ".bat")
if not which_result:
which_result = which_raw(program + ".cmd")
if not which_result:
which_result = which_raw(program + ".exe")
return which_result
else:
return which_raw(program)
def change_permissions_recursive(path, mode):
import os
for root, dirs, files in os.walk(path, topdown=False):
for dir in [os.path.join(root,d) for d in dirs]:
os.chmod(dir, mode)
for file in [os.path.join(root, f) for f in files]:
os.chmod(file, mode)
def install_sdk_tools():
import os
import tarfile
import zipfile
import hashlib
if sys.version_info[0] >= 3:
from urllib.request import urlretrieve
else:
from urllib import urlretrieve
if not os.path.isdir(prerequisite_tools_dir):
os.makedirs(prerequisite_tools_dir)
zip_fullfn = prerequisite_tools_dir + os.path.sep + 'sdk-tools.zip';
if sys.platform == 'win32':
url = 'https://dl.google.com/android/repository/commandlinetools-win-' + ANDROID_SDK_TOOLS_VERSION + '_latest.zip'
expected_shasum = ANDROID_SDK_TOOLS_SHASUM_WINDOWS
else:
url = 'https://dl.google.com/android/repository/commandlinetools-linux-' + ANDROID_SDK_TOOLS_VERSION + '_latest.zip'
expected_shasum = ANDROID_SDK_TOOLS_SHASUM_LINUX
# Download sdk-tools.
url_base_name = os.path.basename(url)
if not os.path.isfile(zip_fullfn):
print('Downloading sdk-tools to:', zip_fullfn)
zip_fullfn = urlretrieve(url, zip_fullfn)[0]
print('Downloaded sdk-tools to:', zip_fullfn)
# Verify SHA-1 checksum of downloaded files.
with open(zip_fullfn, 'rb') as f:
contents = f.read()
found_shasum = hashlib.sha256(contents).hexdigest()
print("SHA-256:", zip_fullfn, "%s" % found_shasum)
if found_shasum != expected_shasum:
fail('Error: SHA-256 checksum ' + found_shasum + ' of downloaded file does not match expected checksum ' + expected_shasum)
print("[ok] Checksum of", zip_fullfn, "matches expected value.")
# Proceed with extraction of the NDK if necessary.
sdk_tools_path = prerequisite_tools_dir + os.path.sep + 'cmdline-tools'
if not os.path.isfile(sdk_tools_path + os.path.sep + "source.properties"):
print("Extracting sdk-tools ...")
# This will go to a subfolder "tools" in the current path.
file_name, file_extension = os.path.splitext(url_base_name)
zip = zipfile.ZipFile(zip_fullfn, 'r')
zip.extractall(prerequisite_tools_dir)
zip.close()
# Move contents of cmdline-tools one level deeper into cmdline-tools/latest
sdk_tools_latest_path = sdk_tools_path + os.path.sep + 'latest'
if os.path.isdir(sdk_tools_latest_path):
shutil.rmtree(sdk_tools_latest_path)
os.makedirs(sdk_tools_latest_path)
shutil.move(sdk_tools_path + os.path.sep + 'NOTICE.txt', sdk_tools_latest_path)
shutil.move(sdk_tools_path + os.path.sep + 'source.properties', sdk_tools_latest_path)
shutil.move(sdk_tools_path + os.path.sep + 'bin', sdk_tools_latest_path)
shutil.move(sdk_tools_path + os.path.sep + 'lib', sdk_tools_latest_path)
# Linux only - Set executable permission on files.
if platform.system() == 'Linux':
print("Setting permissions on sdk-tools executables ...")
change_permissions_recursive(sdk_tools_path, 0o755);
# Add tools/bin to PATH.
sdk_tools_bin_path = sdk_tools_latest_path + os.path.sep + 'bin'
print('Adding to PATH:', sdk_tools_bin_path)
os.environ["PATH"] += os.pathsep + sdk_tools_bin_path
os.environ["ANDROID_HOME"] = os.path.realpath(prerequisite_tools_dir)
os.environ["ANDROID_SDK_ROOT"] = os.path.realpath(prerequisite_tools_dir)
#
# SCRIPT MAIN.
#
if platform.system() not in SUPPORTED_PYTHON_PLATFORMS:
fail('Unsupported python platform %s. Supported platforms: %s', platform.system(),
', '.join(SUPPORTED_PYTHON_PLATFORMS))
if not sys.platform == 'win32':
fail ('Script is currently supported to run under Windows only.')
prerequisite_tools_dir = os.path.dirname(os.path.realpath(__file__)) + os.path.sep + ".." + os.path.sep + "syncthing-android-prereq"
# Check if "sdk-manager" of sdk-tools package is available.
sdk_manager_bin = which("sdkmanager")
if not sdk_manager_bin:
print('Warning: sdkmanager from sdk-tools package is not available on PATH.')
install_sdk_tools();
# Retry.
sdk_manager_bin = which("sdkmanager")
if not sdk_manager_bin:
fail('Error: sdkmanager from sdk-tools package is not available on PATH.')
print('sdk_manager_bin=\'' + sdk_manager_bin + '\'')
# Windows only - Auto accept all sdkmanager licenses.
if sys.platform == 'win32':
subprocess.check_call([sdk_manager_bin, '--update'])
powershell_bin = which('powershell')
subprocess.check_call([powershell_bin, 'for($i=0;$i -lt ' + ANDROID_SDK_VERSION + ';$i++) { $response += \"y`n\"}; $response | sdkmanager --licenses'], stdout=subprocess.DEVNULL)
subprocess.check_call([sdk_manager_bin, 'platforms;android-' + ANDROID_SDK_VERSION])
subprocess.check_call([sdk_manager_bin, 'build-tools;' + ANDROID_SDK_VERSION + '.0.0'])
print('Done.')