1*333d2b36SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*333d2b36SAndroid Build Coastguard Worker 3*333d2b36SAndroid Build Coastguard Workerimport subprocess 4*333d2b36SAndroid Build Coastguard Workerimport argparse 5*333d2b36SAndroid Build Coastguard Workerimport re 6*333d2b36SAndroid Build Coastguard Workerimport sys 7*333d2b36SAndroid Build Coastguard Workerimport zipfile 8*333d2b36SAndroid Build Coastguard Worker 9*333d2b36SAndroid Build Coastguard Workerdef check_target_sdk_less_than_30(args): 10*333d2b36SAndroid Build Coastguard Worker if not args.aapt2: 11*333d2b36SAndroid Build Coastguard Worker sys.exit('--aapt2 is required') 12*333d2b36SAndroid Build Coastguard Worker regex = re.compile(r"targetSdkVersion: *'([0-9]+)'") 13*333d2b36SAndroid Build Coastguard Worker output = subprocess.check_output([args.aapt2, "dump", "badging", args.apk], text=True) 14*333d2b36SAndroid Build Coastguard Worker targetSdkVersion = None 15*333d2b36SAndroid Build Coastguard Worker for line in output.splitlines(): 16*333d2b36SAndroid Build Coastguard Worker match = regex.fullmatch(line.strip()) 17*333d2b36SAndroid Build Coastguard Worker if match: 18*333d2b36SAndroid Build Coastguard Worker targetSdkVersion = int(match.group(1)) 19*333d2b36SAndroid Build Coastguard Worker break 20*333d2b36SAndroid Build Coastguard Worker 21*333d2b36SAndroid Build Coastguard Worker if targetSdkVersion is None or targetSdkVersion >= 30: 22*333d2b36SAndroid Build Coastguard Worker 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)") 23*333d2b36SAndroid Build Coastguard Worker 24*333d2b36SAndroid Build Coastguard Workerdef has_preprocessed_issues(args, *, fail=False): 25*333d2b36SAndroid Build Coastguard Worker if not args.zipalign: 26*333d2b36SAndroid Build Coastguard Worker sys.exit('--zipalign is required') 27*333d2b36SAndroid Build Coastguard Worker ret = subprocess.run([args.zipalign, '-c', '-p', '4', args.apk], stdout=subprocess.DEVNULL).returncode 28*333d2b36SAndroid Build Coastguard Worker if ret != 0: 29*333d2b36SAndroid Build Coastguard Worker if fail: 30*333d2b36SAndroid Build Coastguard Worker sys.exit(args.apk + ': Improper zip alignment') 31*333d2b36SAndroid Build Coastguard Worker return True 32*333d2b36SAndroid Build Coastguard Worker 33*333d2b36SAndroid Build Coastguard Worker with zipfile.ZipFile(args.apk) as zf: 34*333d2b36SAndroid Build Coastguard Worker for info in zf.infolist(): 35*333d2b36SAndroid Build Coastguard Worker if info.filename.startswith('lib/') and info.filename.endswith('.so') and info.compress_type != zipfile.ZIP_STORED: 36*333d2b36SAndroid Build Coastguard Worker if fail: 37*333d2b36SAndroid Build Coastguard Worker sys.exit(args.apk + ': Contains compressed JNI libraries') 38*333d2b36SAndroid Build Coastguard Worker return True 39*333d2b36SAndroid Build Coastguard Worker # It's ok for non-privileged apps to have compressed dex files 40*333d2b36SAndroid Build Coastguard Worker if args.privileged and args.uncompress_priv_app_dex: 41*333d2b36SAndroid Build Coastguard Worker if info.filename.endswith('.dex') and info.compress_type != zipfile.ZIP_STORED: 42*333d2b36SAndroid Build Coastguard Worker if fail: 43*333d2b36SAndroid Build Coastguard Worker sys.exit(args.apk + ': Contains compressed dex files and is privileged') 44*333d2b36SAndroid Build Coastguard Worker return True 45*333d2b36SAndroid Build Coastguard Worker return False 46*333d2b36SAndroid Build Coastguard Worker 47*333d2b36SAndroid Build Coastguard Worker 48*333d2b36SAndroid Build Coastguard Workerdef main(): 49*333d2b36SAndroid Build Coastguard Worker # This script enforces requirements for presigned apps as documented in: 50*333d2b36SAndroid Build Coastguard Worker # go/gms-uncompressed-jni-slides 51*333d2b36SAndroid Build Coastguard Worker # https://docs.partner.android.com/gms/building/integrating/jni-libs 52*333d2b36SAndroid Build Coastguard Worker # https://docs.partner.android.com/gms/policies/domains/mba#jni-lib 53*333d2b36SAndroid Build Coastguard Worker parser = argparse.ArgumentParser() 54*333d2b36SAndroid Build Coastguard Worker parser.add_argument('--aapt2', help = "the path to the aapt2 executable") 55*333d2b36SAndroid Build Coastguard Worker parser.add_argument('--zipalign', help = "the path to the zipalign executable") 56*333d2b36SAndroid Build Coastguard Worker parser.add_argument('--skip-preprocessed-apk-checks', action = 'store_true', help = "the value of the soong property with the same name") 57*333d2b36SAndroid Build Coastguard Worker parser.add_argument('--preprocessed', action = 'store_true', help = "the value of the soong property with the same name") 58*333d2b36SAndroid Build Coastguard Worker parser.add_argument('--privileged', action = 'store_true', help = "the value of the soong property with the same name") 59*333d2b36SAndroid Build Coastguard Worker parser.add_argument('--uncompress-priv-app-dex', action = 'store_true', help = "the value of the product variable with the same name") 60*333d2b36SAndroid Build Coastguard Worker parser.add_argument('apk', help = "the apk to check") 61*333d2b36SAndroid Build Coastguard Worker parser.add_argument('stampfile', help = "a file to touch if successful") 62*333d2b36SAndroid Build Coastguard Worker args = parser.parse_args() 63*333d2b36SAndroid Build Coastguard Worker 64*333d2b36SAndroid Build Coastguard Worker if not args.preprocessed: 65*333d2b36SAndroid Build Coastguard Worker check_target_sdk_less_than_30(args) 66*333d2b36SAndroid Build Coastguard Worker elif args.skip_preprocessed_apk_checks: 67*333d2b36SAndroid Build Coastguard Worker if not has_preprocessed_issues(args): 68*333d2b36SAndroid Build Coastguard Worker sys.exit('This module sets `skip_preprocessed_apk_checks: true`, but does not actually have any issues. Please remove `skip_preprocessed_apk_checks`.') 69*333d2b36SAndroid Build Coastguard Worker else: 70*333d2b36SAndroid Build Coastguard Worker has_preprocessed_issues(args, fail=True) 71*333d2b36SAndroid Build Coastguard Worker 72*333d2b36SAndroid Build Coastguard Worker subprocess.check_call(["touch", args.stampfile]) 73*333d2b36SAndroid Build Coastguard Worker 74*333d2b36SAndroid Build Coastguard Workerif __name__ == "__main__": 75*333d2b36SAndroid Build Coastguard Worker main() 76