Move core/forwarders to internal.
[chromium-blink-merge.git] / build / android / gyp / finalize_apk.py
blob0a8003508b505213b175cad9730611f7a53ff636
1 #!/usr/bin/env python
3 # Copyright 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6 """Signs and zipaligns APK.
8 """
10 import optparse
11 import shutil
12 import sys
13 import tempfile
15 from util import build_utils
17 def RenameInflateAndAddPageAlignment(
18 rezip_apk_jar_path, in_zip_file, out_zip_file):
19 rezip_apk_cmd = [
20 'java',
21 '-classpath',
22 rezip_apk_jar_path,
23 'RezipApk',
24 'renamealign',
25 in_zip_file,
26 out_zip_file,
28 build_utils.CheckOutput(rezip_apk_cmd)
31 def ReorderAndAlignApk(rezip_apk_jar_path, in_zip_file, out_zip_file):
32 rezip_apk_cmd = [
33 'java',
34 '-classpath',
35 rezip_apk_jar_path,
36 'RezipApk',
37 'reorder',
38 in_zip_file,
39 out_zip_file,
41 build_utils.CheckOutput(rezip_apk_cmd)
44 def JarSigner(key_path, key_name, key_passwd, unsigned_path, signed_path):
45 shutil.copy(unsigned_path, signed_path)
46 sign_cmd = [
47 'jarsigner',
48 '-sigalg', 'MD5withRSA',
49 '-digestalg', 'SHA1',
50 '-keystore', key_path,
51 '-storepass', key_passwd,
52 signed_path,
53 key_name,
55 build_utils.CheckOutput(sign_cmd)
58 def AlignApk(zipalign_path, unaligned_path, final_path):
59 align_cmd = [
60 zipalign_path,
61 '-f', '4', # 4 bytes
62 unaligned_path,
63 final_path,
65 build_utils.CheckOutput(align_cmd)
68 def main():
69 parser = optparse.OptionParser()
70 build_utils.AddDepfileOption(parser)
72 parser.add_option('--rezip-apk-jar-path',
73 help='Path to the RezipApk jar file.')
74 parser.add_option('--zipalign-path', help='Path to the zipalign tool.')
75 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.')
76 parser.add_option('--final-apk-path',
77 help='Path to output signed and aligned APK.')
78 parser.add_option('--key-path', help='Path to keystore for signing.')
79 parser.add_option('--key-passwd', help='Keystore password')
80 parser.add_option('--key-name', help='Keystore name')
81 parser.add_option('--stamp', help='Path to touch on success.')
82 parser.add_option('--load-library-from-zip', type='int',
83 help='If non-zero, build the APK such that the library can be loaded ' +
84 'directly from the zip file using the crazy linker. The library ' +
85 'will be renamed, uncompressed and page aligned.')
87 options, _ = parser.parse_args()
89 FinalizeApk(options)
91 if options.depfile:
92 build_utils.WriteDepfile(
93 options.depfile, build_utils.GetPythonDependencies())
95 if options.stamp:
96 build_utils.Touch(options.stamp)
99 def FinalizeApk(options):
100 with tempfile.NamedTemporaryFile() as signed_apk_path_tmp, \
101 tempfile.NamedTemporaryFile() as apk_to_sign_tmp:
103 if options.load_library_from_zip:
104 # We alter the name of the library so that the Android Package Manager
105 # does not extract it into a separate file. This must be done before
106 # signing, as the filename is part of the signed manifest. At the same
107 # time we uncompress the library, which is necessary so that it can be
108 # loaded directly from the APK.
109 # Move the library to a page boundary by adding a page alignment file.
110 apk_to_sign = apk_to_sign_tmp.name
111 RenameInflateAndAddPageAlignment(
112 options.rezip_apk_jar_path, options.unsigned_apk_path, apk_to_sign)
113 else:
114 apk_to_sign = options.unsigned_apk_path
116 signed_apk_path = signed_apk_path_tmp.name
117 JarSigner(options.key_path, options.key_name, options.key_passwd,
118 apk_to_sign, signed_apk_path)
120 if options.load_library_from_zip:
121 # Reorder the contents of the APK. This re-establishes the canonical
122 # order which means the library will be back at its page aligned location.
123 # This step also aligns uncompressed items to 4 bytes.
124 ReorderAndAlignApk(
125 options.rezip_apk_jar_path, signed_apk_path, options.final_apk_path)
126 else:
127 # Align uncompressed items to 4 bytes
128 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path)
131 if __name__ == '__main__':
132 sys.exit(main())