@@ -930,13 +930,16 @@ def _dist(self):
930
930
ctx .distribution = dist
931
931
return dist
932
932
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
+ """
940
943
941
944
fix_args = ('--dir' , '--private' , '--add-jar' , '--add-source' ,
942
945
'--whitelist' , '--blacklist' , '--presplash' , '--icon' )
@@ -950,9 +953,11 @@ def _fix_args(self, args):
950
953
elif i + 1 < len (unknown_args ):
951
954
unknown_args [i + 1 ] = realpath (expanduser (unknown_args [i + 1 ]))
952
955
953
- def _prepare_release_env (self , args ):
956
+ @staticmethod
957
+ def _prepare_release_env (args ):
954
958
"""
955
959
prepares envitonment dict with the necessary flags for signing an apk
960
+ :param args: parser args
956
961
"""
957
962
env = os .environ .copy ()
958
963
if args .build_mode == 'release' :
@@ -970,20 +975,26 @@ def _prepare_release_env(self, args):
970
975
return env
971
976
972
977
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
+ """
974
984
ctx = self .ctx
975
985
dist = self ._dist
976
986
bs = Bootstrap .get_bootstrap (args .bootstrap , ctx )
977
987
ctx .prepare_bootstrap (bs )
978
988
979
989
self ._fix_args (args )
980
990
env = self ._prepare_release_env (args )
981
- build = imp .load_source ('build' , join (dist .dist_dir , 'build.py' ))
982
991
983
992
with current_directory (dist .dist_dir ):
984
993
self .hook ("before_apk_build" )
985
994
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
+
987
998
self .hook ("after_apk_build" )
988
999
self .hook ("before_apk_assemble" )
989
1000
build_tools_versions = os .listdir (join (ctx .sdk_dir ,
@@ -1022,64 +1033,72 @@ def _build_package(self, args, package_type):
1022
1033
"Unknown build mode {} for apk()" .format (args .build_mode ))
1023
1034
output = shprint (gradlew , gradle_task , _tail = 20 ,
1024
1035
_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
1033
1037
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
1035
1051
1036
1052
self .hook ("after_apk_assemble" )
1037
1053
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' )
1047
1055
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 ]
1061
1062
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 )
1062
1088
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 , './' )
1075
1090
1076
1091
@require_prebuilt_dist
1077
1092
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 )
1079
1096
1080
1097
@require_prebuilt_dist
1081
1098
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 )
1083
1102
1084
1103
@require_prebuilt_dist
1085
1104
def create (self , args ):
0 commit comments