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

kivy / python-for-android / 6774900670

06 Nov 2023 06:20PM UTC coverage: 59.187% (+0.08%) from 59.106%
6774900670

push

github

web-flow
Remove `distutils` usage, as is not available anymore on Python `3.12` (#2912)

* Remove distutils usage, as is not available anymore on Python 3.12

* Updated testapps to use setuptools instead of distutils

943 of 2239 branches covered (0.0%)

Branch coverage included in aggregate %.

24 of 26 new or added lines in 6 files covered. (92.31%)

1 existing line in 1 file now uncovered.

4733 of 7351 relevant lines covered (64.39%)

2.56 hits per line

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

23.31
/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 fnmatch import fnmatch
4✔
21
import jinja2
4✔
22

23
from pythonforandroid.util import rmdir, ensure_dir, max_build_tool_version
4✔
24

25

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

39

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

43

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

47

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

55
curdir = dirname(__file__)
4✔
56

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

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

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

74
WHITELIST_PATTERNS = []
4✔
75

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

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

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

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

92

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

96

97
def render(template, dest, **kwargs):
4✔
98
    '''Using jinja2, render `template` to the filename `dest`, supplying the
99

100
    keyword arguments as template parameters.
101
    '''
102

103
    dest_dir = dirname(dest)
×
104
    if dest_dir and not exists(dest_dir):
×
105
        makedirs(dest_dir)
×
106

107
    template = environment.get_template(template)
×
108
    text = template.render(**kwargs)
×
109

110
    f = open(dest, 'wb')
×
111
    f.write(text.encode('utf-8'))
×
112
    f.close()
×
113

114

115
def is_whitelist(name):
4✔
116
    return match_filename(WHITELIST_PATTERNS, name)
×
117

118

119
def is_blacklist(name):
4✔
120
    if is_whitelist(name):
×
121
        return False
×
122
    return match_filename(BLACKLIST_PATTERNS, name)
×
123

124

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

134

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

148

149
def make_tar(tfn, source_dirs, byte_compile_python=False, optimize_python=True):
4✔
150
    '''
151
    Make a zip file `fn` from the contents of source_dis.
152
    '''
153

154
    def clean(tinfo):
×
155
        """cleaning function (for reproducible builds)"""
156
        tinfo.uid = tinfo.gid = 0
×
157
        tinfo.uname = tinfo.gname = ''
×
158
        tinfo.mtime = 0
×
159
        return tinfo
×
160

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

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

194
        # put the file
195
        tf.add(fn, afn, filter=clean)
×
196
    tf.close()
×
197
    gf.close()
×
198

199

200
def compile_py_file(python_file, optimize_python=True):
4✔
201
    '''
202
    Compile python_file to *.pyc and return the filename of the *.pyc file.
203
    '''
204

205
    if PYTHON is None:
×
206
        return
×
207

208
    args = [PYTHON, '-m', 'compileall', '-b', '-f', python_file]
×
209
    if optimize_python:
×
210
        # -OO = strip docstrings
211
        args.insert(1, '-OO')
×
212
    return_code = subprocess.call(args)
×
213

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

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

222

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

237
    assets_dir = "src/main/assets"
×
238

239
    # Delete the old assets.
240
    rmdir(assets_dir, ignore_errors=True)
×
241
    ensure_dir(assets_dir)
×
242

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

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

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

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

302
        for asset in args.assets:
×
303
            asset_src, asset_dest = asset.split(":")
×
304
            if isfile(realpath(asset_src)):
×
305
                ensure_dir(dirname(join(assets_dir, asset_dest)))
×
306
                shutil.copy(realpath(asset_src), join(assets_dir, asset_dest))
×
307
            else:
308
                shutil.copytree(realpath(asset_src), join(assets_dir, asset_dest))
×
309

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

329
    # Remove extra env vars tar-able directory:
330
    rmdir(env_vars_tarpath)
×
331

332
    # Prepare some variables for templating process
333
    res_dir = "src/main/res"
×
334
    res_dir_initial = "src/res_initial"
×
335
    # make res_dir stateless
336
    if exists(res_dir_initial):
×
337
        rmdir(res_dir, ignore_errors=True)
×
338
        shutil.copytree(res_dir_initial, res_dir)
×
339
    else:
340
        shutil.copytree(res_dir, res_dir_initial)
×
341

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

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

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

389
            shutil.copy(
×
390
                args.presplash or default_presplash,
391
                join(res_dir, 'drawable/presplash.jpg')
392
            )
393

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

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

415
    versioned_name = (args.name.replace(' ', '').replace('\'', '') +
×
416
                      '-' + args.version)
417

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

432
    if args.intent_filters:
×
433
        with open(args.intent_filters) as fd:
×
434
            args.intent_filters = fd.read()
×
435

436
    if not args.add_activity:
×
437
        args.add_activity = []
×
438

439
    if not args.activity_launch_mode:
×
440
        args.activity_launch_mode = ''
×
441

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

458
    service = False
×
459
    if args.private:
×
460
        service_main = join(realpath(args.private), 'service', 'main.py')
×
461
        if exists(service_main) or exists(service_main + 'o'):
×
462
            service = True
×
463

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

472
        foreground = 'foreground' in options
×
473
        sticky = 'sticky' in options
×
474

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

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

498
    if android_api.isdigit():
×
499
        android_api = int(android_api)
×
500
    else:
501
        raise ValueError(
×
502
            "failed to extract the Android API level from " +
503
            "build.properties. expected int, got: '" +
504
            str(android_api) + "'"
505
        )
506

507
    with open('local.properties', 'r') as fileh:
×
508
        sdk_dir = fileh.read().strip()
×
509
    sdk_dir = sdk_dir[8:]
×
510

511
    # Try to build with the newest available build tools
512
    ignored = {".DS_Store", ".ds_store"}
×
513
    build_tools_versions = [x for x in listdir(join(sdk_dir, 'build-tools')) if x not in ignored]
×
NEW
514
    build_tools_version = max_build_tool_version(build_tools_versions)
×
515

516
    # Folder name for launcher (used by SDL2 bootstrap)
517
    url_scheme = 'kivy'
×
518

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

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

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

551
    # Copy the AndroidManifest.xml to the dist root dir so that ant
552
    # can also use it
553
    if exists('AndroidManifest.xml'):
×
554
        remove('AndroidManifest.xml')
×
555
    shutil.copy(manifest_path, 'AndroidManifest.xml')
×
556

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

570
    # gradle properties
571
    render(
×
572
        'gradle.tmpl.properties',
573
        'gradle.properties',
574
        args=args)
575

576
    # ant build templates
577
    render(
×
578
        'build.tmpl.xml',
579
        'build.xml',
580
        args=args,
581
        versioned_name=versioned_name)
582

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

604
    if exists(join("templates", "custom_rules.tmpl.xml")):
×
605
        render(
×
606
            'custom_rules.tmpl.xml',
607
            'custom_rules.xml',
608
            args=args)
609

610
    if get_bootstrap_name() == "webview":
×
611
        render('WebViewLoader.tmpl.java',
×
612
               'src/main/java/org/kivy/android/WebViewLoader.java',
613
               args=args)
614

615
    if args.sign:
×
616
        render('build.properties', 'build.properties')
×
617
    else:
618
        if exists('build.properties'):
×
619
            os.remove('build.properties')
×
620

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

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

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

650

651
def parse_permissions(args_permissions):
4✔
652
    if args_permissions and isinstance(args_permissions[0], list):
4!
653
        args_permissions = [p for perm in args_permissions for p in perm]
4✔
654

655
    def _is_advanced_permission(permission):
4✔
656
        return permission.startswith("(") and permission.endswith(")")
4✔
657

658
    def _decode_advanced_permission(permission):
4✔
659
        SUPPORTED_PERMISSION_PROPERTIES = ["name", "maxSdkVersion", "usesPermissionFlags"]
4✔
660
        _permission_args = permission[1:-1].split(";")
4✔
661
        _permission_args = (arg.split("=") for arg in _permission_args)
4✔
662
        advanced_permission = dict(_permission_args)
4✔
663

664
        if "name" not in advanced_permission:
4!
665
            raise ValueError("Advanced permission must have a name property")
×
666

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

675
        return advanced_permission
4✔
676

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

688

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

700

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

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

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

722

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

734

735
def create_argument_parser():
4✔
736
    ndk_api = get_dist_ndk_min_api_level()
4✔
737
    import argparse
4✔
738
    ap = argparse.ArgumentParser(description='''\
4✔
739
Package a Python application for Android (using
740
bootstrap ''' + get_bootstrap_name() + ''').
741

742
For this to work, Java and Ant need to be in your path, as does the
743
tools directory of the Android SDK.
744
''')
745

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

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

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

955
    return ap
4✔
956

957

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

961
    ndk_api = get_dist_ndk_min_api_level()
×
962
    ap = create_argument_parser()
×
963

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

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

981
    args = ap.parse_args(args)
×
982

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

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

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

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

1008
    args.permissions = parse_permissions(args.permissions)
×
1009

1010
    args.manifest_orientation = get_manifest_orientation(
×
1011
        args.orientation, args.manifest_orientation
1012
    )
1013

1014
    if get_bootstrap_name() == "sdl2":
×
1015
        args.sdl_orientation_hint = get_sdl_orientation_hint(args.orientation)
×
1016

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

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

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

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

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

1051
    return args
×
1052

1053

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