Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 2 | # Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | import glob |
| 7 | import optparse |
| 8 | import os |
| 9 | import shutil |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 10 | import sys |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 11 | |
| 12 | import apk_utils |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 13 | import utils |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 14 | import zip_utils |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 15 | |
| 16 | USAGE = 'usage: %prog [options] <apk>' |
| 17 | |
| 18 | def parse_options(): |
| 19 | parser = optparse.OptionParser(usage=USAGE) |
| 20 | parser.add_option('--dex', |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 21 | help='Directory or archive with dex files to use instead ' |
| 22 | 'of those in the apk', |
| 23 | default=None) |
| 24 | parser.add_option('--desugared-library-dex', |
| 25 | help='Path to desugared library dex file to use or archive ' |
| 26 | 'containing a single classes.dex file', |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 27 | default=None) |
| 28 | parser.add_option('--resources', |
| 29 | help=('pattern that matches resources to use instead of ' |
| 30 | + 'those in the apk'), |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 31 | default=None) |
| 32 | parser.add_option('--out', |
| 33 | help='output file (default ./$(basename <apk>))', |
| 34 | default=None) |
| 35 | parser.add_option('--keystore', |
| 36 | help='keystore file (default ~/.android/app.keystore)', |
| 37 | default=None) |
Søren Gjesse | c4b3f61 | 2017-05-24 10:10:43 +0200 | [diff] [blame] | 38 | parser.add_option('--install', |
| 39 | help='install the generated apk with adb options -t -r -d', |
| 40 | default=False, |
| 41 | action='store_true') |
Søren Gjesse | 12d41d4 | 2017-06-01 09:01:24 +0200 | [diff] [blame] | 42 | parser.add_option('--adb-options', |
| 43 | help='additional adb options when running adb', |
| 44 | default=None) |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 45 | parser.add_option('--quiet', |
| 46 | help='disable verbose logging', |
| 47 | default=False) |
Christoffer Quist Adamsen | 8c803b4 | 2022-05-31 10:36:17 +0200 | [diff] [blame] | 48 | parser.add_option('--sign-before-align', |
| 49 | help='Sign the apk before aligning', |
| 50 | default=False, |
| 51 | action='store_true') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 52 | (options, args) = parser.parse_args() |
| 53 | if len(args) != 1: |
| 54 | parser.error('Expected <apk> argument, got: ' + ' '.join(args)) |
| 55 | apk = args[0] |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 56 | return (options, apk) |
| 57 | |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 58 | def is_archive(file): |
| 59 | return file.endswith('.zip') or file.endswith('.jar') |
| 60 | |
| 61 | def repack( |
| 62 | apk, processed_out, desugared_library_dex, resources, temp, quiet, logging): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 63 | processed_apk = os.path.join(temp, 'processed.apk') |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 64 | shutil.copyfile(apk, processed_apk) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 65 | if not processed_out: |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 66 | utils.Print('Using original APK as is', quiet=quiet) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 67 | return processed_apk |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 68 | utils.Print( |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 69 | 'Repacking APK with dex files from {}'.format(processed_out), quiet=quiet) |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 70 | |
| 71 | # Delete original dex files in APK. |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 72 | with utils.ChangedWorkingDirectory(temp, quiet=quiet): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 73 | cmd = ['zip', '-d', 'processed.apk', '*.dex'] |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 74 | utils.RunCmd(cmd, quiet=quiet, logging=logging) |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 75 | |
| 76 | # Unzip the jar or zip file into `temp`. |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 77 | if is_archive(processed_out): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 78 | cmd = ['unzip', processed_out, '-d', temp] |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 79 | if quiet: |
| 80 | cmd.insert(1, '-q') |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 81 | utils.RunCmd(cmd, quiet=quiet, logging=logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 82 | processed_out = temp |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 83 | elif desugared_library_dex: |
| 84 | for dex_name in glob.glob('*.dex', root_dir=processed_out): |
| 85 | src = os.path.join(processed_out, dex_name) |
| 86 | dst = os.path.join(temp, dex_name) |
| 87 | shutil.copyfile(src, dst) |
| 88 | processed_out = temp |
| 89 | |
| 90 | if desugared_library_dex: |
| 91 | desugared_library_dex_index = len(glob.glob('*.dex', root_dir=temp)) + 1 |
| 92 | desugared_library_dex_name = 'classes%s.dex' % desugared_library_dex_index |
| 93 | desugared_library_dex_dst = os.path.join(temp, desugared_library_dex_name) |
| 94 | if is_archive(desugared_library_dex): |
| 95 | zip_utils.extract_member( |
| 96 | desugared_library_dex, 'classes.dex', desugared_library_dex_dst) |
| 97 | else: |
| 98 | shutil.copyfile(desugared_library_dex, desugared_library_dex_dst) |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 99 | |
| 100 | # Insert the new dex and resource files from `processed_out` into the APK. |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 101 | with utils.ChangedWorkingDirectory(processed_out, quiet=quiet): |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 102 | dex_files = glob.glob('*.dex') |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 103 | dex_files.sort() |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 104 | resource_files = glob.glob(resources) if resources else [] |
Rico Wind | 5e48682 | 2022-08-17 14:04:35 +0200 | [diff] [blame] | 105 | cmd = ['zip', '-u', '-0', processed_apk] + dex_files + resource_files |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 106 | utils.RunCmd(cmd, quiet=quiet, logging=logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 107 | return processed_apk |
| 108 | |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 109 | def sign(unsigned_apk, keystore, temp, quiet, logging): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 110 | signed_apk = os.path.join(temp, 'unaligned.apk') |
Christoffer Quist Adamsen | 3f7e428 | 2022-04-21 13:12:31 +0200 | [diff] [blame] | 111 | return apk_utils.sign_with_apksigner( |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 112 | unsigned_apk, signed_apk, keystore, quiet=quiet, logging=logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 113 | |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 114 | def align(signed_apk, temp, quiet, logging): |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 115 | utils.Print('Aligning', quiet=quiet) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 116 | aligned_apk = os.path.join(temp, 'aligned.apk') |
Christoffer Quist Adamsen | 3f7e428 | 2022-04-21 13:12:31 +0200 | [diff] [blame] | 117 | return apk_utils.align(signed_apk, aligned_apk) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 118 | |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 119 | def masseur( |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 120 | apk, dex=None, desugared_library_dex=None, resources=None, out=None, |
| 121 | adb_options=None, sign_before_align=False, keystore=None, install=False, |
| 122 | quiet=False, logging=True): |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 123 | if not out: |
| 124 | out = os.path.basename(apk) |
| 125 | if not keystore: |
Christoffer Quist Adamsen | 3f7e428 | 2022-04-21 13:12:31 +0200 | [diff] [blame] | 126 | keystore = apk_utils.default_keystore() |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 127 | with utils.TempDir() as temp: |
| 128 | processed_apk = None |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 129 | if dex: |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 130 | processed_apk = repack( |
| 131 | apk, dex, desugared_library_dex, resources, temp, quiet, logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 132 | else: |
Christoffer Quist Adamsen | 0aaca16 | 2023-02-27 13:02:01 +0100 | [diff] [blame^] | 133 | assert not desugared_library_dex |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 134 | utils.Print( |
| 135 | 'Signing original APK without modifying dex files', quiet=quiet) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 136 | processed_apk = os.path.join(temp, 'processed.apk') |
| 137 | shutil.copyfile(apk, processed_apk) |
Christoffer Quist Adamsen | 8c803b4 | 2022-05-31 10:36:17 +0200 | [diff] [blame] | 138 | if sign_before_align: |
| 139 | signed_apk = sign( |
| 140 | processed_apk, keystore, temp, quiet=quiet, logging=logging) |
| 141 | aligned_apk = align(signed_apk, temp, quiet=quiet, logging=logging) |
| 142 | utils.Print('Writing result to {}'.format(out), quiet=quiet) |
| 143 | shutil.copyfile(aligned_apk, out) |
| 144 | else: |
| 145 | aligned_apk = align(processed_apk, temp, quiet=quiet, logging=logging) |
| 146 | signed_apk = sign( |
| 147 | aligned_apk, keystore, temp, quiet=quiet, logging=logging) |
| 148 | utils.Print('Writing result to {}'.format(out), quiet=quiet) |
| 149 | shutil.copyfile(signed_apk, out) |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 150 | if install: |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 151 | adb_cmd = ['adb'] |
| 152 | if adb_options: |
| 153 | adb_cmd.extend( |
| 154 | [option for option in adb_options.split(' ') if option]) |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 155 | adb_cmd.extend(['install', '-t', '-r', '-d', out]); |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 156 | utils.RunCmd(adb_cmd, quiet=quiet, logging=logging) |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 157 | |
| 158 | def main(): |
| 159 | (options, apk) = parse_options() |
| 160 | masseur(apk, **vars(options)) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 161 | return 0 |
| 162 | |
| 163 | if __name__ == '__main__': |
| 164 | sys.exit(main()) |