@@ -37,13 +37,24 @@ class AndroidBuilder(object):
37
37
GRADLE_KEY_STORE_PASS = "RELEASE_STORE_PASSWORD"
38
38
GRADLE_KEY_ALIAS_PASS = "RELEASE_KEY_PASSWORD"
39
39
40
- def __init__ (self , verbose , app_android_root , no_res , proj_obj , use_studio = False ):
40
+ GRADLE_PROP_TARGET_VERSION = 'PROP_TARGET_SDK_VERSION'
41
+ GRADLE_PROP_NDK_MODE = 'PROP_NDK_MODE'
42
+ GRADLE_PROP_APP_ABI = 'PROP_APP_ABI'
43
+ GRADLE_PROP_COMPILE_SCRIPT = 'PROP_COMPILE_SCRIPT'
44
+ GRADLE_PROP_LUA_ENCRYPT = 'PROP_LUA_ENCRYPT'
45
+ GRADLE_PROP_LUA_ENCRYPT_KEY = 'PROP_LUA_ENCRYPT_KEY'
46
+ GRADLE_PROP_LUA_ENCRYPT_SIGN = 'PROP_LUA_ENCRYPT_SIGN'
47
+
48
+ def __init__ (self , verbose , app_android_root , no_res , proj_obj , ndk_mode , app_abi , use_studio = False , gradle_support_ndk = False ):
41
49
self ._verbose = verbose
42
50
43
51
self .app_android_root = app_android_root
44
52
self ._no_res = no_res
45
53
self ._project = proj_obj
46
54
self .use_studio = use_studio
55
+ self .gradle_support_ndk = gradle_support_ndk
56
+ self .app_abi = app_abi
57
+ self .ndk_mode = ndk_mode
47
58
48
59
# check environment variable
49
60
if self .use_studio :
@@ -60,6 +71,21 @@ def _run_cmd(self, command, cwd=None):
60
71
cocos .CMDRunner .run_cmd (command , self ._verbose , cwd = cwd )
61
72
62
73
def _parse_cfg (self ):
74
+ # get the properties for sign release apk
75
+ if self .use_studio :
76
+ self .key_store_str = AndroidBuilder .GRADLE_KEY_STORE
77
+ self .key_alias_str = AndroidBuilder .GRADLE_KEY_ALIAS
78
+ self .key_store_pass_str = AndroidBuilder .GRADLE_KEY_STORE_PASS
79
+ self .key_alias_pass_str = AndroidBuilder .GRADLE_KEY_ALIAS_PASS
80
+ else :
81
+ self .key_store_str = AndroidBuilder .ANT_KEY_STORE
82
+ self .key_alias_str = AndroidBuilder .ANT_KEY_ALIAS
83
+ self .key_store_pass_str = AndroidBuilder .ANT_KEY_STORE_PASS
84
+ self .key_alias_pass_str = AndroidBuilder .ANT_KEY_ALIAS_PASS
85
+
86
+ if self .gradle_support_ndk :
87
+ return
88
+
63
89
self .cfg_path = os .path .join (self .app_android_root , BUILD_CFIG_FILE )
64
90
try :
65
91
f = open (self .cfg_path )
@@ -79,18 +105,6 @@ def _parse_cfg(self):
79
105
80
106
self .ndk_module_paths = cfg ['ndk_module_path' ]
81
107
82
- # get the properties for sign release apk
83
- if self .use_studio :
84
- self .key_store_str = AndroidBuilder .GRADLE_KEY_STORE
85
- self .key_alias_str = AndroidBuilder .GRADLE_KEY_ALIAS
86
- self .key_store_pass_str = AndroidBuilder .GRADLE_KEY_STORE_PASS
87
- self .key_alias_pass_str = AndroidBuilder .GRADLE_KEY_ALIAS_PASS
88
- else :
89
- self .key_store_str = AndroidBuilder .ANT_KEY_STORE
90
- self .key_alias_str = AndroidBuilder .ANT_KEY_ALIAS
91
- self .key_store_pass_str = AndroidBuilder .ANT_KEY_STORE_PASS
92
- self .key_alias_pass_str = AndroidBuilder .ANT_KEY_ALIAS_PASS
93
-
94
108
move_cfg = {}
95
109
self .key_store = None
96
110
if cfg .has_key (AndroidBuilder .CFG_KEY_STORE ):
@@ -169,6 +183,18 @@ def remove_c_libs(self, libs_dir):
169
183
os .remove (lib_file )
170
184
171
185
def update_project (self , android_platform ):
186
+ if self .gradle_support_ndk :
187
+ # If gradle supports ndk build, should write local.properties manually
188
+ local_porps_path = os .path .join (self .app_android_root , 'local.properties' )
189
+ lines = [
190
+ 'sdk.dir=%s\n ' % self .sdk_root ,
191
+ 'ndk.dir=%s\n ' % cocos .check_environment_variable ('NDK_ROOT' )
192
+ ]
193
+ f = open (local_porps_path , 'w' )
194
+ f .writelines (lines )
195
+ f .close ()
196
+ return
197
+
172
198
if self .use_studio :
173
199
manifest_path = os .path .join (self .app_android_root , 'app' )
174
200
else :
@@ -356,7 +382,7 @@ def ant_build_apk(self, build_mode, custom_step_args):
356
382
self ._project .invoke_custom_step_script (cocos_project .Project .CUSTOM_STEP_POST_ANT_BUILD ,
357
383
target_platform , args_ant_copy )
358
384
359
- def gradle_build_apk (self , build_mode ):
385
+ def gradle_build_apk (self , build_mode , android_platform , compile_obj ):
360
386
# check the compileSdkVersion & buildToolsVersion
361
387
check_file = os .path .join (self .app_android_root , 'app' , 'build.gradle' )
362
388
f = open (check_file )
@@ -404,6 +430,32 @@ def gradle_build_apk(self, build_mode):
404
430
405
431
mode_str = 'Debug' if build_mode == 'debug' else 'Release'
406
432
cmd = '"%s" --parallel --info assemble%s' % (gradle_path , mode_str )
433
+
434
+ if self .gradle_support_ndk :
435
+ add_props = {
436
+ AndroidBuilder .GRADLE_PROP_NDK_MODE : self .ndk_mode
437
+ }
438
+ if android_platform :
439
+ add_props [AndroidBuilder .GRADLE_PROP_TARGET_VERSION ] = android_platform
440
+
441
+ if self .app_abi :
442
+ add_props [AndroidBuilder .GRADLE_PROP_APP_ABI ] = ':' .join (self .app_abi .split (' ' ))
443
+
444
+ if self ._project ._is_script_project ():
445
+ if compile_obj ._compile_script :
446
+ add_props [AndroidBuilder .GRADLE_PROP_COMPILE_SCRIPT ] = 1
447
+ else :
448
+ add_props [AndroidBuilder .GRADLE_PROP_COMPILE_SCRIPT ] = 0
449
+
450
+ if self ._project ._is_lua_project ():
451
+ if compile_obj ._lua_encrypt :
452
+ add_props [AndroidBuilder .GRADLE_PROP_LUA_ENCRYPT ] = 1
453
+ add_props [AndroidBuilder .GRADLE_PROP_LUA_ENCRYPT_KEY ] = compile_obj ._lua_encrypt_key
454
+ add_props [AndroidBuilder .GRADLE_PROP_LUA_ENCRYPT_SIGN ] = compile_obj ._lua_encrypt_sign
455
+
456
+ for key in add_props .keys ():
457
+ cmd += ' -P%s=%s' % (key , add_props [key ])
458
+
407
459
self ._run_cmd (cmd , cwd = self .app_android_root )
408
460
409
461
class LuaBuildType :
@@ -461,7 +513,7 @@ def _get_build_type(self, param_of_appabi):
461
513
462
514
return self .LuaBuildType .UNKNOWN
463
515
464
- def do_build_apk (self , build_mode , no_apk , output_dir , custom_step_args , compile_obj ):
516
+ def do_build_apk (self , build_mode , no_apk , output_dir , custom_step_args , android_platform , compile_obj ):
465
517
if self .use_studio :
466
518
assets_dir = os .path .join (self .app_android_root , "app" , "assets" )
467
519
project_name = None
@@ -489,45 +541,44 @@ def do_build_apk(self, build_mode, no_apk, output_dir, custom_step_args, compile
489
541
project_name = self ._xml_attr (self .app_android_root , 'build.xml' , 'project' , 'name' )
490
542
gen_apk_folder = os .path .join (self .app_android_root , 'bin' )
491
543
492
- # copy resources
493
- self ._copy_resources (custom_step_args , assets_dir )
544
+ # gradle supports copy assets & compile scripts from engine 3.15
545
+ if not self .gradle_support_ndk :
546
+ # copy resources
547
+ self ._copy_resources (custom_step_args , assets_dir )
548
+
549
+ # check the project config & compile the script files
550
+ if self ._project ._is_lua_project ():
551
+ src_dir = os .path .join (assets_dir , 'src' )
552
+ build_type = self ._get_build_type (compile_obj .app_abi )
494
553
495
- # check the project config & compile the script files
496
- if self ._project ._is_lua_project ():
497
- print "generate byte code ............"
498
- src_dir = os .path .join (assets_dir , 'src' )
499
- build_type = self ._get_build_type (compile_obj .app_abi )
554
+ # only build 64bit
555
+ if build_type == self .LuaBuildType .ONLY_BUILD_64BIT :
556
+ dst_dir = os .path .join (assets_dir , 'src/64bit' )
557
+ is_compiled = compile_obj .compile_lua_scripts (src_dir , dst_dir , True )
558
+ if is_compiled :
559
+ # remove unneeded lua files
560
+ compile_obj ._remove_file_with_ext (src_dir , '.lua' )
561
+ shutil .rmtree (os .path .join (src_dir , 'cocos' ))
562
+
563
+ # only build 32bit
564
+ if build_type == self .LuaBuildType .ONLY_BUILD_32BIT :
565
+ # build 32-bit bytecode
566
+ compile_obj .compile_lua_scripts (src_dir , src_dir , False )
500
567
501
- # only build 64bit
502
- if build_type == self .LuaBuildType .ONLY_BUILD_64BIT :
503
- if build_mode == 'release' :
568
+ # build 32bit and 64bit
569
+ if build_type == self .LuaBuildType .BUILD_32BIT_AND_64BIT :
570
+ # build 64-bit bytecode
504
571
dst_dir = os .path .join (assets_dir , 'src/64bit' )
505
572
compile_obj .compile_lua_scripts (src_dir , dst_dir , True )
506
- # remove unneeded lua files
507
- compile_obj ._remove_file_with_ext (src_dir , '.lua' )
508
- shutil .rmtree (os .path .join (src_dir , 'cocos' ))
509
- else :
573
+ # build 32-bit bytecode
574
+ compile_obj .compile_lua_scripts (src_dir , src_dir , False )
575
+
576
+ if build_type == self .LuaBuildType .UNKNOWN :
577
+ # haven't set APP_ABI in parameter and Application.mk, default build 32bit
510
578
compile_obj .compile_lua_scripts (src_dir , src_dir , False )
511
579
512
- # only build 32bit
513
- if build_type == self .LuaBuildType .ONLY_BUILD_32BIT :
514
- # build 32-bit bytecode
515
- compile_obj .compile_lua_scripts (src_dir , src_dir , False )
516
-
517
- # build 32bit and 64bit
518
- if build_type == self .LuaBuildType .BUILD_32BIT_AND_64BIT :
519
- # build 64-bit bytecode
520
- dst_dir = os .path .join (assets_dir , 'src/64bit' )
521
- compile_obj .compile_lua_scripts (src_dir , dst_dir , True )
522
- # build 32-bit bytecode
523
- compile_obj .compile_lua_scripts (src_dir , src_dir , False )
524
-
525
- if build_type == self .LuaBuildType .UNKNOWN :
526
- # haven't set APP_ABI in parameter and Application.mk, default build 32bit
527
- compile_obj .compile_lua_scripts (src_dir , src_dir , False )
528
-
529
- if self ._project ._is_js_project ():
530
- compile_obj .compile_js_scripts (assets_dir , assets_dir )
580
+ if self ._project ._is_js_project ():
581
+ compile_obj .compile_js_scripts (assets_dir , assets_dir )
531
582
532
583
if not no_apk :
533
584
# gather the sign info if necessary
@@ -536,7 +587,7 @@ def do_build_apk(self, build_mode, no_apk, output_dir, custom_step_args, compile
536
587
537
588
# build apk
538
589
if self .use_studio :
539
- self .gradle_build_apk (build_mode )
590
+ self .gradle_build_apk (build_mode , android_platform , compile_obj )
540
591
else :
541
592
self .ant_build_apk (build_mode , custom_step_args )
542
593
0 commit comments