Skip to content

support for building Android app bundle #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ $ cocos new MyGame -l cpp -p org.cocos2d.mygame

$ cd MyGame

# Will deploy the project to device and run it
$ cocos run -p android
# Will deploy the project to device and run it (if '-bundle' is appended to build mode [release/debug], it'll create app bundle. otherwise, apk)
$ cocos run -p android [-m release/debug/release-bundle/debug-bundle]


```
Expand Down
13 changes: 8 additions & 5 deletions plugins/plugin_compile/build_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def check_android_platform(self, sdk_root, android_platform, proj_path):

return ret

def gradle_build_apk(self, mode, android_platform, compile_obj):
def gradle_build_apk(self, mode, android_platform, compile_obj, bundle):
# check the compileSdkVersion & buildToolsVersion
check_file = os.path.join(self.app_android_root, 'app', 'build.gradle')
f = open(check_file)
Expand Down Expand Up @@ -401,8 +401,9 @@ def gradle_build_apk(self, mode, android_platform, compile_obj):
raise cocos.CCPluginError(MultiLanguage.get_string('COMPILE_ERROR_GRALEW_NOT_EXIST_FMT', gradle_path),
cocos.CCPluginError.ERROR_PATH_NOT_FOUND)

build = 'bundle' if bundle else 'assemble'
mode_str = 'Debug' if mode == 'debug' else 'Release'
cmd = '"%s" --parallel --info assemble%s' % (gradle_path, mode_str)
cmd = '"%s" --parallel --info %s%s' % (gradle_path, build, mode_str)

if self.gradle_support_ndk:
add_props = {
Expand Down Expand Up @@ -487,7 +488,7 @@ def _get_build_arch(self, param_of_appabi):

return self.LuaBuildArch.UNKNOWN

def do_build_apk(self, mode, no_apk, no_sign, output_dir, custom_step_args, android_platform, compile_obj):
def do_build_apk(self, mode, no_apk, no_sign, output_dir, custom_step_args, android_platform, compile_obj, bundle):
assets_dir = os.path.join(self.app_android_root, "app", "assets")
project_name = None
setting_file = os.path.join(self.app_android_root, 'settings.gradle')
Expand Down Expand Up @@ -556,10 +557,12 @@ def do_build_apk(self, mode, no_apk, no_sign, output_dir, custom_step_args, andr
self._gather_sign_info()

# build apk
self.gradle_build_apk(mode, android_platform, compile_obj)
self.gradle_build_apk(mode, android_platform, compile_obj, bundle)

# copy the apk to output dir
if output_dir:
if bundle:
return
elif output_dir:
# support generate unsigned apk
if mode == "release" and no_sign:
apk_name = '%s-%s-unsigned.apk' % (project_name, mode)
Expand Down
6 changes: 4 additions & 2 deletions plugins/plugin_compile/project_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ def _add_custom_options(self, parser):

def _check_custom_options(self, args):
# get the mode parameter
available_modes = [ 'release', 'debug' ]
available_modes = [ 'release', 'debug', 'release-bundle', 'debug-bundle' ]
self._mode = self.check_param(args.mode, 'debug', available_modes,
MultiLanguage.get_string('COMPILE_ERROR_WRONG_MODE_FMT',
available_modes))
self._bundle = True if self._mode.find('bundle') != -1 else False
self._mode = self._mode.split('-')[0]

# android arguments
available_build_types = [ 'cmake','ndk-build', 'none']
Expand Down Expand Up @@ -537,7 +539,7 @@ def build_android(self):
# build apk
if not self._no_apk:
cocos.Logging.info(MultiLanguage.get_string('COMPILE_INFO_BUILD_APK'))
self.apk_path = builder.do_build_apk(build_mode, self._no_apk, self._no_sign, output_dir, self._custom_step_args, self._ap, self)
self.apk_path = builder.do_build_apk(build_mode, self._no_apk, self._no_sign, output_dir, self._custom_step_args, self._ap, self, self._bundle)
self.android_package, self.android_activity = builder.get_apk_info()

cocos.Logging.info(MultiLanguage.get_string('COMPILE_INFO_BUILD_SUCCEED'))
Expand Down
2 changes: 2 additions & 0 deletions plugins/plugin_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def deploy_android(self, dependencies):
cocos.Logging.info(MultiLanguage.get_string('DEPLOY_INFO_INSTALLING_APK'))

compile_dep = dependencies['compile']
if compile_dep._bundle:
return
self.package = compile_dep.android_package
self.activity = compile_dep.android_activity
apk_path = compile_dep.apk_path
Expand Down
4 changes: 2 additions & 2 deletions plugins/plugin_run/project_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ def run_mac(self, dependencies):
self._run_with_desktop_options(launch_macapp)

def run_android_device(self, dependencies):
if not self._platforms.is_android_active():
deploy_dep = dependencies['deploy']
if not self._platforms.is_android_active() or not hasattr(deploy_dep, 'package'):
return

sdk_root = cocos.check_environment_variable('ANDROID_SDK_ROOT')
adb_path = cocos.CMDRunner.convert_path_to_cmd(os.path.join(sdk_root, 'platform-tools', 'adb'))
deploy_dep = dependencies['deploy']
startapp = "%s shell am start -n \"%s/%s\"" % (adb_path, deploy_dep.package, deploy_dep.activity)
self._run_cmd(startapp)
pass
Expand Down