@@ -65,7 +65,7 @@ def check_python_dependencies():
6565
6666import sys
6767from sys import platform
68- from os .path import (join , dirname , realpath , exists , expanduser )
68+ from os .path import (join , dirname , realpath , exists , expanduser , basename )
6969import os
7070import glob
7171import shutil
@@ -605,7 +605,7 @@ def clean(self, args):
605605 'Asked to clean "{}" but this argument is not '
606606 'recognised' .format (component )))
607607 component_clean_methods [component ](args )
608-
608+
609609
610610 def clean_all (self , args ):
611611 '''Delete all build components; the package cache, package builds,
@@ -755,18 +755,48 @@ def apk(self, args):
755755 build = imp .load_source ('build' , join (dist .dist_dir , 'build.py' ))
756756 with current_directory (dist .dist_dir ):
757757 self .hook ("before_apk_build" )
758+ os .environ ["ANDROID_API" ] = str (self .ctx .android_api )
758759 build_args = build .parse_args (args .unknown_args )
759760 self .hook ("after_apk_build" )
760761 self .hook ("before_apk_assemble" )
761762
762- try :
763- ant = sh .Command ('ant' )
764- except sh .CommandNotFound :
765- error ('Could not find ant binary, please install it and make '
766- 'sure it is in your $PATH.' )
767- exit (1 )
763+ if exists (join (dist .dist_dir , "templates" , "build.tmpl.gradle" )):
764+ # gradle-based build
765+ env ["ANDROID_NDK_HOME" ] = self .ctx .ndk_dir
766+ env ["ANDROID_HOME" ] = self .ctx .sdk_dir
767+
768+ gradlew = sh .Command ('./gradlew' )
769+ if args .build_mode == "debug" :
770+ gradle_task = "assembleDebug"
771+ elif args .build_mode == "release" :
772+ gradle_task = "assembleRelease"
773+ else :
774+ error ("Unknown build mode {} for apk()" .format (
775+ args .build_mode ))
776+ exit (1 )
777+ output = shprint (gradlew , gradle_task , _tail = 20 ,
778+ _critical = True , _env = env )
779+
780+ # gradle output apks somewhere else
781+ # and don't have version in file
782+ apk_dir = join (dist .dist_dir , "build" , "outputs" , "apk" )
783+ apk_glob = "*-{}.apk"
784+ apk_add_version = True
785+
786+ else :
787+ # ant-based build
788+ try :
789+ ant = sh .Command ('ant' )
790+ except sh .CommandNotFound :
791+ error ('Could not find ant binary, please install it '
792+ 'and make sure it is in your $PATH.' )
793+ exit (1 )
794+ output = shprint (ant , args .build_mode , _tail = 20 ,
795+ _critical = True , _env = env )
796+ apk_dir = join (dist .dist_dir , "bin" )
797+ apk_glob = "*-*-{}.apk"
798+ apk_add_version = False
768799
769- output = shprint (ant , args .build_mode , _tail = 20 , _critical = True , _env = env )
770800 self .hook ("after_apk_assemble" )
771801
772802 info_main ('# Copying APK to current directory' )
@@ -784,16 +814,24 @@ def apk(self, args):
784814 suffix = args .build_mode
785815 if suffix == 'release' and not args .keystore :
786816 suffix = suffix + '-unsigned'
787- apks = glob .glob (join (dist . dist_dir , 'bin' , '*-*-{}.apk' .format (suffix )))
817+ apks = glob .glob (join (apk_dir , apk_glob .format (suffix )))
788818 if len (apks ) == 0 :
789819 raise ValueError ('Couldn\' t find the built APK' )
790820 if len (apks ) > 1 :
791- info ('More than one built APK found...guessing you '
821+ info ('More than one built APK found... guessing you '
792822 'just built {}' .format (apks [- 1 ]))
793823 apk_file = apks [- 1 ]
794824
795825 info_main ('# Found APK file: {}' .format (apk_file ))
796- shprint (sh .cp , apk_file , './' )
826+ if apk_add_version :
827+ info ('# Add version number to APK' )
828+ apk_name , apk_suffix = basename (apk_file ).split ("-" , 1 )
829+ apk_file_dest = "{}-{}-{}" .format (
830+ apk_name , build_args .version , apk_suffix )
831+ info ('# APK renamed to {}' .format (apk_file_dest ))
832+ shprint (sh .cp , apk_file , apk_file_dest )
833+ else :
834+ shprint (sh .cp , apk_file , './' )
797835
798836 @require_prebuilt_dist
799837 def create (self , args ):
0 commit comments