diff options
author | Cole Faust <colefaust@google.com> | 2023-09-13 22:38:33 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2023-09-13 22:38:33 +0000 |
commit | 3835509f95b1a12d0d6e5bf32e2e1680450b193b (patch) | |
tree | fb3edbb903057eb6edb3a01340cb0342e31e4dd0 /scripts | |
parent | ece322f64024fe41489edb3529462d64c959cb6b (diff) | |
parent | 9c5c09f0ad6ac83104dae86ad719a46fb3ea91c4 (diff) | |
download | soong-3835509f95b1a12d0d6e5bf32e2e1680450b193b.tar.gz |
Merge "Error out if skip_preprocessed_apk_checks is set when it's not necessary" into main
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/check_prebuilt_presigned_apk.py | 70 | ||||
-rwxr-xr-x | scripts/check_target_sdk_less_than_30.py | 30 |
2 files changed, 70 insertions, 30 deletions
diff --git a/scripts/check_prebuilt_presigned_apk.py b/scripts/check_prebuilt_presigned_apk.py new file mode 100755 index 000000000..abedfb770 --- /dev/null +++ b/scripts/check_prebuilt_presigned_apk.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import subprocess +import argparse +import re +import sys +import zipfile + +def check_target_sdk_less_than_30(args): + if not args.aapt2: + sys.exit('--aapt2 is required') + regex = re.compile(r"targetSdkVersion: *'([0-9]+)'") + output = subprocess.check_output([args.aapt2, "dump", "badging", args.apk], text=True) + targetSdkVersion = None + for line in output.splitlines(): + match = regex.fullmatch(line.strip()) + if match: + targetSdkVersion = int(match.group(1)) + break + + if targetSdkVersion is None or targetSdkVersion >= 30: + sys.exit(args.apk + ": Prebuilt, presigned apks with targetSdkVersion >= 30 (or a codename targetSdkVersion) must set preprocessed: true in the Android.bp definition (because they must be signed with signature v2, and the build system would wreck that signature otherwise)") + +def has_preprocessed_issues(args, *, fail=False): + if not args.zipalign: + sys.exit('--zipalign is required') + ret = subprocess.run([args.zipalign, '-c', '-p', '4', args.apk], stdout=subprocess.DEVNULL).returncode + if ret != 0: + if fail: + sys.exit(args.apk + ': Improper zip alignment') + return True + + with zipfile.ZipFile(args.apk) as zf: + for info in zf.infolist(): + if info.filename.startswith('lib/') and info.filename.endswith('.so') and info.compress_type != zipfile.ZIP_STORED: + if fail: + sys.exit(args.apk + ': Contains compressed JNI libraries') + return True + # It's ok for non-privileged apps to have compressed dex files, see go/gms-uncompressed-jni-slides + if args.privileged: + if info.filename.endswith('.dex') and info.compress_type != zipfile.ZIP_STORED: + if fail: + sys.exit(args.apk + ': Contains compressed dex files and is privileged') + return True + return False + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--aapt2', help = "the path to the aapt2 executable") + parser.add_argument('--zipalign', help = "the path to the zipalign executable") + parser.add_argument('--skip-preprocessed-apk-checks', action = 'store_true', help = "the value of the soong property with the same name") + parser.add_argument('--preprocessed', action = 'store_true', help = "the value of the soong property with the same name") + parser.add_argument('--privileged', action = 'store_true', help = "the value of the soong property with the same name") + parser.add_argument('apk', help = "the apk to check") + parser.add_argument('stampfile', help = "a file to touch if successful") + args = parser.parse_args() + + if not args.preprocessed: + check_target_sdk_less_than_30(args) + elif args.skip_preprocessed_apk_checks: + if not has_preprocessed_issues(args): + sys.exit('This module sets `skip_preprocessed_apk_checks: true`, but does not actually have any issues. Please remove `skip_preprocessed_apk_checks`.') + else: + has_preprocessed_issues(args, fail=True) + + subprocess.check_call(["touch", args.stampfile]) + +if __name__ == "__main__": + main() diff --git a/scripts/check_target_sdk_less_than_30.py b/scripts/check_target_sdk_less_than_30.py deleted file mode 100755 index 69b0bf011..000000000 --- a/scripts/check_target_sdk_less_than_30.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python3 - -import subprocess -import argparse -import re -import sys - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument('aapt2', help = "the path to the aapt2 executable") - parser.add_argument('apk', help = "the apk to check") - parser.add_argument('stampfile', help = "a file to touch if successful") - args = parser.parse_args() - - regex = re.compile(r"targetSdkVersion: *'([0-9]+)'") - output = subprocess.check_output([args.aapt2, "dump", "badging", args.apk], text=True) - targetSdkVersion = None - for line in output.splitlines(): - match = regex.fullmatch(line.strip()) - if match: - targetSdkVersion = int(match.group(1)) - break - - if targetSdkVersion is None or targetSdkVersion >= 30: - sys.exit(args.apk + ": Prebuilt, presigned apks with targetSdkVersion >= 30 (or a codename targetSdkVersion) must set preprocessed: true in the Android.bp definition (because they must be signed with signature v2, and the build system would wreck that signature otherwise)") - - subprocess.check_call(["touch", args.stampfile]) - -if __name__ == "__main__": - main() |