Skip to content

Commit 165036c

Browse files
committed
Added improved SDK dir and api check
1 parent 327df86 commit 165036c

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

pythonforandroid/toolchain.py

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
from colorama import Style, Fore
4646

47+
DEFAULT_ANDROID_API = 14
48+
4749

4850
logger = logging.getLogger('p4a')
4951
# logger.setLevel(logging.DEBUG)
@@ -126,7 +128,10 @@ def require_prebuilt_dist(func):
126128
@wraps(func)
127129
def wrapper_func(self, args):
128130
ctx = self.ctx
129-
ctx.prepare_build_environment()
131+
ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir,
132+
user_ndk_dir=self.ndk_dir,
133+
user_android_api=self.android_api,
134+
user_ndk_version=self.ndk_version)
130135
dist = self._dist
131136
if dist.needs_build:
132137
info('No dist exists that meets your requirements, so one will '
@@ -537,7 +542,8 @@ def ndk_dir(self):
537542
def ndk_dir(self, value):
538543
self._ndk_dir = value
539544

540-
def prepare_build_environment(self):
545+
def prepare_build_environment(self, user_sdk_dir, user_ndk_dir,
546+
user_android_api, user_ndk_version):
541547
'''Checks that build dependencies exist and sets internal variables
542548
for the Android SDK etc.
543549
@@ -549,10 +555,52 @@ def prepare_build_environment(self):
549555

550556
ok = True
551557

558+
# Work out where the Android SDK is
559+
sdk_dir = None
560+
if user_sdk_dir:
561+
sdk_dir = user_sdk_dir
562+
if sdk_dir is None:
563+
sdk_dir = environ.get('ANDROIDSDK', None)
564+
if sdk_dir is None:
565+
sdk_dir = environ.get('ANDROID_HOME', None)
566+
if sdk_dir is None:
567+
warning('Android SDK dir was not specified, exiting.')
568+
exit(1)
569+
570+
# Check what Android API we're using, and install platform
571+
# tools if necessary
572+
android_api = None
573+
if user_android_api:
574+
android_api = user_android_api
575+
if android_api is None:
576+
android_api = environ.get('ANDROIDAPI', None)
577+
if android_api is None:
578+
info('Android API target was not set manually, using '
579+
'the default of {}'.format(DEFAULT_ANDROID_API))
580+
android_api = DEFAULT_ANDROID_API
581+
android_api = int(android_api)
582+
583+
android = sh.Command(join(sdk_dir, 'tools', 'android'))
584+
targets = android('list').stdout.split('\n')
585+
apis = [s for s in targets if re.match(r'^ *API level: ', s)]
586+
apis = [int(re.findall(r'[0-9]+', s)[0]) for s in apis]
587+
info('Available Android APIs are ({})'.format(', '.join(map(str, apis))))
588+
if android_api in apis:
589+
info('Requested API target {} is available, continuing.'.format(android_api))
590+
else:
591+
warning('Requested API target {} is not available, exiting.'.format(android_api))
592+
exit(1)
593+
594+
exit(1)
595+
596+
597+
598+
599+
self.sdk_dir = environ.get('ANDROIDSDK', None)
600+
552601
# AND: We should check for ndk-build and ant?
553602
self.android_api = environ.get('ANDROIDAPI', '14')
554603
self.ndk_ver = environ.get('ANDROIDNDKVER', 'r9')
555-
self.sdk_dir = environ.get('ANDROIDSDK', None)
556604
if self.sdk_dir is None:
557605
ok = False
558606
self.ndk_dir = environ.get('ANDROIDNDK', None)
@@ -1955,7 +2003,7 @@ def __init__(self):
19552003
self._ctx = None
19562004

19572005
parser = argparse.ArgumentParser(
1958-
description="Tool for managing the iOS / Python toolchain",
2006+
description="Tool for managing the Android / Python toolchain",
19592007
usage="""toolchain <command> [<args>]
19602008
19612009
Currently available commands:
@@ -1977,8 +2025,20 @@ def __init__(self):
19772025
clean_dists
19782026
""")
19792027
parser.add_argument("command", help="Command to run")
2028+
2029+
# General options
19802030
parser.add_argument('--debug', dest='debug', action='store_true',
19812031
help='Display debug output and all build info')
2032+
parser.add_argument('--sdk_dir', dest='sdk_dir', default='',
2033+
help='The filepath where the Android SDK is installed')
2034+
parser.add_argument('--ndk_dir', dest='ndk_dir', default='',
2035+
help='The filepath where the Android NDK is installed')
2036+
parser.add_argument('--android_api', dest='android_api', default=0, type=int,
2037+
help='The Android API level to build against.')
2038+
parser.add_argument('--ndk_version', dest='ndk_version', default='',
2039+
help=('The version of the Android NDK. This is optional, '
2040+
'we try to work it out automatically from the ndk_dir.'))
2041+
19822042

19832043
# Options for specifying the Distribution
19842044
parser.add_argument(
@@ -2011,6 +2071,10 @@ def __init__(self):
20112071

20122072
if args.debug:
20132073
logger.setLevel(logging.DEBUG)
2074+
self.sdk_dir = args.sdk_dir
2075+
self.ndk_dir = args.ndk_dir
2076+
self.android_api = args.android_api
2077+
self.ndk_version = args.ndk_version
20142078

20152079
# import ipdb
20162080
# ipdb.set_trace()

0 commit comments

Comments
 (0)