• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kivy / python-for-android / 4398018833

pending completion
4398018833

push

github

GitHub
android_api to integer (#2765)

906 of 2275 branches covered (39.82%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

4686 of 7432 relevant lines covered (63.05%)

2.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.26
/pythonforandroid/bootstraps/common/build/build.py
1
#!/usr/bin/env python3
2

3
from gzip import GzipFile
4✔
4
import hashlib
4✔
5
import json
4✔
6
from os.path import (
4✔
7
    dirname, join, isfile, realpath,
8
    relpath, split, exists, basename
9
)
10
from os import environ, listdir, makedirs, remove
4✔
11
import os
4✔
12
import shlex
4✔
13
import shutil
4✔
14
import subprocess
4✔
15
import sys
4✔
16
import tarfile
4✔
17
import tempfile
4✔
18
import time
4✔
19

20
from distutils.version import LooseVersion
4✔
21
from fnmatch import fnmatch
4✔
22
import jinja2
4✔
23

24

25
def get_dist_info_for(key, error_if_missing=True):
4✔
26
    try:
×
27
        with open(join(dirname(__file__), 'dist_info.json'), 'r') as fileh:
×
28
            info = json.load(fileh)
×
29
        value = info[key]
×
30
    except (OSError, KeyError) as e:
×
31
        if not error_if_missing:
×
32
            return None
×
33
        print("BUILD FAILURE: Couldn't extract the key `" + key + "` " +
×
34
              "from dist_info.json: " + str(e))
35
        sys.exit(1)
×
36
    return value
×
37

38

39
def get_hostpython():
4✔
40
    return get_dist_info_for('hostpython')
×
41

42

43
def get_bootstrap_name():
4✔
44
    return get_dist_info_for('bootstrap')
×
45

46

47
if os.name == 'nt':
4!
48
    ANDROID = 'android.bat'
×
49
    ANT = 'ant.bat'
×
50
else:
51
    ANDROID = 'android'
4✔
52
    ANT = 'ant'
4✔
53

54
curdir = dirname(__file__)
4✔
55

56
BLACKLIST_PATTERNS = [
4✔
57
    # code versionning
58
    '^*.hg/*',
59
    '^*.git/*',
60
    '^*.bzr/*',
61
    '^*.svn/*',
62

63
    # temp files
64
    '~',
65
    '*.bak',
66
    '*.swp',
67

68
    # Android artifacts
69
    '*.apk',
70
    '*.aab',
71
]
72

73
WHITELIST_PATTERNS = []
4✔
74

75
if os.environ.get("P4A_BUILD_IS_RUNNING_UNITTESTS", "0") != "1":
4!
76
    PYTHON = get_hostpython()
×
77
    _bootstrap_name = get_bootstrap_name()
×
78
else:
79
    PYTHON = "python3"
4✔
80
    _bootstrap_name = "sdl2"
4✔
81

82
if PYTHON is not None and not exists(PYTHON):
4!
83
    PYTHON = None
4✔
84

85
if _bootstrap_name in ('sdl2', 'webview', 'service_only'):
4!
86
    WHITELIST_PATTERNS.append('pyconfig.h')
4✔
87

88
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(
4✔
89
    join(curdir, 'templates')))
90

91

92
DEFAULT_PYTHON_ACTIVITY_JAVA_CLASS = 'org.kivy.android.PythonActivity'
4✔
93
DEFAULT_PYTHON_SERVICE_JAVA_CLASS = 'org.kivy.android.PythonService'
4✔
94

95

96
def ensure_dir(path):
4✔
97
    if not exists(path):
×
98
        makedirs(path)
×
99

100

101
def render(template, dest, **kwargs):
4✔
102
    '''Using jinja2, render `template` to the filename `dest`, supplying the
103

104
    keyword arguments as template parameters.
105
    '''
106

107
    dest_dir = dirname(dest)
×
108
    if dest_dir and not exists(dest_dir):
×
109
        makedirs(dest_dir)
×
110

111
    template = environment.get_template(template)
×
112
    text = template.render(**kwargs)
×
113

114
    f = open(dest, 'wb')
×
115
    f.write(text.encode('utf-8'))
×
116
    f.close()
×
117

118

119
def is_whitelist(name):
4✔
120
    return match_filename(WHITELIST_PATTERNS, name)
×
121

122

123
def is_blacklist(name):
4✔
124
    if is_whitelist(name):
×
125
        return False
×
126
    return match_filename(BLACKLIST_PATTERNS, name)
×
127

128

129
def match_filename(pattern_list, name):
4✔
130
    for pattern in pattern_list:
×
131
        if pattern.startswith('^'):
×
132
            pattern = pattern[1:]
×
133
        else:
134
            pattern = '*/' + pattern
×
135
        if fnmatch(name, pattern):
×
136
            return True
×
137

138

139
def listfiles(d):
4✔
140
    basedir = d
×
141
    subdirlist = []
×
142
    for item in os.listdir(d):
×
143
        fn = join(d, item)
×
144
        if isfile(fn):
×
145
            yield fn
×
146
        else:
147
            subdirlist.append(join(basedir, item))
×
148
    for subdir in subdirlist:
×
149
        for fn in listfiles(subdir):
×
150
            yield fn
×
151

152

153
def make_tar(tfn, source_dirs, byte_compile_python=False, optimize_python=True):
4✔
154
    '''
155
    Make a zip file `fn` from the contents of source_dis.
156
    '''
157

158
    def clean(tinfo):
×
159
        """cleaning function (for reproducible builds)"""
160
        tinfo.uid = tinfo.gid = 0
×
161
        tinfo.uname = tinfo.gname = ''
×
162
        tinfo.mtime = 0
×
163
        return tinfo
×
164

165
    # get the files and relpath file of all the directory we asked for
166
    files = []
×
167
    for sd in source_dirs:
×
168
        sd = realpath(sd)
×
169
        for fn in listfiles(sd):
×
170
            if is_blacklist(fn):
×
171
                continue
×
172
            if fn.endswith('.py') and byte_compile_python:
×
173
                fn = compile_py_file(fn, optimize_python=optimize_python)
×
174
            files.append((fn, relpath(realpath(fn), sd)))
×
175
    files.sort()  # deterministic
×
176

177
    # create tar.gz of thoses files
178
    gf = GzipFile(tfn, 'wb', mtime=0)  # deterministic
×
179
    tf = tarfile.open(None, 'w', gf, format=tarfile.USTAR_FORMAT)
×
180
    dirs = []
×
181
    for fn, afn in files:
×
182
        dn = dirname(afn)
×
183
        if dn not in dirs:
×
184
            # create every dirs first if not exist yet
185
            d = ''
×
186
            for component in split(dn):
×
187
                d = join(d, component)
×
188
                if d.startswith('/'):
×
189
                    d = d[1:]
×
190
                if d == '' or d in dirs:
×
191
                    continue
×
192
                dirs.append(d)
×
193
                tinfo = tarfile.TarInfo(d)
×
194
                tinfo.type = tarfile.DIRTYPE
×
195
                clean(tinfo)
×
196
                tf.addfile(tinfo)
×
197

198
        # put the file
199
        tf.add(fn, afn, filter=clean)
×
200
    tf.close()
×
201
    gf.close()
×
202

203

204
def compile_py_file(python_file, optimize_python=True):
4✔
205
    '''
206
    Compile python_file to *.pyc and return the filename of the *.pyc file.
207
    '''
208

209
    if PYTHON is None:
×
210
        return
×
211

212
    args = [PYTHON, '-m', 'compileall', '-b', '-f', python_file]
×
213
    if optimize_python:
×
214
        # -OO = strip docstrings
215
        args.insert(1, '-OO')
×
216
    return_code = subprocess.call(args)
×
217

218
    if return_code != 0:
×
219
        print('Error while running "{}"'.format(' '.join(args)))
×
220
        print('This probably means one of your Python files has a syntax '
×
221
              'error, see logs above')
222
        exit(1)
×
223

224
    return ".".join([os.path.splitext(python_file)[0], "pyc"])
×
225

226

227
def make_package(args):
4✔
228
    # If no launcher is specified, require a main.py/main.pyc:
229
    if (get_bootstrap_name() != "sdl" or args.launcher is None) and \
×
230
            get_bootstrap_name() not in ["webview", "service_library"]:
231
        # (webview doesn't need an entrypoint, apparently)
232
        if args.private is None or (
×
233
                not exists(join(realpath(args.private), 'main.py')) and
234
                not exists(join(realpath(args.private), 'main.pyc'))):
235
            print('''BUILD FAILURE: No main.py(c) found in your app directory. This
×
236
file must exist to act as the entry point for you app. If your app is
237
started by a file with a different name, rename it to main.py or add a
238
main.py that loads it.''')
239
            sys.exit(1)
×
240

241
    assets_dir = "src/main/assets"
×
242

243
    # Delete the old assets.
244
    shutil.rmtree(assets_dir, ignore_errors=True)
×
245
    ensure_dir(assets_dir)
×
246

247
    # Add extra environment variable file into tar-able directory:
248
    env_vars_tarpath = tempfile.mkdtemp(prefix="p4a-extra-env-")
×
249
    with open(os.path.join(env_vars_tarpath, "p4a_env_vars.txt"), "w") as f:
×
250
        if hasattr(args, "window"):
×
251
            f.write("P4A_IS_WINDOWED=" + str(args.window) + "\n")
×
252
        if hasattr(args, "sdl_orientation_hint"):
×
253
            f.write("KIVY_ORIENTATION=" + str(args.sdl_orientation_hint) + "\n")
×
254
        f.write("P4A_NUMERIC_VERSION=" + str(args.numeric_version) + "\n")
×
255
        f.write("P4A_MINSDK=" + str(args.min_sdk_version) + "\n")
×
256

257
    # Package up the private data (public not supported).
258
    use_setup_py = get_dist_info_for("use_setup_py",
×
259
                                     error_if_missing=False) is True
260
    private_tar_dirs = [env_vars_tarpath]
×
261
    _temp_dirs_to_clean = []
×
262
    try:
×
263
        if args.private:
×
264
            if not use_setup_py or (
×
265
                    not exists(join(args.private, "setup.py")) and
266
                    not exists(join(args.private, "pyproject.toml"))
267
                    ):
268
                print('No setup.py/pyproject.toml used, copying '
×
269
                      'full private data into .apk.')
270
                private_tar_dirs.append(args.private)
×
271
            else:
272
                print("Copying main.py's ONLY, since other app data is "
×
273
                      "expected in site-packages.")
274
                main_py_only_dir = tempfile.mkdtemp()
×
275
                _temp_dirs_to_clean.append(main_py_only_dir)
×
276

277
                # Check all main.py files we need to copy:
278
                copy_paths = ["main.py", join("service", "main.py")]
×
279
                for copy_path in copy_paths:
×
280
                    variants = [
×
281
                        copy_path,
282
                        copy_path.partition(".")[0] + ".pyc",
283
                    ]
284
                    # Check in all variants with all possible endings:
285
                    for variant in variants:
×
286
                        if exists(join(args.private, variant)):
×
287
                            # Make sure surrounding directly exists:
288
                            dir_path = os.path.dirname(variant)
×
289
                            if (len(dir_path) > 0 and
×
290
                                    not exists(
291
                                        join(main_py_only_dir, dir_path)
292
                                    )):
293
                                os.mkdir(join(main_py_only_dir, dir_path))
×
294
                            # Copy actual file:
295
                            shutil.copyfile(
×
296
                                join(args.private, variant),
297
                                join(main_py_only_dir, variant),
298
                            )
299

300
                # Append directory with all main.py's to result apk paths:
301
                private_tar_dirs.append(main_py_only_dir)
×
302
        if get_bootstrap_name() == "webview":
×
303
            for asset in listdir('webview_includes'):
×
304
                shutil.copy(join('webview_includes', asset), join(assets_dir, asset))
×
305

306
        for asset in args.assets:
×
307
            asset_src, asset_dest = asset.split(":")
×
308
            if isfile(realpath(asset_src)):
×
309
                ensure_dir(dirname(join(assets_dir, asset_dest)))
×
310
                shutil.copy(realpath(asset_src), join(assets_dir, asset_dest))
×
311
            else:
312
                shutil.copytree(realpath(asset_src), join(assets_dir, asset_dest))
×
313

314
        if args.private or args.launcher:
×
315
            for arch in get_dist_info_for("archs"):
×
316
                libs_dir = f"libs/{arch}"
×
317
                make_tar(
×
318
                    join(libs_dir, "libpybundle.so"),
319
                    [f"_python_bundle__{arch}"],
320
                    byte_compile_python=args.byte_compile_python,
321
                    optimize_python=args.optimize_python,
322
                )
323
            make_tar(
×
324
                join(assets_dir, "private.tar"),
325
                private_tar_dirs,
326
                byte_compile_python=args.byte_compile_python,
327
                optimize_python=args.optimize_python,
328
            )
329
    finally:
330
        for directory in _temp_dirs_to_clean:
×
331
            shutil.rmtree(directory)
×
332

333
    # Remove extra env vars tar-able directory:
334
    shutil.rmtree(env_vars_tarpath)
×
335

336
    # Prepare some variables for templating process
337
    res_dir = "src/main/res"
×
338
    res_dir_initial = "src/res_initial"
×
339
    # make res_dir stateless
340
    if exists(res_dir_initial):
×
341
        shutil.rmtree(res_dir, ignore_errors=True)
×
342
        shutil.copytree(res_dir_initial, res_dir)
×
343
    else:
344
        shutil.copytree(res_dir, res_dir_initial)
×
345

346
    # Add user resouces
347
    for resource in args.resources:
×
348
        resource_src, resource_dest = resource.split(":")
×
349
        if isfile(realpath(resource_src)):
×
350
            ensure_dir(dirname(join(res_dir, resource_dest)))
×
351
            shutil.copy(realpath(resource_src), join(res_dir, resource_dest))
×
352
        else:
353
            shutil.copytree(realpath(resource_src),
×
354
                            join(res_dir, resource_dest), dirs_exist_ok=True)
355

356
    default_icon = 'templates/kivy-icon.png'
×
357
    default_presplash = 'templates/kivy-presplash.jpg'
×
358
    shutil.copy(
×
359
        args.icon or default_icon,
360
        join(res_dir, 'mipmap/icon.png')
361
    )
362
    if args.icon_fg and args.icon_bg:
×
363
        shutil.copy(args.icon_fg, join(res_dir, 'mipmap/icon_foreground.png'))
×
364
        shutil.copy(args.icon_bg, join(res_dir, 'mipmap/icon_background.png'))
×
365
        with open(join(res_dir, 'mipmap-anydpi-v26/icon.xml'), "w") as fd:
×
366
            fd.write("""<?xml version="1.0" encoding="utf-8"?>
×
367
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
368
    <background android:drawable="@mipmap/icon_background"/>
369
    <foreground android:drawable="@mipmap/icon_foreground"/>
370
</adaptive-icon>
371
""")
372
    elif args.icon_fg or args.icon_bg:
×
373
        print("WARNING: Received an --icon_fg or an --icon_bg argument, but not both. "
×
374
              "Ignoring.")
375

376
    if get_bootstrap_name() != "service_only":
×
377
        lottie_splashscreen = join(res_dir, 'raw/splashscreen.json')
×
378
        if args.presplash_lottie:
×
379
            shutil.copy(
×
380
                'templates/lottie.xml',
381
                join(res_dir, 'layout/lottie.xml')
382
            )
383
            ensure_dir(join(res_dir, 'raw'))
×
384
            shutil.copy(
×
385
                args.presplash_lottie,
386
                join(res_dir, 'raw/splashscreen.json')
387
            )
388
        else:
389
            if exists(lottie_splashscreen):
×
390
                remove(lottie_splashscreen)
×
391
                remove(join(res_dir, 'layout/lottie.xml'))
×
392

393
            shutil.copy(
×
394
                args.presplash or default_presplash,
395
                join(res_dir, 'drawable/presplash.jpg')
396
            )
397

398
    # If extra Java jars were requested, copy them into the libs directory
399
    jars = []
×
400
    if args.add_jar:
×
401
        for jarname in args.add_jar:
×
402
            if not exists(jarname):
×
403
                print('Requested jar does not exist: {}'.format(jarname))
×
404
                sys.exit(-1)
×
405
            shutil.copy(jarname, 'src/main/libs')
×
406
            jars.append(basename(jarname))
×
407

408
    # If extra aar were requested, copy them into the libs directory
409
    aars = []
×
410
    if args.add_aar:
×
411
        ensure_dir("libs")
×
412
        for aarname in args.add_aar:
×
413
            if not exists(aarname):
×
414
                print('Requested aar does not exists: {}'.format(aarname))
×
415
                sys.exit(-1)
×
416
            shutil.copy(aarname, 'libs')
×
417
            aars.append(basename(aarname).rsplit('.', 1)[0])
×
418

419
    versioned_name = (args.name.replace(' ', '').replace('\'', '') +
×
420
                      '-' + args.version)
421

422
    version_code = 0
×
423
    if not args.numeric_version:
×
424
        """
425
        Set version code in format (10 + minsdk + app_version)
426
        Historically versioning was (arch + minsdk + app_version),
427
        with arch expressed with a single digit from 6 to 9.
428
        Since the multi-arch support, has been changed to 10.
429
        """
430
        min_sdk = args.min_sdk_version
×
431
        for i in args.version.split('.'):
×
432
            version_code *= 100
×
433
            version_code += int(i)
×
434
        args.numeric_version = "{}{}{}".format("10", min_sdk, version_code)
×
435

436
    if args.intent_filters:
×
437
        with open(args.intent_filters) as fd:
×
438
            args.intent_filters = fd.read()
×
439

440
    if not args.add_activity:
×
441
        args.add_activity = []
×
442

443
    if not args.activity_launch_mode:
×
444
        args.activity_launch_mode = ''
×
445

446
    if args.extra_source_dirs:
×
447
        esd = []
×
448
        for spec in args.extra_source_dirs:
×
449
            if ':' in spec:
×
450
                specdir, specincludes = spec.split(':')
×
451
                print('WARNING: Currently gradle builds only support including source '
×
452
                      'directories, so when building using gradle all files in '
453
                      '{} will be included.'.format(specdir))
454
            else:
455
                specdir = spec
×
456
                specincludes = '**'
×
457
            esd.append((realpath(specdir), specincludes))
×
458
        args.extra_source_dirs = esd
×
459
    else:
460
        args.extra_source_dirs = []
×
461

462
    service = False
×
463
    if args.private:
×
464
        service_main = join(realpath(args.private), 'service', 'main.py')
×
465
        if exists(service_main) or exists(service_main + 'o'):
×
466
            service = True
×
467

468
    service_names = []
×
469
    base_service_class = args.service_class_name.split('.')[-1]
×
470
    for sid, spec in enumerate(args.services):
×
471
        spec = spec.split(':')
×
472
        name = spec[0]
×
473
        entrypoint = spec[1]
×
474
        options = spec[2:]
×
475

476
        foreground = 'foreground' in options
×
477
        sticky = 'sticky' in options
×
478

479
        service_names.append(name)
×
480
        service_target_path =\
×
481
            'src/main/java/{}/Service{}.java'.format(
482
                args.package.replace(".", "/"),
483
                name.capitalize()
484
            )
485
        render(
×
486
            'Service.tmpl.java',
487
            service_target_path,
488
            name=name,
489
            entrypoint=entrypoint,
490
            args=args,
491
            foreground=foreground,
492
            sticky=sticky,
493
            service_id=sid + 1,
494
            base_service_class=base_service_class,
495
        )
496

497
    # Find the SDK directory and target API
498
    with open('project.properties', 'r') as fileh:
×
499
        target = fileh.read().strip()
×
500
    android_api = target.split('-')[1]
×
501

NEW
502
    if android_api.isdigit():
×
NEW
503
        android_api = int(android_api)
×
504
    else:
UNCOV
505
        raise ValueError(
×
506
            "failed to extract the Android API level from " +
507
            "build.properties. expected int, got: '" +
508
            str(android_api) + "'"
509
        )
510

511
    with open('local.properties', 'r') as fileh:
×
512
        sdk_dir = fileh.read().strip()
×
513
    sdk_dir = sdk_dir[8:]
×
514

515
    # Try to build with the newest available build tools
516
    ignored = {".DS_Store", ".ds_store"}
×
517
    build_tools_versions = [x for x in listdir(join(sdk_dir, 'build-tools')) if x not in ignored]
×
518
    build_tools_versions = sorted(build_tools_versions,
×
519
                                  key=LooseVersion)
520
    build_tools_version = build_tools_versions[-1]
×
521

522
    # Folder name for launcher (used by SDL2 bootstrap)
523
    url_scheme = 'kivy'
×
524

525
    # Copy backup rules file if specified and update the argument
526
    res_xml_dir = join(res_dir, 'xml')
×
527
    if args.backup_rules:
×
528
        ensure_dir(res_xml_dir)
×
529
        shutil.copy(join(args.private, args.backup_rules), res_xml_dir)
×
530
        args.backup_rules = split(args.backup_rules)[1][:-4]
×
531

532
    # Copy res_xml files to src/main/res/xml
533
    if args.res_xmls:
×
534
        ensure_dir(res_xml_dir)
×
535
        for xmlpath in args.res_xmls:
×
536
            if not os.path.exists(xmlpath):
×
537
                xmlpath = join(args.private, xmlpath)
×
538
            shutil.copy(xmlpath, res_xml_dir)
×
539

540
    # Render out android manifest:
541
    manifest_path = "src/main/AndroidManifest.xml"
×
542
    render_args = {
×
543
        "args": args,
544
        "service": service,
545
        "service_names": service_names,
546
        "android_api": android_api,
547
        "debug": "debug" in args.build_mode,
548
        "native_services": args.native_services
549
    }
550
    if get_bootstrap_name() == "sdl2":
×
551
        render_args["url_scheme"] = url_scheme
×
552
    render(
×
553
        'AndroidManifest.tmpl.xml',
554
        manifest_path,
555
        **render_args)
556

557
    # Copy the AndroidManifest.xml to the dist root dir so that ant
558
    # can also use it
559
    if exists('AndroidManifest.xml'):
×
560
        remove('AndroidManifest.xml')
×
561
    shutil.copy(manifest_path, 'AndroidManifest.xml')
×
562

563
    # gradle build templates
564
    render(
×
565
        'build.tmpl.gradle',
566
        'build.gradle',
567
        args=args,
568
        aars=aars,
569
        jars=jars,
570
        android_api=android_api,
571
        build_tools_version=build_tools_version,
572
        debug_build="debug" in args.build_mode,
573
        is_library=(get_bootstrap_name() == 'service_library'),
574
        )
575

576
    # gradle properties
577
    render(
×
578
        'gradle.tmpl.properties',
579
        'gradle.properties',
580
        args=args)
581

582
    # ant build templates
583
    render(
×
584
        'build.tmpl.xml',
585
        'build.xml',
586
        args=args,
587
        versioned_name=versioned_name)
588

589
    # String resources:
590
    timestamp = time.time()
×
591
    if 'SOURCE_DATE_EPOCH' in environ:
×
592
        # for reproducible builds
593
        timestamp = int(environ['SOURCE_DATE_EPOCH'])
×
594
    private_version = "{} {} {}".format(
×
595
        args.version,
596
        args.numeric_version,
597
        timestamp
598
    )
599
    render_args = {
×
600
        "args": args,
601
        "private_version": hashlib.sha1(private_version.encode()).hexdigest()
602
    }
603
    if get_bootstrap_name() == "sdl2":
×
604
        render_args["url_scheme"] = url_scheme
×
605
    render(
×
606
        'strings.tmpl.xml',
607
        join(res_dir, 'values/strings.xml'),
608
        **render_args)
609

610
    if exists(join("templates", "custom_rules.tmpl.xml")):
×
611
        render(
×
612
            'custom_rules.tmpl.xml',
613
            'custom_rules.xml',
614
            args=args)
615

616
    if get_bootstrap_name() == "webview":
×
617
        render('WebViewLoader.tmpl.java',
×
618
               'src/main/java/org/kivy/android/WebViewLoader.java',
619
               args=args)
620

621
    if args.sign:
×
622
        render('build.properties', 'build.properties')
×
623
    else:
624
        if exists('build.properties'):
×
625
            os.remove('build.properties')
×
626

627
    # Apply java source patches if any are present:
628
    if exists(join('src', 'patches')):
×
629
        print("Applying Java source code patches...")
×
630
        for patch_name in os.listdir(join('src', 'patches')):
×
631
            patch_path = join('src', 'patches', patch_name)
×
632
            print("Applying patch: " + str(patch_path))
×
633

634
            # -N: insist this is FORWARD patch, don't reverse apply
635
            # -p1: strip first path component
636
            # -t: batch mode, don't ask questions
637
            patch_command = ["patch", "-N", "-p1", "-t", "-i", patch_path]
×
638

639
            try:
×
640
                # Use a dry run to establish whether the patch is already applied.
641
                # If we don't check this, the patch may be partially applied (which is bad!)
642
                subprocess.check_output(patch_command + ["--dry-run"])
×
643
            except subprocess.CalledProcessError as e:
×
644
                if e.returncode == 1:
×
645
                    # Return code 1 means not all hunks could be applied, this usually
646
                    # means the patch is already applied.
647
                    print("Warning: failed to apply patch (exit code 1), "
×
648
                          "assuming it is already applied: ",
649
                          str(patch_path))
650
                else:
651
                    raise e
×
652
            else:
653
                # The dry run worked, so do the real thing
654
                subprocess.check_output(patch_command)
×
655

656

657
def parse_permissions(args_permissions):
4✔
658
    if args_permissions and isinstance(args_permissions[0], list):
4!
659
        args_permissions = [p for perm in args_permissions for p in perm]
4✔
660

661
    def _is_advanced_permission(permission):
4✔
662
        return permission.startswith("(") and permission.endswith(")")
4✔
663

664
    def _decode_advanced_permission(permission):
4✔
665
        SUPPORTED_PERMISSION_PROPERTIES = ["name", "maxSdkVersion", "usesPermissionFlags"]
4✔
666
        _permission_args = permission[1:-1].split(";")
4✔
667
        _permission_args = (arg.split("=") for arg in _permission_args)
4✔
668
        advanced_permission = dict(_permission_args)
4✔
669

670
        if "name" not in advanced_permission:
4!
671
            raise ValueError("Advanced permission must have a name property")
×
672

673
        for key in advanced_permission.keys():
4✔
674
            if key not in SUPPORTED_PERMISSION_PROPERTIES:
4✔
675
                raise ValueError(
4✔
676
                    f"Property '{key}' is not supported. "
677
                    "Advanced permission only supports: "
678
                    f"{', '.join(SUPPORTED_PERMISSION_PROPERTIES)} properties"
679
                )
680

681
        return advanced_permission
4✔
682

683
    _permissions = []
4✔
684
    for permission in args_permissions:
4✔
685
        if _is_advanced_permission(permission):
4✔
686
            _permissions.append(_decode_advanced_permission(permission))
4✔
687
        else:
688
            if "." in permission:
4✔
689
                _permissions.append(dict(name=permission))
4✔
690
            else:
691
                _permissions.append(dict(name=f"android.permission.{permission}"))
4✔
692
    return _permissions
4✔
693

694

695
def get_sdl_orientation_hint(orientations):
4✔
696
    SDL_ORIENTATION_MAP = {
4✔
697
        "landscape": "LandscapeLeft",
698
        "portrait": "Portrait",
699
        "portrait-reverse": "PortraitUpsideDown",
700
        "landscape-reverse": "LandscapeRight",
701
    }
702
    return " ".join(
4✔
703
        [SDL_ORIENTATION_MAP[x] for x in orientations if x in SDL_ORIENTATION_MAP]
704
    )
705

706

707
def get_manifest_orientation(orientations, manifest_orientation=None):
4✔
708
    # If the user has specifically set an orientation to use in the manifest,
709
    # use that.
710
    if manifest_orientation is not None:
4✔
711
        return manifest_orientation
4✔
712

713
    # If multiple or no orientations are specified, use unspecified in the manifest,
714
    # as we can only specify one orientation in the manifest.
715
    if len(orientations) != 1:
4✔
716
        return "unspecified"
4✔
717

718
    # Convert the orientation to a value that can be used in the manifest.
719
    # If the specified orientation is not supported, use unspecified.
720
    MANIFEST_ORIENTATION_MAP = {
4✔
721
        "landscape": "landscape",
722
        "portrait": "portrait",
723
        "portrait-reverse": "reversePortrait",
724
        "landscape-reverse": "reverseLandscape",
725
    }
726
    return MANIFEST_ORIENTATION_MAP.get(orientations[0], "unspecified")
4✔
727

728

729
def get_dist_ndk_min_api_level():
4✔
730
    # Get the default minsdk, equal to the NDK API that this dist is built against
731
    try:
4✔
732
        with open('dist_info.json', 'r') as fileh:
4!
733
            info = json.load(fileh)
×
734
            ndk_api = int(info['ndk_api'])
×
735
    except (OSError, KeyError, ValueError, TypeError):
4✔
736
        print('WARNING: Failed to read ndk_api from dist info, defaulting to 12')
4✔
737
        ndk_api = 12  # The old default before ndk_api was introduced
4✔
738
    return ndk_api
4✔
739

740

741
def create_argument_parser():
4✔
742
    ndk_api = get_dist_ndk_min_api_level()
4✔
743
    import argparse
4✔
744
    ap = argparse.ArgumentParser(description='''\
4✔
745
Package a Python application for Android (using
746
bootstrap ''' + get_bootstrap_name() + ''').
747

748
For this to work, Java and Ant need to be in your path, as does the
749
tools directory of the Android SDK.
750
''')
751

752
    # --private is required unless for sdl2, where there's also --launcher
753
    ap.add_argument('--private', dest='private',
4✔
754
                    help='the directory with the app source code files' +
755
                         ' (containing your main.py entrypoint)',
756
                    required=(get_bootstrap_name() != "sdl2"))
757
    ap.add_argument('--package', dest='package',
4✔
758
                    help=('The name of the java package the project will be'
759
                          ' packaged under.'),
760
                    required=True)
761
    ap.add_argument('--name', dest='name',
4✔
762
                    help=('The human-readable name of the project.'),
763
                    required=True)
764
    ap.add_argument('--numeric-version', dest='numeric_version',
4✔
765
                    help=('The numeric version number of the project. If not '
766
                          'given, this is automatically computed from the '
767
                          'version.'))
768
    ap.add_argument('--version', dest='version',
4✔
769
                    help=('The version number of the project. This should '
770
                          'consist of numbers and dots, and should have the '
771
                          'same number of groups of numbers as previous '
772
                          'versions.'),
773
                    required=True)
774
    if get_bootstrap_name() == "sdl2":
4!
775
        ap.add_argument('--launcher', dest='launcher', action='store_true',
4✔
776
                        help=('Provide this argument to build a multi-app '
777
                              'launcher, rather than a single app.'))
778
        ap.add_argument('--home-app', dest='home_app', action='store_true', default=False,
4✔
779
                        help=('Turn your application into a home app (launcher)'))
780
    ap.add_argument('--permission', dest='permissions', action='append', default=[],
4✔
781
                    help='The permissions to give this app.', nargs='+')
782
    ap.add_argument('--meta-data', dest='meta_data', action='append', default=[],
4✔
783
                    help='Custom key=value to add in application metadata')
784
    ap.add_argument('--uses-library', dest='android_used_libs', action='append', default=[],
4✔
785
                    help='Used shared libraries included using <uses-library> tag in AndroidManifest.xml')
786
    ap.add_argument('--asset', dest='assets',
4✔
787
                    action="append", default=[],
788
                    metavar="/path/to/source:dest",
789
                    help='Put this in the assets folder at assets/dest')
790
    ap.add_argument('--resource', dest='resources',
4✔
791
                    action="append", default=[],
792
                    metavar="/path/to/source:kind/asset",
793
                    help='Put this in the res folder at res/kind')
794
    ap.add_argument('--icon', dest='icon',
4✔
795
                    help=('A png file to use as the icon for '
796
                          'the application.'))
797
    ap.add_argument('--icon-fg', dest='icon_fg',
4✔
798
                    help=('A png file to use as the foreground of the adaptive icon '
799
                          'for the application.'))
800
    ap.add_argument('--icon-bg', dest='icon_bg',
4✔
801
                    help=('A png file to use as the background of the adaptive icon '
802
                          'for the application.'))
803
    ap.add_argument('--service', dest='services', action='append', default=[],
4✔
804
                    help='Declare a new service entrypoint: '
805
                         'NAME:PATH_TO_PY[:foreground]')
806
    ap.add_argument('--native-service', dest='native_services', action='append', default=[],
4✔
807
                    help='Declare a new native service: '
808
                         'package.name.service')
809
    if get_bootstrap_name() != "service_only":
4!
810
        ap.add_argument('--presplash', dest='presplash',
4✔
811
                        help=('A jpeg file to use as a screen while the '
812
                              'application is loading.'))
813
        ap.add_argument('--presplash-lottie', dest='presplash_lottie',
4✔
814
                        help=('A lottie (json) file to use as an animation while the '
815
                              'application is loading.'))
816
        ap.add_argument('--presplash-color',
4✔
817
                        dest='presplash_color',
818
                        default='#000000',
819
                        help=('A string to set the loading screen '
820
                              'background color. '
821
                              'Supported formats are: '
822
                              '#RRGGBB #AARRGGBB or color names '
823
                              'like red, green, blue, etc.'))
824
        ap.add_argument('--window', dest='window', action='store_true',
4✔
825
                        default=False,
826
                        help='Indicate if the application will be windowed')
827
        ap.add_argument('--manifest-orientation', dest='manifest_orientation',
4✔
828
                        help=('The orientation that will be set in the '
829
                              'android:screenOrientation attribute of the activity '
830
                              'in the AndroidManifest.xml file. If not set, '
831
                              'the value will be synthesized from the --orientation option.'))
832
        ap.add_argument('--orientation', dest='orientation',
4✔
833
                        action="append", default=[],
834
                        choices=['portrait', 'landscape', 'landscape-reverse', 'portrait-reverse'],
835
                        help=('The orientations that the app will display in. '
836
                              'Since Android ignores android:screenOrientation '
837
                              'when in multi-window mode (Which is the default on Android 12+), '
838
                              'this option will also set the window orientation hints '
839
                              'for apps using the (default) SDL bootstrap.'
840
                              'If multiple orientations are given, android:screenOrientation '
841
                              'will be set to "unspecified"'))
842

843
    ap.add_argument('--enable-androidx', dest='enable_androidx',
4✔
844
                    action='store_true',
845
                    help=('Enable the AndroidX support library, '
846
                          'requires api = 28 or greater'))
847
    ap.add_argument('--android-entrypoint', dest='android_entrypoint',
4✔
848
                    default=DEFAULT_PYTHON_ACTIVITY_JAVA_CLASS,
849
                    help='Defines which java class will be used for startup, usually a subclass of PythonActivity')
850
    ap.add_argument('--android-apptheme', dest='android_apptheme',
4✔
851
                    default='@android:style/Theme.NoTitleBar',
852
                    help='Defines which app theme should be selected for the main activity')
853
    ap.add_argument('--add-compile-option', dest='compile_options', default=[],
4✔
854
                    action='append', help='add compile options to gradle.build')
855
    ap.add_argument('--add-gradle-repository', dest='gradle_repositories',
4✔
856
                    default=[],
857
                    action='append',
858
                    help='Ddd a repository for gradle')
859
    ap.add_argument('--add-packaging-option', dest='packaging_options',
4✔
860
                    default=[],
861
                    action='append',
862
                    help='Dndroid packaging options')
863

864
    ap.add_argument('--wakelock', dest='wakelock', action='store_true',
4✔
865
                    help=('Indicate if the application needs the device '
866
                          'to stay on'))
867
    ap.add_argument('--blacklist', dest='blacklist',
4✔
868
                    default=join(curdir, 'blacklist.txt'),
869
                    help=('Use a blacklist file to match unwanted file in '
870
                          'the final APK'))
871
    ap.add_argument('--whitelist', dest='whitelist',
4✔
872
                    default=join(curdir, 'whitelist.txt'),
873
                    help=('Use a whitelist file to prevent blacklisting of '
874
                          'file in the final APK'))
875
    ap.add_argument('--release', dest='build_mode', action='store_const',
4✔
876
                    const='release', default='debug',
877
                    help='Build your app as a non-debug release build. '
878
                         '(Disables gdb debugging among other things)')
879
    ap.add_argument('--with-debug-symbols', dest='with_debug_symbols',
4✔
880
                    action='store_const', const=True, default=False,
881
                    help='Will keep debug symbols from `.so` files.')
882
    ap.add_argument('--add-jar', dest='add_jar', action='append',
4✔
883
                    help=('Add a Java .jar to the libs, so you can access its '
884
                          'classes with pyjnius. You can specify this '
885
                          'argument more than once to include multiple jars'))
886
    ap.add_argument('--add-aar', dest='add_aar', action='append',
4✔
887
                    help=('Add an aar dependency manually'))
888
    ap.add_argument('--depend', dest='depends', action='append',
4✔
889
                    help=('Add a external dependency '
890
                          '(eg: com.android.support:appcompat-v7:19.0.1)'))
891
    # The --sdk option has been removed, it is ignored in favour of
892
    # --android-api handled by toolchain.py
893
    ap.add_argument('--sdk', dest='sdk_version', default=-1,
4✔
894
                    type=int, help=('Deprecated argument, does nothing'))
895
    ap.add_argument('--minsdk', dest='min_sdk_version',
4✔
896
                    default=ndk_api, type=int,
897
                    help=('Minimum Android SDK version that the app supports. '
898
                          'Defaults to {}.'.format(ndk_api)))
899
    ap.add_argument('--allow-minsdk-ndkapi-mismatch', default=False,
4✔
900
                    action='store_true',
901
                    help=('Allow the --minsdk argument to be different from '
902
                          'the discovered ndk_api in the dist'))
903
    ap.add_argument('--intent-filters', dest='intent_filters',
4✔
904
                    help=('Add intent-filters xml rules to the '
905
                          'AndroidManifest.xml file. The argument is a '
906
                          'filename containing xml. The filename should be '
907
                          'located relative to the python-for-android '
908
                          'directory'))
909
    ap.add_argument('--res_xml', dest='res_xmls', action='append', default=[],
4✔
910
                    help='Add files to res/xml directory (for example device-filters)', nargs='+')
911
    ap.add_argument('--with-billing', dest='billing_pubkey',
4✔
912
                    help='If set, the billing service will be added (not implemented)')
913
    ap.add_argument('--add-source', dest='extra_source_dirs', action='append',
4✔
914
                    help='Include additional source dirs in Java build')
915
    if get_bootstrap_name() == "webview":
4!
916
        ap.add_argument('--port',
×
917
                        help='The port on localhost that the WebView will access',
918
                        default='5000')
919
    ap.add_argument('--try-system-python-compile', dest='try_system_python_compile',
4✔
920
                    action='store_true',
921
                    help='Use the system python during compileall if possible.')
922
    ap.add_argument('--sign', action='store_true',
4✔
923
                    help=('Try to sign the APK with your credentials. You must set '
924
                          'the appropriate environment variables.'))
925
    ap.add_argument('--add-activity', dest='add_activity', action='append',
4✔
926
                    help='Add this Java class as an Activity to the manifest.')
927
    ap.add_argument('--activity-launch-mode',
4✔
928
                    dest='activity_launch_mode',
929
                    default='singleTask',
930
                    help='Set the launch mode of the main activity in the manifest.')
931
    ap.add_argument('--allow-backup', dest='allow_backup', default='true',
4✔
932
                    help="if set to 'false', then android won't backup the application.")
933
    ap.add_argument('--backup-rules', dest='backup_rules', default='',
4✔
934
                    help=('Backup rules for Android Auto Backup. Argument is a '
935
                          'filename containing xml. The filename should be '
936
                          'located relative to the private directory containing your source code '
937
                          'files (containing your main.py entrypoint). '
938
                          'See https://developer.android.com/guide/topics/data/'
939
                          'autobackup#IncludingFiles for more information'))
940
    ap.add_argument('--no-byte-compile-python', dest='byte_compile_python',
4✔
941
                    action='store_false', default=True,
942
                    help='Skip byte compile for .py files.')
943
    ap.add_argument('--no-optimize-python', dest='optimize_python',
4✔
944
                    action='store_false', default=True,
945
                    help=('Whether to compile to optimised .pyc files, using -OO '
946
                          '(strips docstrings and asserts)'))
947
    ap.add_argument('--extra-manifest-xml', default='',
4✔
948
                    help=('Extra xml to write directly inside the <manifest> element of'
949
                          'AndroidManifest.xml'))
950
    ap.add_argument('--extra-manifest-application-arguments', default='',
4✔
951
                    help='Extra arguments to be added to the <manifest><application> tag of'
952
                         'AndroidManifest.xml')
953
    ap.add_argument('--manifest-placeholders', dest='manifest_placeholders',
4✔
954
                    default='[:]', help=('Inject build variables into the manifest '
955
                                         'via the manifestPlaceholders property'))
956
    ap.add_argument('--service-class-name', dest='service_class_name', default=DEFAULT_PYTHON_SERVICE_JAVA_CLASS,
4✔
957
                    help='Use that parameter if you need to implement your own PythonServive Java class')
958
    ap.add_argument('--activity-class-name', dest='activity_class_name', default=DEFAULT_PYTHON_ACTIVITY_JAVA_CLASS,
4✔
959
                    help='The full java class name of the main activity')
960

961
    return ap
4✔
962

963

964
def parse_args_and_make_package(args=None):
4✔
965
    global BLACKLIST_PATTERNS, WHITELIST_PATTERNS, PYTHON
966

967
    ndk_api = get_dist_ndk_min_api_level()
×
968
    ap = create_argument_parser()
×
969

970
    # Put together arguments, and add those from .p4a config file:
971
    if args is None:
×
972
        args = sys.argv[1:]
×
973

974
    def _read_configuration():
×
975
        if not exists(".p4a"):
×
976
            return
×
977
        print("Reading .p4a configuration")
×
978
        with open(".p4a") as fd:
×
979
            lines = fd.readlines()
×
980
        lines = [shlex.split(line)
×
981
                 for line in lines if not line.startswith("#")]
982
        for line in lines:
×
983
            for arg in line:
×
984
                args.append(arg)
×
985
    _read_configuration()
×
986

987
    args = ap.parse_args(args)
×
988

989
    if args.name and args.name[0] == '"' and args.name[-1] == '"':
×
990
        args.name = args.name[1:-1]
×
991

992
    if ndk_api != args.min_sdk_version:
×
993
        print(('WARNING: --minsdk argument does not match the api that is '
×
994
               'compiled against. Only proceed if you know what you are '
995
               'doing, otherwise use --minsdk={} or recompile against api '
996
               '{}').format(ndk_api, args.min_sdk_version))
997
        if not args.allow_minsdk_ndkapi_mismatch:
×
998
            print('You must pass --allow-minsdk-ndkapi-mismatch to build '
×
999
                  'with --minsdk different to the target NDK api from the '
1000
                  'build step')
1001
            sys.exit(1)
×
1002
        else:
1003
            print('Proceeding with --minsdk not matching build target api')
×
1004

1005
    if args.billing_pubkey:
×
1006
        print('Billing not yet supported!')
×
1007
        sys.exit(1)
×
1008

1009
    if args.sdk_version == -1:
×
1010
        print('WARNING: Received a --sdk argument, but this argument is '
×
1011
              'deprecated and does nothing.')
1012
        args.sdk_version = -1  # ensure it is not used
×
1013

1014
    args.permissions = parse_permissions(args.permissions)
×
1015

1016
    args.manifest_orientation = get_manifest_orientation(
×
1017
        args.orientation, args.manifest_orientation
1018
    )
1019

1020
    if get_bootstrap_name() == "sdl2":
×
1021
        args.sdl_orientation_hint = get_sdl_orientation_hint(args.orientation)
×
1022

1023
    if args.res_xmls and isinstance(args.res_xmls[0], list):
×
1024
        args.res_xmls = [x for res in args.res_xmls for x in res]
×
1025

1026
    if args.try_system_python_compile:
×
1027
        # Hardcoding python2.7 is okay for now, as python3 skips the
1028
        # compilation anyway
1029
        python_executable = 'python2.7'
×
1030
        try:
×
1031
            subprocess.call([python_executable, '--version'])
×
1032
        except (OSError, subprocess.CalledProcessError):
×
1033
            pass
×
1034
        else:
1035
            PYTHON = python_executable
×
1036

1037
    if args.blacklist:
×
1038
        with open(args.blacklist) as fd:
×
1039
            patterns = [x.strip() for x in fd.read().splitlines()
×
1040
                        if x.strip() and not x.strip().startswith('#')]
1041
        BLACKLIST_PATTERNS += patterns
×
1042

1043
    if args.whitelist:
×
1044
        with open(args.whitelist) as fd:
×
1045
            patterns = [x.strip() for x in fd.read().splitlines()
×
1046
                        if x.strip() and not x.strip().startswith('#')]
1047
        WHITELIST_PATTERNS += patterns
×
1048

1049
    if args.private is None and \
×
1050
            get_bootstrap_name() == 'sdl2' and args.launcher is None:
1051
        print('Need --private directory or ' +
×
1052
              '--launcher (SDL2 bootstrap only)' +
1053
              'to have something to launch inside the .apk!')
1054
        sys.exit(1)
×
1055
    make_package(args)
×
1056

1057
    return args
×
1058

1059

1060
if __name__ == "__main__":
1061
    if get_bootstrap_name() in ('sdl2', 'webview', 'service_only'):
1062
        WHITELIST_PATTERNS.append('pyconfig.h')
1063
    parse_args_and_make_package()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc