Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 6 | import apk_utils |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 7 | import glob |
| 8 | import optparse |
| 9 | import os |
| 10 | import shutil |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 11 | import sys |
| 12 | import utils |
| 13 | |
| 14 | USAGE = 'usage: %prog [options] <apk>' |
| 15 | |
| 16 | def parse_options(): |
| 17 | parser = optparse.OptionParser(usage=USAGE) |
| 18 | parser.add_option('--dex', |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 19 | help=('directory with dex files to use instead of those in ' |
| 20 | + 'the apk'), |
| 21 | default=None) |
| 22 | parser.add_option('--resources', |
| 23 | help=('pattern that matches resources to use instead of ' |
| 24 | + 'those in the apk'), |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 25 | default=None) |
| 26 | parser.add_option('--out', |
| 27 | help='output file (default ./$(basename <apk>))', |
| 28 | default=None) |
| 29 | parser.add_option('--keystore', |
| 30 | help='keystore file (default ~/.android/app.keystore)', |
| 31 | default=None) |
Søren Gjesse | c4b3f61 | 2017-05-24 10:10:43 +0200 | [diff] [blame] | 32 | parser.add_option('--install', |
| 33 | help='install the generated apk with adb options -t -r -d', |
| 34 | default=False, |
| 35 | action='store_true') |
Søren Gjesse | 12d41d4 | 2017-06-01 09:01:24 +0200 | [diff] [blame] | 36 | parser.add_option('--adb-options', |
| 37 | help='additional adb options when running adb', |
| 38 | default=None) |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 39 | parser.add_option('--quiet', |
| 40 | help='disable verbose logging', |
| 41 | default=False) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 42 | (options, args) = parser.parse_args() |
| 43 | if len(args) != 1: |
| 44 | parser.error('Expected <apk> argument, got: ' + ' '.join(args)) |
| 45 | apk = args[0] |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 46 | return (options, apk) |
| 47 | |
| 48 | def findKeystore(): |
| 49 | return os.path.join(os.getenv('HOME'), '.android', 'app.keystore') |
| 50 | |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 51 | def repack(apk, processed_out, resources, temp, quiet, logging): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 52 | processed_apk = os.path.join(temp, 'processed.apk') |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 53 | shutil.copyfile(apk, processed_apk) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 54 | if not processed_out: |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 55 | utils.Print('Using original APK as is', quiet=quiet) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 56 | return processed_apk |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 57 | utils.Print( |
| 58 | 'Repacking APK with dex files from {}'.format(processed_apk), quiet=quiet) |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 59 | |
| 60 | # Delete original dex files in APK. |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 61 | with utils.ChangedWorkingDirectory(temp, quiet=quiet): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 62 | cmd = ['zip', '-d', 'processed.apk', '*.dex'] |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 63 | utils.RunCmd(cmd, quiet=quiet, logging=logging) |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 64 | |
| 65 | # Unzip the jar or zip file into `temp`. |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 66 | if processed_out.endswith('.zip') or processed_out.endswith('.jar'): |
| 67 | cmd = ['unzip', processed_out, '-d', temp] |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 68 | if quiet: |
| 69 | cmd.insert(1, '-q') |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 70 | utils.RunCmd(cmd, quiet=quiet, logging=logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 71 | processed_out = temp |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 72 | |
| 73 | # 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] | 74 | with utils.ChangedWorkingDirectory(processed_out, quiet=quiet): |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 75 | dex_files = glob.glob('*.dex') |
| 76 | resource_files = glob.glob(resources) if resources else [] |
| 77 | cmd = ['zip', '-u', '-9', processed_apk] + dex_files + resource_files |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 78 | utils.RunCmd(cmd, quiet=quiet, logging=logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 79 | return processed_apk |
| 80 | |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 81 | def sign(unsigned_apk, keystore, temp, quiet, logging): |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 82 | signed_apk = os.path.join(temp, 'unaligned.apk') |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 83 | apk_utils.sign_with_apksigner( |
| 84 | unsigned_apk, signed_apk, keystore, quiet=quiet, logging=logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 85 | return signed_apk |
| 86 | |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 87 | def align(signed_apk, temp, quiet, logging): |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 88 | utils.Print('Aligning', quiet=quiet) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 89 | aligned_apk = os.path.join(temp, 'aligned.apk') |
Morten Krogh-Jespersen | c8efedd | 2019-01-28 11:36:17 +0100 | [diff] [blame] | 90 | zipalign_path = ( |
| 91 | 'zipalign' if 'build_tools' in os.environ.get('PATH') |
Morten Krogh-Jespersen | 220e570 | 2019-02-27 12:57:01 +0100 | [diff] [blame] | 92 | else os.path.join(utils.getAndroidBuildTools(), 'zipalign')) |
Morten Krogh-Jespersen | c8efedd | 2019-01-28 11:36:17 +0100 | [diff] [blame] | 93 | cmd = [ |
| 94 | zipalign_path, |
| 95 | '-f', |
| 96 | '4', |
| 97 | signed_apk, |
| 98 | aligned_apk |
| 99 | ] |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 100 | utils.RunCmd(cmd, quiet=quiet, logging=logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 101 | return signed_apk |
| 102 | |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 103 | def masseur( |
Christoffer Quist Adamsen | 2dcea10 | 2019-02-20 11:31:38 +0100 | [diff] [blame] | 104 | apk, dex=None, resources=None, out=None, adb_options=None, keystore=None, |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 105 | install=False, quiet=False, logging=True): |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 106 | if not out: |
| 107 | out = os.path.basename(apk) |
| 108 | if not keystore: |
| 109 | keystore = findKeystore() |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 110 | with utils.TempDir() as temp: |
| 111 | processed_apk = None |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 112 | if dex: |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 113 | processed_apk = repack(apk, dex, resources, temp, quiet, logging) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 114 | else: |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 115 | utils.Print( |
| 116 | 'Signing original APK without modifying dex files', quiet=quiet) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 117 | processed_apk = os.path.join(temp, 'processed.apk') |
| 118 | shutil.copyfile(apk, processed_apk) |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 119 | signed_apk = sign( |
| 120 | processed_apk, keystore, temp, quiet=quiet, logging=logging) |
| 121 | aligned_apk = align(signed_apk, temp, quiet=quiet, logging=logging) |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 122 | utils.Print('Writing result to {}'.format(out), quiet=quiet) |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 123 | shutil.copyfile(aligned_apk, out) |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 124 | if install: |
Christoffer Quist Adamsen | 1de3dde | 2019-01-24 13:17:46 +0100 | [diff] [blame] | 125 | adb_cmd = ['adb'] |
| 126 | if adb_options: |
| 127 | adb_cmd.extend( |
| 128 | [option for option in adb_options.split(' ') if option]) |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 129 | adb_cmd.extend(['install', '-t', '-r', '-d', out]); |
Christoffer Quist Adamsen | 41cbdca | 2019-04-12 08:52:03 +0200 | [diff] [blame] | 130 | utils.RunCmd(adb_cmd, quiet=quiet, logging=logging) |
Christoffer Quist Adamsen | 4038bbe | 2019-01-15 14:31:46 +0100 | [diff] [blame] | 131 | |
| 132 | def main(): |
| 133 | (options, apk) = parse_options() |
| 134 | masseur(apk, **vars(options)) |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 135 | return 0 |
| 136 | |
| 137 | if __name__ == '__main__': |
| 138 | sys.exit(main()) |