Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions | Variables
android_finder Namespace Reference

Functions

 BuildOptions ()
 
 ProcessOptions (options)
 
 ParseAndroidListSdkResult (text)
 
 AndroidListSdk ()
 
 AndroidSdkFindPackage (packages, key)
 
 EnsureSdkPackageInstalled (packages, key)
 
 SdkPackagesForAbi (abi)
 
 TargetForAbi (abi)
 
 EnsureAndroidSdkPackagesInstalled (abi)
 
 ParseAndroidListAvdResult (text)
 
 AndroidListAvd ()
 
 FindAvd (avds, key)
 
 CreateAvd (avdName, abi)
 
 AvdExists (avdName)
 
 EnsureAvdExists (avdName, abi)
 
 StartEmulator (abi, avdName, pollFn)
 
 ParseAndroidDevices (text)
 
 GetAndroidDevices ()
 
 FilterOfflineDevices (devices)
 
 GetOnlineAndroidDevices ()
 
 GetAndroidDeviceProperty (device, property)
 
 GetAndroidDeviceAbis (device)
 
 FindAndroidRunning (abi)
 
 AddSdkToolsToPath ()
 
 FindAndroid (abi, bootstrap)
 
 Main ()
 

Variables

bool DEBUG = False
 
bool VERBOSE = False
 

Detailed Description

Find an Android device with a given ABI.

The name of the Android device is printed to stdout.

Optionally configure and launch an emulator if there's no existing device for a
given ABI. Will download and install Android SDK components as needed.

Function Documentation

◆ AddSdkToolsToPath()

android_finder.AddSdkToolsToPath ( )

Definition at line 312 of file android_finder.py.

312def AddSdkToolsToPath():
313 script_dir = os.path.dirname(sys.argv[0])
314 dart_root = os.path.realpath(os.path.join(script_dir, '..', '..'))
315 third_party_root = os.path.join(dart_root, 'third_party')
316 android_tools = os.path.join(third_party_root, 'android_tools')
317 android_sdk_root = os.path.join(android_tools, 'sdk')
318 android_sdk_tools = os.path.join(android_sdk_root, 'tools')
319 android_sdk_platform_tools = os.path.join(android_sdk_root,
320 'platform-tools')
321 os.environ['PATH'] = ':'.join(
322 [os.environ['PATH'], android_sdk_tools, android_sdk_platform_tools])
323 # Remove any environment variables that would affect our build.
324 for i in [
325 'ANDROID_NDK_ROOT', 'ANDROID_SDK_ROOT', 'ANDROID_TOOLCHAIN', 'AR',
326 'BUILDTYPE', 'CC', 'CXX', 'GYP_DEFINES', 'LD_LIBRARY_PATH', 'LINK',
327 'MAKEFLAGS', 'MAKELEVEL', 'MAKEOVERRIDES', 'MFLAGS', 'NM'
328 ]:
329 if i in os.environ:
330 del os.environ[i]
331
332

◆ AndroidListAvd()

android_finder.AndroidListAvd ( )
Returns a list of available Android Virtual Devices.

Definition at line 194 of file android_finder.py.

194def AndroidListAvd():
195 """Returns a list of available Android Virtual Devices."""
196 return ParseAndroidListAvdResult(
197 utils.RunCommand(["android", "list", "avd"]))
198
199
RunCommand(command, input=None, pollFn=None, outStream=None, errStream=None, killOnEarlyReturn=True, verbose=False, debug=False, printErrorInfo=False)
Definition utils.py:160

◆ AndroidListSdk()

android_finder.AndroidListSdk ( )

Definition at line 90 of file android_finder.py.

90def AndroidListSdk():
91 return ParseAndroidListSdkResult(
92 utils.RunCommand(["android", "list", "sdk", "-a", "-e"]))
93
94

◆ AndroidSdkFindPackage()

android_finder.AndroidSdkFindPackage (   packages,
  key 
)
Args:
packages: list of (id-num, id-key, type, description).
key: (id-key, type, description-prefix).

Definition at line 95 of file android_finder.py.

95def AndroidSdkFindPackage(packages, key):
96 """
97 Args:
98 packages: list of (id-num, id-key, type, description).
99 key: (id-key, type, description-prefix).
100 """
101 (key_id, key_type, key_description_prefix) = key
102 for package in packages:
103 (package_num, package_id, package_type, package_description) = package
104 if (package_id == key_id and package_type == key_type and
105 package_description.startswith(key_description_prefix)):
106 return package
107 return None
108
109

◆ AvdExists()

android_finder.AvdExists (   avdName)

Definition at line 219 of file android_finder.py.

219def AvdExists(avdName):
220 avdList = AndroidListAvd()
221 return FindAvd(avdList, avdName) is not None
222
223

◆ BuildOptions()

android_finder.BuildOptions ( )

Definition at line 26 of file android_finder.py.

26def BuildOptions():
27 result = optparse.OptionParser()
28 result.add_option(
29 "-a",
30 "--abi",
31 action="store",
32 type="string",
33 help="Desired ABI. armeabi-v7a or x86.")
34 result.add_option(
35 "-b",
36 "--bootstrap",
37 help=
38 'Bootstrap - create an emulator, installing SDK packages if needed.',
39 default=False,
40 action="store_true")
41 result.add_option(
42 "-d",
43 "--debug",
44 help='Turn on debugging diagnostics.',
45 default=False,
46 action="store_true")
47 result.add_option(
48 "-v",
49 "--verbose",
50 help='Verbose output.',
51 default=False,
52 action="store_true")
53 return result
54
55

◆ CreateAvd()

android_finder.CreateAvd (   avdName,
  abi 
)

Definition at line 207 of file android_finder.py.

207def CreateAvd(avdName, abi):
208 out = utils.RunCommand([
209 "android", "create", "avd", "--name", avdName, "--target",
210 TargetForAbi(abi), '--abi', abi
211 ],
212 input="no\n")
213 if out.find('Created AVD ') < 0:
214 if VERBOSE:
215 sys.stderr.write('Could not create AVD:\n%s\n' % out)
216 raise utils.Error('Could not create AVD')
217
218

◆ EnsureAndroidSdkPackagesInstalled()

android_finder.EnsureAndroidSdkPackagesInstalled (   abi)
Return true if at least one package was not already installed.

Definition at line 157 of file android_finder.py.

157def EnsureAndroidSdkPackagesInstalled(abi):
158 """Return true if at least one package was not already installed."""
159 abiPackageList = SdkPackagesForAbi(abi)
160 installedSomething = False
161 packages = AndroidListSdk()
162 for package in abiPackageList:
163 installedSomething |= EnsureSdkPackageInstalled(packages, package)
164 return installedSomething
165
166

◆ EnsureAvdExists()

android_finder.EnsureAvdExists (   avdName,
  abi 
)

Definition at line 224 of file android_finder.py.

224def EnsureAvdExists(avdName, abi):
225 if AvdExists(avdName):
226 return
227 if VERBOSE:
228 sys.stderr.write('Checking SDK packages...\n')
229 if EnsureAndroidSdkPackagesInstalled(abi):
230 # Installing a new package could have made a previously invalid AVD valid
231 if AvdExists(avdName):
232 return
233 CreateAvd(avdName, abi)
234
235

◆ EnsureSdkPackageInstalled()

android_finder.EnsureSdkPackageInstalled (   packages,
  key 
)
Makes sure the package with a given key is installed.

key is (id-key, type, description-prefix)

Returns True if the package was not already installed.

Definition at line 110 of file android_finder.py.

110def EnsureSdkPackageInstalled(packages, key):
111 """
112 Makes sure the package with a given key is installed.
113
114 key is (id-key, type, description-prefix)
115
116 Returns True if the package was not already installed.
117 """
118 entry = AndroidSdkFindPackage(packages, key)
119 if entry is None:
120 raise utils.Error("Could not find a package for key %s" % key)
121 packageId = entry[0]
122 if VERBOSE:
123 sys.stderr.write('Checking Android SDK package %s...\n' % str(entry))
124 out = utils.RunCommand(
125 ["android", "update", "sdk", "-a", "-u", "--filter",
126 str(packageId)])
127 return '\nInstalling Archives:\n' in out
128
129

◆ FilterOfflineDevices()

android_finder.FilterOfflineDevices (   devices)

Definition at line 278 of file android_finder.py.

278def FilterOfflineDevices(devices):
279 online = {}
280 for device in devices.keys():
281 status = devices[device]
282 if status != 'offline':
283 online[device] = status
284 return online
285
286

◆ FindAndroid()

android_finder.FindAndroid (   abi,
  bootstrap 
)

Definition at line 333 of file android_finder.py.

333def FindAndroid(abi, bootstrap):
334 if VERBOSE:
335 sys.stderr.write(
336 'Looking for an Android device running abi %s...\n' % abi)
337 AddSdkToolsToPath()
338 device = FindAndroidRunning(abi)
339 if not device:
340 if bootstrap:
341 if VERBOSE:
342 sys.stderr.write("No emulator found, try to create one.\n")
343 avdName = 'dart-build-%s' % abi
344 EnsureAvdExists(avdName, abi)
345
346 # It takes a while to start up an emulator.
347 # Provide feedback while we wait.
348 pollResult = [None]
349
350 def pollFunction():
351 if VERBOSE:
352 sys.stderr.write('.')
353 pollResult[0] = FindAndroidRunning(abi)
354 # Stop polling once we have our result.
355 return pollResult[0] != None
356
357 StartEmulator(abi, avdName, pollFunction)
358 device = pollResult[0]
359 return device
360
361

◆ FindAndroidRunning()

android_finder.FindAndroidRunning (   abi)

Definition at line 305 of file android_finder.py.

305def FindAndroidRunning(abi):
306 for device in GetOnlineAndroidDevices().keys():
307 if abi in GetAndroidDeviceAbis(device):
308 return device
309 return None
310
311

◆ FindAvd()

android_finder.FindAvd (   avds,
  key 
)

Definition at line 200 of file android_finder.py.

200def FindAvd(avds, key):
201 for avd in avds:
202 if avd['Name'] == key:
203 return avd
204 return None
205
206

◆ GetAndroidDeviceAbis()

android_finder.GetAndroidDeviceAbis (   device)

Definition at line 296 of file android_finder.py.

296def GetAndroidDeviceAbis(device):
297 abis = []
298 for property in ['ro.product.cpu.abi', 'ro.product.cpu.abi2']:
299 out = GetAndroidDeviceProperty(device, property)
300 if len(out) > 0:
301 abis.append(out)
302 return abis
303
304

◆ GetAndroidDeviceProperty()

android_finder.GetAndroidDeviceProperty (   device,
  property 
)

Definition at line 291 of file android_finder.py.

291def GetAndroidDeviceProperty(device, property):
292 return utils.RunCommand(["adb", "-s", device, "shell", "getprop",
293 property]).strip()
294
295

◆ GetAndroidDevices()

android_finder.GetAndroidDevices ( )

Definition at line 274 of file android_finder.py.

274def GetAndroidDevices():
275 return ParseAndroidDevices(utils.RunCommand(["adb", "devices"]))
276
277

◆ GetOnlineAndroidDevices()

android_finder.GetOnlineAndroidDevices ( )

Definition at line 287 of file android_finder.py.

287def GetOnlineAndroidDevices():
288 return FilterOfflineDevices(GetAndroidDevices())
289
290

◆ Main()

android_finder.Main ( )

Definition at line 362 of file android_finder.py.

362def Main():
363 # Parse options.
364 parser = BuildOptions()
365 (options, args) = parser.parse_args()
366 if not ProcessOptions(options):
367 parser.print_help()
368 return 1
369
370 # If there are additional arguments, report error and exit.
371 if args:
372 parser.print_help()
373 return 1
374
375 try:
376 device = FindAndroid(options.abi, options.bootstrap)
377 if device != None:
378 sys.stdout.write("%s\n" % device)
379 return 0
380 else:
381 if VERBOSE:
382 sys.stderr.write('Could not find device\n')
383 return 2
384 except utils.Error as e:
385 sys.stderr.write("error: %s\n" % e)
386 if DEBUG:
387 traceback.print_exc(file=sys.stderr)
388 return -1
389
390

◆ ParseAndroidDevices()

android_finder.ParseAndroidDevices (   text)
Return Dictionary [name] -> status

Definition at line 262 of file android_finder.py.

262def ParseAndroidDevices(text):
263 """Return Dictionary [name] -> status"""
264 text = text.split('List of devices attached')[-1]
265 lines = [line.strip() for line in text.split('\n')]
266 lines = [line for line in lines if len(line) > 0]
267 devices = {}
268 for line in lines:
269 lineItems = line.split('\t')
270 devices[lineItems[0]] = lineItems[1]
271 return devices
272
273

◆ ParseAndroidListAvdResult()

android_finder.ParseAndroidListAvdResult (   text)
Parse the output of an 'android list avd' command.
Return List of {Name: Path: Target: ABI: Skin: Sdcard:}

Definition at line 167 of file android_finder.py.

167def ParseAndroidListAvdResult(text):
168 """
169 Parse the output of an 'android list avd' command.
170 Return List of {Name: Path: Target: ABI: Skin: Sdcard:}
171 """
172 text = text.split('Available Android Virtual Devices:\n')[-1]
173 text = text.split(
174 'The following Android Virtual Devices could not be loaded:\n')[0]
175 result = []
176 line_re = re.compile(r'^\s*([^\:]+)\:\s*(.*)$')
177 for chunk in text.split('\n---------\n'):
178 entry = {}
179 for line in chunk.split('\n'):
180 line = line.strip()
181 if len(line) == 0:
182 continue
183 match = line_re.match(line)
184 if match is None:
185 sys.stderr.write('Match fail %s\n' % str(line))
186 continue
187 #raise utils.Error('Match failed')
188 entry[match.group(1)] = match.group(2)
189 if len(entry) > 0:
190 result.append(entry)
191 return result
192
193

◆ ParseAndroidListSdkResult()

android_finder.ParseAndroidListSdkResult (   text)
Parse the output of an 'android list sdk' command.

Return list of (id-num, id-key, type, description).

Definition at line 67 of file android_finder.py.

67def ParseAndroidListSdkResult(text):
68 """
69 Parse the output of an 'android list sdk' command.
70
71 Return list of (id-num, id-key, type, description).
72 """
73 header_regex = re.compile(
74 r'Packages available for installation or update: \d+\n')
75 packages = re.split(header_regex, text)
76 if len(packages) != 2:
77 raise utils.Error("Could not get a list of packages to install")
78 entry_regex = re.compile(
79 r'^id\: (\d+) or "([^"]*)"\n\s*Type\: ([^\n]*)\n\s*Desc\: (.*)')
80 entries = []
81 for entry in packages[1].split('----------\n'):
82 match = entry_regex.match(entry)
83 if match == None:
84 continue
85 entries.append((int(match.group(1)), match.group(2), match.group(3),
86 match.group(4)))
87 return entries
88
89
Type::kYUV Type::kRGBA() int(0.7 *637)

◆ ProcessOptions()

android_finder.ProcessOptions (   options)

Definition at line 56 of file android_finder.py.

56def ProcessOptions(options):
57 global DEBUG
58 DEBUG = options.debug
59 global VERBOSE
60 VERBOSE = options.verbose
61 if options.abi is None:
62 sys.stderr.write('--abi not specified.\n')
63 return False
64 return True
65
66

◆ SdkPackagesForAbi()

android_finder.SdkPackagesForAbi (   abi)

Definition at line 130 of file android_finder.py.

130def SdkPackagesForAbi(abi):
131 packagesForAbi = {
132 'armeabi-v7a': [
133 # The platform needed to install the armeabi ABI system image:
134 ('android-15', 'Platform', 'Android SDK Platform 4.0.3'),
135 # The armeabi-v7a ABI system image:
136 ('sysimg-15', 'SystemImage', 'Android SDK Platform 4.0.3')
137 ],
138 'x86': [
139 # The platform needed to install the x86 ABI system image:
140 ('android-15', 'Platform', 'Android SDK Platform 4.0.3'),
141 # The x86 ABI system image:
142 ('sysimg-15', 'SystemImage', 'Android SDK Platform 4.0.4')
143 ]
144 }
145
146 if abi not in packagesForAbi:
147 raise utils.Error('Unsupported abi %s' % abi)
148 return packagesForAbi[abi]
149
150

◆ StartEmulator()

android_finder.StartEmulator (   abi,
  avdName,
  pollFn 
)
Start an emulator for a given abi and svdName.

Echo the emulator's stderr and stdout output to our stderr.

Call pollFn repeatedly until it returns False. Leave the emulator running
when we return.

Implementation note: Normally we would call the 'emulator' binary, which
is a wrapper that launches the appropriate abi-specific emulator. But there
is a bug that causes the emulator to exit immediately with a result code of
-11 if run from a ssh shell or a No Machine shell. (And only if called from
three levels of nested python scripts.) Calling the ABI-specific versions
of the emulator directly works around this bug.

Definition at line 236 of file android_finder.py.

236def StartEmulator(abi, avdName, pollFn):
237 """
238 Start an emulator for a given abi and svdName.
239
240 Echo the emulator's stderr and stdout output to our stderr.
241
242 Call pollFn repeatedly until it returns False. Leave the emulator running
243 when we return.
244
245 Implementation note: Normally we would call the 'emulator' binary, which
246 is a wrapper that launches the appropriate abi-specific emulator. But there
247 is a bug that causes the emulator to exit immediately with a result code of
248 -11 if run from a ssh shell or a No Machine shell. (And only if called from
249 three levels of nested python scripts.) Calling the ABI-specific versions
250 of the emulator directly works around this bug.
251 """
252 emulatorName = {'x86': 'emulator-x86', 'armeabi-v7a': 'emulator-arm'}[abi]
253 command = [emulatorName, '-avd', avdName, '-no-boot-anim', '-no-window']
255 command,
256 pollFn=pollFn,
257 killOnEarlyReturn=False,
258 outStream=sys.stderr,
259 errStream=sys.stderr)
260
261

◆ TargetForAbi()

android_finder.TargetForAbi (   abi)

Definition at line 151 of file android_finder.py.

151def TargetForAbi(abi):
152 for package in SdkPackagesForAbi(abi):
153 if package[1] == 'Platform':
154 return package[0]
155
156

Variable Documentation

◆ DEBUG

bool android_finder.DEBUG = False

Definition at line 22 of file android_finder.py.

◆ VERBOSE

bool android_finder.VERBOSE = False

Definition at line 23 of file android_finder.py.