# Skip building userdata.img and cache.img when signing the target files. new_args = ["--is_signing", "--add_missing"] # add_img_to_target_files builds the system image from scratch, so the # recovery patch is guaranteed to be regenerated there. if OPTIONS.rebuild_recovery: new_args.append("--rebuild_recovery") new_args.append(args[1]) add_img_to_target_files.main(new_args)
如果需要使用-k选项 估计要这样填写了 -k platform=build/target/product/security 然后追加进key_mapping_options的 defBuildKeyMap(misc_info, key_mapping_options): for s, d in key_mapping_options: if s isNone: # -d option devkey = misc_info.get("default_system_dev_certificate", "build/target/product/security/testkey") devkeydir = os.path.dirname(devkey)
OPTIONS.key_map.update({ devkeydir + "/testkey": d + "/releasekey", devkeydir + "/devkey": d + "/releasekey", devkeydir + "/releasekey": d + "/releasekey", devkeydir + "/media": d + "/media", devkeydir + "/shared": d + "/shared", devkeydir + "/platform": d + "/platform", "device/qcom/sepolicy/generic/vendor/timeservice" + "/timeservice_app_cert" : d + "/timeservice", }) else: OPTIONS.key_map[s] = d
Parses the APK certs info from a given target-files zip 解析 target files zip文件。获取apk的certs 信息
其实就是 读取META/apkcerts.txt ,解析其中的每一行
defReadApkCerts(tf_zip): """Parses the APK certs info from a given target-files zip. Given a target-files ZipFile, parses the META/apkcerts.txt entry and returns a tuple with the following elements: (1) a dictionary that maps packages to certs (based on the "certificate" and "private_key" attributes in the file; (2) a string representing the extension of compressed APKs in the target files (e.g ".gz", ".bro"). Args: tf_zip: The input target_files ZipFile (already open). Returns: (certmap, ext): certmap is a dictionary that maps packages to certs; ext is the extension string of compressed APKs (e.g. ".gz"), or None if there's no compressed APKs. """ certmap = {} compressed_extension = None
# META/apkcerts.txt contains the info for _all_ the packages known at build # time. Filter out the ones that are not installed. installed_files = set() for name in tf_zip.namelist(): basename = os.path.basename(name) if basename: installed_files.add(basename) # 遍历zip文件中的所有文件,放到一个 set中。这样就不会有重复的文件名了。
for line in tf_zip.read("META/apkcerts.txt").split("\n"): # 直接读取 META/apkcerts.txt 文件按行分割然后遍历没一行 line = line.strip()# 去除多余空格指令的字符 ifnot line: # 如果上空行 跳过 continue m = re.match( # 这里初始化 匹配 每一行 内容的正则, 其中就是用这个正则来 解析了每一行的对应内容了 r'^name="(?P<NAME>.*)"\s+certificate="(?P<CERT>.*)"\s+' r'private_key="(?P<PRIVKEY>.*?)"(\s+compressed="(?P<COMPRESSED>.*)")?$', line) ifnot m: # 如果没用匹配 就跳过 continue # name="AaptAutoVersionTest.apk" certificate="build/target/product/security/testkey.x509.pem" private_key="build/target/product/security/testkey.pk8"
public_key_suffix_len = len(OPTIONS.public_key_suffix) private_key_suffix_len = len(OPTIONS.private_key_suffix) if cert in SPECIAL_CERT_STRINGS andnot privkey: certmap[name] = cert elif (cert.endswith(OPTIONS.public_key_suffix) and privkey.endswith(OPTIONS.private_key_suffix) and cert[:-public_key_suffix_len] == privkey[:-private_key_suffix_len]): certmap[name] = cert[:-public_key_suffix_len] else: raise ValueError("Failed to parse line from apkcerts.txt:\n" + line)
ifnot this_compressed_extension: continue
# Only count the installed files. filename = name + '.' + this_compressed_extension if filename notin installed_files: continue
# Make sure that all the values in the compression map have the same # extension. We don't support multiple compression methods in the same # system image. if compressed_extension: if this_compressed_extension != compressed_extension: raise ValueError( "Multiple compressed extensions: {} vs {}".format( compressed_extension, this_compressed_extension)) else: compressed_extension = this_compressed_extension
return (certmap, ("." + compressed_extension) if compressed_extension elseNone)
defGetApkCerts(certmap): # apply the key remapping to the contents of the file for apk, cert in certmap.iteritems(): certmap[apk] = OPTIONS.key_map.get(cert, cert)
# apply all the -e options, overriding anything in the file for apk, cert in OPTIONS.extra_apks.iteritems(): ifnot cert: cert = "PRESIGNED" certmap[apk] = OPTIONS.key_map.get(cert, cert)
defCheckApkAndApexKeysAvailable(input_tf_zip, known_keys, compressed_extension, apex_keys): unknown_files = [] for info in input_tf_zip.infolist(): if (info.filename.startswith('SYSTEM/apex') and info.filename.endswith('.apex')): name = os.path.basename(info.filename) if name notin known_keys: unknown_files.append(name) continue
# And APKs. (is_apk, is_compressed, should_be_skipped) = GetApkFileInfo(info.filename, compressed_extension, OPTIONS.skip_apks_with_path_prefix) ifnot is_apk or should_be_skipped: continue
name = os.path.basename(info.filename) if is_compressed: name = name[:-len(compressed_extension)] if name notin known_keys: unknown_files.append(name)
assertnot unknown_files, \ ("No key specified for:\n {}\n" "Use '-e <apkname>=' to specify a key (which may be an empty string to " "not sign this apk).".format("\n ".join(unknown_files)))