def test_sign_apk(): src_file = r'E:\temp\apk\3.5.4\3.5.4beta_p_01-487-20181206_sec_2.apk' dst_file = r'E:\temp\apk\3.5.4\3.5.4beta_p_01-487-20181206_sec_3.apk' keystore = r'D:\auto_build\pytxxy\project\develop\Android\pytxxy\pycreditKeystore' storepass = '******' storealias = 'pycreditKeystoreAlias' apk_util.sign_apk(keystore, storepass, storealias, src_file, dst_file)
def _protect_file(self, main_prj_path): ip = self.pro_build_config[BuilderLabel.PROTECT_FLAG][BuilderLabel.IP_FLAG] user_name = self.pro_build_config[BuilderLabel.PROTECT_FLAG][BuilderLabel.USER_FLAG] api_key = self.pro_build_config[BuilderLabel.PROTECT_FLAG][BuilderLabel.API_KEY_FLAG] api_secret = self.pro_build_config[BuilderLabel.PROTECT_FLAG][BuilderLabel.API_SECRET_FLAG] protected_path = protect_app.protect(ip, user_name, api_key, api_secret, self.apk_output_path) if self.to_align: aligned_path = file_util.get_middle_path(protected_path) apk_util.zipalign(protected_path, aligned_path) to_sign_path = aligned_path else: to_sign_path = protected_path keystore = os.path.join(main_prj_path, self.pro_build_config[BuilderLabel.SIGNER_FLAG][ BuilderLabel.KEYSTORE_FLAG]) keystore = os.path.abspath(file_util.normalpath(keystore)) storepass = self.pro_build_config[BuilderLabel.SIGNER_FLAG][BuilderLabel.STOREPASS_FLAG] storealias = self.pro_build_config[BuilderLabel.SIGNER_FLAG][BuilderLabel.STOREALIAS_FLAG] signed_path = apk_util.get_default_signed_path(protected_path) rtn = apk_util.sign_apk(keystore, storepass, storealias, to_sign_path, signed_path) if rtn: str_info = 'Protect {} and sign success.'.format(self.apk_output_path) source_name = os.path.basename(signed_path) else: str_info = 'Protect {} and sign failed!'.format(self.apk_output_path) raise Exception(str_info) print(str_info) return source_name
def process(self): parent_dir = os.path.dirname(self.dst) if not os.path.isdir(parent_dir): os.makedirs(parent_dir) rtn = apk_util.sign_apk(self.keystore, self.storepass, self.storealias, self.src, self.dst) if rtn: print(f'Sign {self.src} success and the result is {self.dst}.') else: print(f'Sign {self.src} failed!')
def process(self): git_flag = 'use_git' if not hasattr(self, git_flag): self.use_git = False code_url = self.ori_build_config[BuildConfigParser.ROOT_FLAG][ BuildConfigParser.CODE_URL_FLAG] static_config_path = self.work_path + os.sep + self.ori_build_config[ BuildConfigParser.ROOT_FLAG][BuildConfigParser.STATIC_FLAG] static_config_path = file_util.normalpath(static_config_path) main_prj_name = self.ori_build_config[ BuildConfigParser.WORKSPACE_FLAG][BuildConfigParser.MAIN_FLAG] if self.use_git: self.project_code_path = os.path.join(self.project_path, self.branch) else: self.project_code_path = self.project_path # 进行代码更新操作 if self.to_update: if self.use_git: git.checkout_or_update(self.project_code_path, code_url, self.code_ver, self.branch) else: # 根据参数配置svn用户名和密码 username_flag = 'svn_user' password_flag = 'svn_pwd' if hasattr(self, username_flag) and hasattr( self, password_flag): # print('{}: {}'.format(username_flag, self.svn_user)) # print('{}: {}'.format(password_flag, self.svn_pwd)) svn.set_user_info(getattr(self, username_flag), getattr(self, password_flag)) # 先checkout svn库上代码,再更新和本地相关的配置文件 checkout_or_update_single_item(self.project_code_path, code_url, revision=self.code_ver) # 更新gradle编译配置配置,新框架不再需要该步骤(20161110 17:39) # gradle_setting_name = 'settings.gradle' # gradle_setting_path = static_config_path + os.sep + gradle_setting_name # update_gradle_setting(main_prj_path, gradle_setting_path) # 设置配置信息并获取当前代码版本号 if self.use_git: self.prj_root = git.get_git_root(self.project_code_path) main_prj_path = os.path.join(self.prj_root, main_prj_name) self.code_ver = git.get_revision(self.prj_root) else: self.prj_root = self.project_code_path main_prj_path = os.path.join(self.project_code_path, main_prj_name) self.code_ver = svn.get_revision(self.project_code_path) print('current code version is ' + str(self.code_ver)) if self.to_update: # 更新android sdk本地配置 local_file_name = 'local.properties' local_build_file = os.path.join(static_config_path, local_file_name) target_build_file = os.path.join(self.prj_root, local_file_name) file_util.replace_file(local_build_file, target_build_file) # 进行版本编译操作 if self.to_build: to_check_vals = ['ver_name', 'ver_code', 'ver_env'] for name in to_check_vals: value = getattr(self, name) if not value: info = 'Please specify the {}.'.format(name) print(info) exit(1) # 参数非空判断验证通过开始进行正式业务逻辑 self.pro_build_config = self._get_pro_build_config() self.pre_build() if self.is_debug: mode_flag = apk_builder.DEBUG_FLAG else: mode_flag = apk_builder.RELEASE_FLAG mode = apk_builder.FLAG_MODE_MAP[mode_flag] workspace_map = self.ori_build_config[ BuildConfigParser.WORKSPACE_FLAG] if BuildConfigParser.CHANNEL_INFO_FLAG in workspace_map: res_path = self.work_path + os.sep + workspace_map[ BuildConfigParser.CHANNEL_INFO_FLAG] else: res_path = None if hasattr(self, 'channel_file') and hasattr(self, 'channel'): if self.channel_file: if not os.path.exists(self.channel_file): print('not exist {}'.format(self.channel_file)) exit(1) self.build_app(main_prj_path, self.channel_file, res_path, self.pro_build_config) else: if self.channel: chan_id = self.channel else: chan_id = ProjectBuilder.DEFAULT_CHAN self.build_app(main_prj_path, None, res_path, self.pro_build_config, chan_id, mode) else: chan_id = ProjectBuilder.DEFAULT_CHAN self.build_app(main_prj_path, None, res_path, self.pro_build_config, chan_id, mode) if os.path.exists(self.apk_output_path) and os.path.isfile( self.apk_output_path): # 将编译信息写文件 build_info_format = self.ori_build_config[ BuildConfigParser.BUILD_INFO_TEMPLET_FLAG] build_info = build_info_format.format(ver_name=self.ver_name, code_ver=self.code_ver, ver_code=self.ver_code) build_info_path = self.output_directory + os.sep + 'readme-{}-{}.txt'.format( self.ver_name, self.ver_code) file_util.write_to_file(build_info_path, build_info, encoding='utf-8') target_name = os.path.basename(self.apk_output_path) to_upload_path = os.path.dirname(self.apk_output_path) source_name = os.path.basename(self.apk_output_path) # 有需要则先加固,再签名 if ProjectBuilder.PROTECT_FLAG in self.pro_build_config and ProjectBuilder.SIGNER_FLAG in self.pro_build_config: env_mode = self.pro_build_config[ ProjectBuilder.ENV_MODE_FLAG] if self.pro_build_config[ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.IS_NEED_FLAG][env_mode]: ip = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.IP_FLAG] user_name = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.USER_FLAG] api_key = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.API_KEY_FLAG] api_secret = self.pro_build_config[ ProjectBuilder.PROTECT_FLAG][ ProjectBuilder.API_SECRET_FLAG] protected_path = protect_app.protect( ip, user_name, api_key, api_secret, self.apk_output_path) if hasattr(self, 'to_align') and self.to_align: aligned_path = file_util.get_middle_path( protected_path) apk_util.zipalign(protected_path, aligned_path) to_sign_path = aligned_path else: to_sign_path = protected_path keystore = os.path.join( main_prj_path, self.pro_build_config[ProjectBuilder.SIGNER_FLAG][ ProjectBuilder.KEYSTORE_FLAG]) storepass = self.pro_build_config[ ProjectBuilder.SIGNER_FLAG][ ProjectBuilder.STOREPASS_FLAG] storealias = self.pro_build_config[ ProjectBuilder.SIGNER_FLAG][ ProjectBuilder.STOREALIAS_FLAG] signed_path = apk_util.get_default_signed_path( protected_path) rtn = apk_util.sign_apk(keystore, storepass, storealias, to_sign_path, signed_path) if rtn: str_info = 'Protect {} and sign success.'.format( self.apk_output_path) source_name = os.path.basename(signed_path) else: str_info = 'Protect {} and sign failed!'.format( self.apk_output_path) raise Exception(str_info) print(str_info) str_info = 'Build success, code version is {}.'.format( self.code_ver) print(str_info) # 进行编译好的版本提交操作 if hasattr(self, 'to_upload') and self.to_upload: result = re.match(BuildManager.__APK_OUTPUT_PATH_PATTERN, target_name) if result: ver_name_info = result.group(1) else: str_info = 'The output file name {} is invalid!'.format( target_name) raise Exception(str_info) # ftp_config_path = os.path.join(self.work_path, 'config') ftp_config_path = self.work_path print('ver_name_info:', ver_name_info) print('target_name: ', target_name) print('source_name:', source_name) ftp_upload.upload_to_sftp(ftp_config_path, ver_name_info, self.ver_env, self.code_ver, to_upload_path, 'Android', target_name, source_name) else: str_info = 'Build failed, code version is {}.'.format( self.code_ver) print(str_info)