def add_to_zip(zip_path, data, compress=True, alignment=4): zipalign.AddToZipHermetic( out_apk, zip_path, data=data, compress=compress, alignment=0 if compress and not fast_align else alignment)
def _AddNativeLibraries(out_apk, native_libs, android_abi, uncompress, fast_align): """Add native libraries to APK.""" has_crazy_linker = any('android_linker' in os.path.basename(p) for p in native_libs) has_monochrome = any('monochrome' in os.path.basename(p) for p in native_libs) for path in native_libs: basename = os.path.basename(path) compress = None if uncompress and os.path.splitext(basename)[1] == '.so': # Trichrome if has_crazy_linker and has_monochrome: compress = False elif ( 'android_linker' not in basename and (not has_crazy_linker or 'clang_rt' not in basename) and (not has_crazy_linker or 'crashpad_handler' not in basename)): compress = False if has_crazy_linker and not has_monochrome: basename = 'crazy.' + basename apk_path = 'lib/%s/%s' % (android_abi, basename) zipalign.AddToZipHermetic( out_apk, apk_path, src_path=path, compress=compress, alignment=0 if compress and not fast_align else 0x1000)
def _AddAssets(apk, path_tuples, fast_align, disable_compression=False): """Adds the given paths to the apk. Args: apk: ZipFile to write to. paths: List of paths (with optional :zipPath suffix) to add. disable_compression: Whether to disable compression. """ # Group all uncompressed assets together in the hope that it will increase # locality of mmap'ed files. for target_compress in (False, True): for src_path, dest_path in path_tuples: compress = not disable_compression and ( os.path.splitext(src_path)[1] not in _NO_COMPRESS_EXTENSIONS) if target_compress == compress: # AddToZipHermetic() uses this logic to avoid growing small files. # We need it here in order to set alignment correctly. if compress and os.path.getsize(src_path) < 16: compress = False apk_path = 'assets/' + dest_path try: apk.getinfo(apk_path) # Should never happen since write_build_config.py handles merging. raise Exception( 'Multiple targets specified the asset path: %s' % apk_path) except KeyError: zipalign.AddToZipHermetic( apk, apk_path, src_path=src_path, compress=compress, alignment=0 if compress and not fast_align else 4)
def _ZipAligned(dex_files, output_path): """Creates a .dex.jar with 4-byte aligned files. Args: dex_files: List of dex files. output_path: The output file in which to write the zip. """ with zipfile.ZipFile(output_path, 'w') as z: for i, dex_file in enumerate(dex_files): name = 'classes{}.dex'.format(i + 1 if i > 0 else '') zipalign.AddToZipHermetic(z, name, src_path=dex_file, alignment=4)
def _MakeApk(self, apk, libs, apk_dir, out_dir, crazy): apk_file = os.path.join(apk_dir, apk) with zipfile.ZipFile(apk_file, 'w') as archive: for lib in libs: # Make an ELF-format .so file. The fake symbolizer will fudge functions # for libraries that exist. library_file = os.path.join(out_dir, lib) self._MakeElf(library_file) # Add the library to the APK. name_in_apk = 'crazy.' + lib if crazy else lib zipalign.AddToZipHermetic(archive, name_in_apk, src_path=library_file, compress=False, alignment=0x1000)
def _AddNativeLibraries(out_apk, native_libs, android_abi, uncompress, fast_align): """Add native libraries to APK. Returns: A list of (apk_path, compress, alignment) tuple representing what and how native libraries are added. """ added_libraries = [] has_crazy_linker = any( 'android_linker' in os.path.basename(p) for p in native_libs) has_monochrome = any('monochrome' in os.path.basename(p) for p in native_libs) for path in native_libs: basename = os.path.basename(path) compress = True if uncompress and os.path.splitext(basename)[1] == '.so': # Trichrome if has_crazy_linker and has_monochrome: compress = False elif ('android_linker' not in basename and (not has_crazy_linker or 'clang_rt' not in basename) and (not has_crazy_linker or 'crashpad_handler' not in basename)): compress = False if has_crazy_linker and not has_monochrome: basename = 'crazy.' + basename lib_android_abi = android_abi if path.startswith('android_clang_arm64_hwasan/'): lib_android_abi = 'arm64-v8a-hwasan' apk_path = 'lib/%s/%s' % (lib_android_abi, basename) alignment = 0 if compress and not fast_align else 0x1000 zipalign.AddToZipHermetic( out_apk, apk_path, src_path=path, compress=compress, alignment=alignment) added_libraries.append((apk_path, compress, alignment)) return added_libraries
def _AddFiles(apk, details): """Adds files to the apk. Args: apk: path to APK to add to. details: A list of file detail tuples (src_path, apk_path, compress, alignment) representing what and how files are added to the APK. """ for apk_path, src_path, compress, alignment in details: # This check is only relevant for assets, but it should not matter if it is # checked for the whole list of files. try: apk.getinfo(apk_path) # Should never happen since write_build_config.py handles merging. raise Exception('Multiple targets specified the asset path: %s' % apk_path) except KeyError: zipalign.AddToZipHermetic(apk, apk_path, src_path=src_path, compress=compress, alignment=alignment)