Skip to content

Commit 740fd54

Browse files
committed
add release builds and apk signing
1 parent 93728aa commit 740fd54

File tree

7 files changed

+87
-15
lines changed

7 files changed

+87
-15
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is used to override default values used by the Ant build system.
2+
#
3+
# This file must be checked in Version Control Systems, as it is
4+
# integral to the build system of your project.
5+
6+
# This file is only used by the Ant script.
7+
8+
# You can use this to override default values such as
9+
# 'source.dir' for the location of your java source folder and
10+
# 'out.dir' for the location of your output folder.
11+
12+
# You can also use it define how the release builds are signed by declaring
13+
# the following properties:
14+
# 'key.store' for the location of your keystore and
15+
# 'key.alias' for the name of the key to use.
16+
# The password will be asked during the build when you use the 'release' target.
17+
18+
key.store=${env.P4A_RELEASE_KEYSTORE}
19+
key.alias=${env.P4A_RELEASE_KEYALIAS}
20+
key.store.password=${env.P4A_RELEASE_KEYSTORE_PASSWD}
21+
key.alias.password=${env.P4A_RELEASE_KEYALIAS_PASSWD}

pythonforandroid/bootstraps/pygame/build/build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,8 @@ def parse_args(args=None):
510510

511511
make_package(args)
512512

513+
return args
514+
515+
513516
if __name__ == '__main__':
514517
parse_args()

pythonforandroid/bootstraps/pygame/build/templates/build.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<property file="ant.properties" />
66
<loadproperties srcFile="project.properties" />
77

8+
<property environment="env" />
9+
<property file="build.properties" />
10+
811
<fail
912
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project'"
1013
unless="sdk.dir"

pythonforandroid/bootstraps/sdl2/build/build.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@
1515
# 'key.alias' for the name of the key to use.
1616
# The password will be asked during the build when you use the 'release' target.
1717

18+
key.store=${env.P4A_RELEASE_KEYSTORE}
19+
key.alias=${env.P4A_RELEASE_KEYALIAS}
20+
key.store.password=${env.P4A_RELEASE_KEYSTORE_PASSWD}
21+
key.alias.password=${env.P4A_RELEASE_KEYALIAS_PASSWD}

pythonforandroid/bootstraps/sdl2/build/build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ def parse_args(args=None):
477477

478478
make_package(args)
479479

480+
return args
481+
482+
480483
if __name__ == "__main__":
481484

482485
parse_args()

pythonforandroid/bootstraps/sdl2/build/templates/build.tmpl.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<isset property="env.ANDROID_HOME" />
3939
</condition>
4040

41+
<property file="build.properties" />
42+
4143
<!-- The project.properties file is created and updated by the 'android'
4244
tool, as well as ADT.
4345

pythonforandroid/toolchain.py

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,21 @@ def _dist(self):
527527
def apk(self, args):
528528
'''Create an APK using the given distribution.'''
529529

530-
# AND: Need to add a parser here for any extra options
531-
# parser = argparse.ArgumentParser(
532-
# description='Build an APK')
533-
# args = parser.parse_args(args)
530+
ap = argparse.ArgumentParser(
531+
description='Build an APK')
532+
ap.add_argument('--release', dest='build_mode', action='store_const',
533+
const='release', default='debug',
534+
help='Build the APK in Release mode')
535+
ap.add_argument('--keystore', dest='keystore', action='store', default=None,
536+
help=('Keystore for JAR signing key, will use jarsigner '
537+
'default if not specified (release build only)'))
538+
ap.add_argument('--signkey', dest='signkey', action='store', default=None,
539+
help='Key alias to sign APK with (release build only)')
540+
ap.add_argument('--keystorepw', dest='keystorepw', action='store', default=None,
541+
help='Password for keystore')
542+
ap.add_argument('--signkeypw', dest='signkeypw', action='store', default=None,
543+
help='Password for key alias')
544+
apk_args, args = ap.parse_known_args(args)
534545

535546
ctx = self.ctx
536547
dist = self._dist
@@ -550,21 +561,46 @@ def apk(self, args):
550561
else:
551562
args[i+1] = realpath(expanduser(args[i+1]))
552563

564+
env = os.environ.copy()
565+
if apk_args.build_mode == 'release':
566+
if apk_args.keystore:
567+
env['P4A_RELEASE_KEYSTORE'] = realpath(expanduser(apk_args.keystore))
568+
if apk_args.signkey:
569+
env['P4A_RELEASE_KEYALIAS'] = apk_args.signkey
570+
if apk_args.keystorepw:
571+
env['P4A_RELEASE_KEYSTORE_PASSWD'] = apk_args.keystorepw
572+
if apk_args.signkeypw:
573+
env['P4A_RELEASE_KEYALIAS_PASSWD'] = apk_args.signkeypw
574+
elif apk_args.keystorepw and 'P4A_RELEASE_KEYALIAS_PASSWD' not in env:
575+
env['P4A_RELEASE_KEYALIAS_PASSWD'] = apk_args.keystorepw
576+
553577
build = imp.load_source('build', join(dist.dist_dir, 'build.py'))
554578
with current_directory(dist.dist_dir):
555-
build.parse_args(args)
556-
shprint(sh.ant, 'debug', _tail=20, _critical=True)
579+
build_args = build.parse_args(args)
580+
output = shprint(sh.ant, apk_args.build_mode, _tail=20, _critical=True, _env=env)
557581

558-
# AND: This is very crude, needs improving. Also only works
559-
# for debug for now.
560582
info_main('# Copying APK to current directory')
561-
apks = glob.glob(join(dist.dist_dir, 'bin', '*-*-debug.apk'))
562-
if len(apks) == 0:
563-
raise ValueError('Couldn\'t find the built APK')
564-
if len(apks) > 1:
565-
info('More than one built APK found...guessing you '
566-
'just built {}'.format(apks[-1]))
567-
shprint(sh.cp, apks[-1], './')
583+
584+
apk_re = re.compile(r'.*Package: (.*\.apk)$')
585+
apk_file = None
586+
for line in reversed(output.splitlines()):
587+
m = apk_re.match(line)
588+
if m:
589+
apk_file = m.groups()[0]
590+
break
591+
592+
if not apk_file:
593+
info_main('# APK filename not found in build output, trying to guess')
594+
apks = glob.glob(join(dist.dist_dir, 'bin', '*-*-{}.apk'.format(apk_args.build_mode)))
595+
if len(apks) == 0:
596+
raise ValueError('Couldn\'t find the built APK')
597+
if len(apks) > 1:
598+
info('More than one built APK found...guessing you '
599+
'just built {}'.format(apks[-1]))
600+
apk_file = apks[-1]
601+
602+
info_main('# Found APK file: {}'.format(apk_file))
603+
shprint(sh.cp, apk_file, './')
568604

569605
@require_prebuilt_dist
570606
def create(self, args):

0 commit comments

Comments
 (0)