Skip to content

Commit 4dbf4de

Browse files
committed
split _finish_package off _build_package
1 parent d5edff1 commit 4dbf4de

File tree

1 file changed

+75
-56
lines changed

1 file changed

+75
-56
lines changed

pythonforandroid/toolchain.py

Lines changed: 75 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -930,13 +930,16 @@ def _dist(self):
930930
ctx.distribution = dist
931931
return dist
932932

933-
def _fix_args(self, args):
934-
935-
# Manually fixing these arguments at the string stage is
936-
# unsatisfactory and should probably be changed somehow, but
937-
# we can't leave it until later as the build.py scripts assume
938-
# they are in the current directory.
939-
# works in-place
933+
@staticmethod
934+
def _fix_args(args):
935+
"""
936+
Manually fixing these arguments at the string stage is
937+
unsatisfactory and should probably be changed somehow, but
938+
we can't leave it until later as the build.py scripts assume
939+
they are in the current directory.
940+
works in-place
941+
:param args: parser args
942+
"""
940943

941944
fix_args = ('--dir', '--private', '--add-jar', '--add-source',
942945
'--whitelist', '--blacklist', '--presplash', '--icon')
@@ -950,9 +953,11 @@ def _fix_args(self, args):
950953
elif i + 1 < len(unknown_args):
951954
unknown_args[i+1] = realpath(expanduser(unknown_args[i+1]))
952955

953-
def _prepare_release_env(self, args):
956+
@staticmethod
957+
def _prepare_release_env(args):
954958
"""
955959
prepares envitonment dict with the necessary flags for signing an apk
960+
:param args: parser args
956961
"""
957962
env = os.environ.copy()
958963
if args.build_mode == 'release':
@@ -970,20 +975,26 @@ def _prepare_release_env(self, args):
970975
return env
971976

972977
def _build_package(self, args, package_type):
973-
"""Create an APK using the given distribution."""
978+
"""
979+
Creates an android package using gradle
980+
:param args: parser args
981+
:param package_type: one of 'apk', 'aar'
982+
:return (gradle output, build_args)
983+
"""
974984
ctx = self.ctx
975985
dist = self._dist
976986
bs = Bootstrap.get_bootstrap(args.bootstrap, ctx)
977987
ctx.prepare_bootstrap(bs)
978988

979989
self._fix_args(args)
980990
env = self._prepare_release_env(args)
981-
build = imp.load_source('build', join(dist.dist_dir, 'build.py'))
982991

983992
with current_directory(dist.dist_dir):
984993
self.hook("before_apk_build")
985994
os.environ["ANDROID_API"] = str(self.ctx.android_api)
986-
build_args = build.parse_args(args.unknown_args)
995+
build = imp.load_source('build', join(dist.dist_dir, 'build.py'))
996+
build_args = build.parse_args(args.unknown_args) # this call triggers build.make_package()
997+
987998
self.hook("after_apk_build")
988999
self.hook("before_apk_assemble")
9891000
build_tools_versions = os.listdir(join(ctx.sdk_dir,
@@ -1022,64 +1033,72 @@ def _build_package(self, args, package_type):
10221033
"Unknown build mode {} for apk()".format(args.build_mode))
10231034
output = shprint(gradlew, gradle_task, _tail=20,
10241035
_critical=True, _env=env)
1025-
output_dir = join(dist.dist_dir, "build", "outputs", package_type)
1026-
apk_glob = "*-{}.%s" % package_type
1027-
1028-
is_library = package_type == 'aar'
1029-
if is_library:
1030-
apk_dir = output_dir
1031-
else:
1032-
apk_dir = join(output_dir, args.build_mode)
1036+
return output, build_args
10331037

1034-
apk_add_version = True
1038+
def _finish_package(self, args, output, build_args, package_type, output_dir):
1039+
"""
1040+
Finishes the package after the gradle script run
1041+
:param args: the parser args
1042+
:param output: RunningCommand output
1043+
:param build_args: build args as returned by build.parse_args
1044+
:param package_type: one of 'apk', 'aar'
1045+
:param output_dir: where to put the package file
1046+
"""
1047+
dist = self._dist
1048+
with current_directory(self._dist.dist_dir):
1049+
package_glob = "*-{}.%s" % package_type
1050+
package_add_version = True
10351051

10361052
self.hook("after_apk_assemble")
10371053

1038-
info_main('# Copying android package to current directory')
1039-
1040-
apk_re = re.compile(r'.*Package: (.*\.apk)$')
1041-
apk_file = None
1042-
for line in reversed(output.splitlines()):
1043-
m = apk_re.match(line)
1044-
if m:
1045-
apk_file = m.groups()[0]
1046-
break
1054+
info_main('# Copying android package to current directory')
10471055

1048-
if not apk_file:
1049-
info_main('# Android package filename not found in build output. Guessing...')
1050-
if args.build_mode == "release":
1051-
suffixes = ("release", "release-unsigned")
1052-
else:
1053-
suffixes = ("debug", )
1054-
for suffix in suffixes:
1055-
apks = glob.glob(join(apk_dir, apk_glob.format(suffix)))
1056-
if apks:
1057-
if len(apks) > 1:
1058-
info('More than one built APK found... guessing you '
1059-
'just built {}'.format(apks[-1]))
1060-
apk_file = apks[-1]
1056+
package_re = re.compile(r'.*Package: (.*\.apk)$')
1057+
package_file = None
1058+
for line in reversed(output.splitlines()):
1059+
m = package_re.match(line)
1060+
if m:
1061+
package_file = m.groups()[0]
10611062
break
1063+
if not package_file:
1064+
info_main('# Android package filename not found in build output. Guessing...')
1065+
if args.build_mode == "release":
1066+
suffixes = ("release", "release-unsigned")
1067+
else:
1068+
suffixes = ("debug", )
1069+
for suffix in suffixes:
1070+
package_files = glob.glob(join(output_dir, package_glob.format(suffix)))
1071+
if package_files:
1072+
if len(package_files) > 1:
1073+
info('More than one built APK found... guessing you '
1074+
'just built {}'.format(package_files[-1]))
1075+
package_file = package_files[-1]
1076+
break
1077+
else:
1078+
raise BuildInterruptingException('Couldn\'t find the built APK')
1079+
1080+
info_main('# Found android package file: {}'.format(package_file))
1081+
if package_add_version:
1082+
info('# Add version number to android package')
1083+
package_name = basename(package_file)[:-len(APK_SUFFIX)]
1084+
package_file_dest = "{}-{}-{}".format(
1085+
package_name, build_args.version, APK_SUFFIX)
1086+
info('# Android package renamed to {}'.format(package_file_dest))
1087+
shprint(sh.cp, package_file, package_file_dest)
10621088
else:
1063-
raise BuildInterruptingException('Couldn\'t find the built APK')
1064-
1065-
info_main('# Found android package file: {}'.format(apk_file))
1066-
if apk_add_version:
1067-
info('# Add version number to android package')
1068-
apk_name = basename(apk_file)[:-len(APK_SUFFIX)]
1069-
apk_file_dest = "{}-{}-{}".format(
1070-
apk_name, build_args.version, APK_SUFFIX)
1071-
info('# Android package renamed to {}'.format(apk_file_dest))
1072-
shprint(sh.cp, apk_file, apk_file_dest)
1073-
else:
1074-
shprint(sh.cp, apk_file, './')
1089+
shprint(sh.cp, package_file, './')
10751090

10761091
@require_prebuilt_dist
10771092
def apk(self, args):
1078-
return self._build_package(args, package_type='apk')
1093+
output, build_args = self._build_package(args, package_type='apk')
1094+
output_dir = join(self._dist.dist_dir, "build", "outputs", 'apk', args.build_mode)
1095+
self._finish_package(args, output, build_args, 'apk', output_dir)
10791096

10801097
@require_prebuilt_dist
10811098
def aar(self, args):
1082-
return self._build_package(args, package_type='aar')
1099+
output, build_args = self._build_package(args, package_type='aar')
1100+
output_dir = join(self._dist.dist_dir, "build", "outputs", 'aar')
1101+
self._finish_package(args, output, build_args, 'aar', output_dir)
10831102

10841103
@require_prebuilt_dist
10851104
def create(self, args):

0 commit comments

Comments
 (0)