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

kivy / python-for-android / 4033302547

pending completion
4033302547

push

github

GitHub
Merge pull request #2740 from misl6/release-2023.01.28

907 of 2274 branches covered (39.89%)

Branch coverage included in aggregate %.

52 of 67 new or added lines in 5 files covered. (77.61%)

4665 of 7403 relevant lines covered (63.01%)

2.49 hits per line

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

23.19
/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!
NEW
76
    PYTHON = get_hostpython()
×
NEW
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")
×
NEW
252
        if hasattr(args, "sdl_orientation_hint"):
×
NEW
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
    try:
×
502
        int(android_api)
×
503
    except (ValueError, TypeError):
×
504
        raise ValueError(
×
505
            "failed to extract the Android API level from " +
506
            "build.properties. expected int, got: '" +
507
            str(android_api) + "'"
508
        )
509
    with open('local.properties', 'r') as fileh:
×
510
        sdk_dir = fileh.read().strip()
×
511
    sdk_dir = sdk_dir[8:]
×
512

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

654

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

659
    def _is_advanced_permission(permission):
4✔
660
        return permission.startswith("(") and permission.endswith(")")
4✔
661

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

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

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

679
        return advanced_permission
4✔
680

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

692

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

704

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

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

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

726

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

738

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

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

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

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

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

957
    return ap
4✔
958

959

960
def parse_args_and_make_package(args=None):
4✔
961
    global BLACKLIST_PATTERNS, WHITELIST_PATTERNS, PYTHON
962

NEW
963
    ndk_api = get_dist_ndk_min_api_level()
×
NEW
964
    ap = create_argument_parser()
×
965

966
    # Put together arguments, and add those from .p4a config file:
967
    if args is None:
×
968
        args = sys.argv[1:]
×
969

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

983
    args = ap.parse_args(args)
×
984

985
    if args.name and args.name[0] == '"' and args.name[-1] == '"':
×
986
        args.name = args.name[1:-1]
×
987

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

1001
    if args.billing_pubkey:
×
1002
        print('Billing not yet supported!')
×
1003
        sys.exit(1)
×
1004

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

NEW
1010
    args.permissions = parse_permissions(args.permissions)
×
1011

NEW
1012
    args.manifest_orientation = get_manifest_orientation(
×
1013
        args.orientation, args.manifest_orientation
1014
    )
1015

NEW
1016
    if get_bootstrap_name() == "sdl2":
×
NEW
1017
        args.sdl_orientation_hint = get_sdl_orientation_hint(args.orientation)
×
1018

1019
    if args.res_xmls and isinstance(args.res_xmls[0], list):
×
1020
        args.res_xmls = [x for res in args.res_xmls for x in res]
×
1021

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

1033
    if args.blacklist:
×
1034
        with open(args.blacklist) as fd:
×
1035
            patterns = [x.strip() for x in fd.read().splitlines()
×
1036
                        if x.strip() and not x.strip().startswith('#')]
1037
        BLACKLIST_PATTERNS += patterns
×
1038

1039
    if args.whitelist:
×
1040
        with open(args.whitelist) as fd:
×
1041
            patterns = [x.strip() for x in fd.read().splitlines()
×
1042
                        if x.strip() and not x.strip().startswith('#')]
1043
        WHITELIST_PATTERNS += patterns
×
1044

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

1053
    return args
×
1054

1055

1056
if __name__ == "__main__":
1057
    if get_bootstrap_name() in ('sdl2', 'webview', 'service_only'):
1058
        WHITELIST_PATTERNS.append('pyconfig.h')
1059
    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