Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Side by Side Diff: build/android/gyp/finalize_apk.py

Issue 595933003: Re-invent page aligning libraries in APK file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update for Ross' review Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright 2013 The Chromium Authors. All rights reserved. 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 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 """Signs and zipaligns APK. 6 """Signs and zipaligns APK.
7 7
8 """ 8 """
9 9
10 import optparse 10 import optparse
11 import shutil 11 import shutil
12 import sys 12 import sys
13 import tempfile 13 import tempfile
14 14
15 from util import build_utils 15 from util import build_utils
16 16
17 def RenameLibInApk(rezip_path, in_zip_file, out_zip_file): 17 def AddPageAlignment(prealign_path, in_zip_file, out_zip_file):
18 rename_cmd = [ 18 prealign_cmd = [
19 rezip_path, 19 'java',
20 'rename', 20 '-classpath',
21 prealign_path,
22 'RezipApk',
23 'addalignment',
21 in_zip_file, 24 in_zip_file,
22 out_zip_file, 25 out_zip_file,
23 ] 26 ]
24 build_utils.CheckOutput(rename_cmd) 27 build_utils.CheckOutput(prealign_cmd)
25 28
26 29
27 def SignApk(key_path, key_name, key_passwd, unsigned_path, signed_path): 30 def ReorderAndAlignApk(prealign_path, in_zip_file, out_zip_file):
31 prealign_cmd = [
32 'java',
33 '-classpath',
34 prealign_path,
35 'RezipApk',
36 'reorder',
37 in_zip_file,
38 out_zip_file,
39 ]
40 build_utils.CheckOutput(prealign_cmd)
41
42
43 def JarSigner(key_path, key_name, key_passwd, unsigned_path, signed_path):
28 shutil.copy(unsigned_path, signed_path) 44 shutil.copy(unsigned_path, signed_path)
29 sign_cmd = [ 45 sign_cmd = [
30 'jarsigner', 46 'jarsigner',
31 '-sigalg', 'MD5withRSA', 47 '-sigalg', 'MD5withRSA',
32 '-digestalg', 'SHA1', 48 '-digestalg', 'SHA1',
33 '-keystore', key_path, 49 '-keystore', key_path,
34 '-storepass', key_passwd, 50 '-storepass', key_passwd,
35 signed_path, 51 signed_path,
36 key_name, 52 key_name,
37 ] 53 ]
38 build_utils.CheckOutput(sign_cmd) 54 build_utils.CheckOutput(sign_cmd)
39 55
40 56
41 def AlignApk(zipalign_path, unaligned_path, final_path): 57 def AlignApk(zipalign_path, unaligned_path, final_path):
42 align_cmd = [ 58 align_cmd = [
43 zipalign_path, 59 zipalign_path,
44 '-f', '4', # 4 bytes 60 '-f', '4', # 4 bytes
45 unaligned_path, 61 unaligned_path,
46 final_path, 62 final_path,
47 ] 63 ]
48 build_utils.CheckOutput(align_cmd) 64 build_utils.CheckOutput(align_cmd)
49 65
50 66
51 def UncompressLibAndPageAlignInApk(rezip_path, in_zip_file, out_zip_file): 67 def RenameAndUncompressLibInApk(rezip_path, in_zip_file, out_zip_file):
52 rename_cmd = [ 68 rename_cmd = [
53 rezip_path, 69 rezip_path,
54 'inflatealign', 70 'renameinflate',
55 in_zip_file, 71 in_zip_file,
56 out_zip_file, 72 out_zip_file,
57 ] 73 ]
58 build_utils.CheckOutput(rename_cmd)
59
60
61 def DropDataDescriptorsInApk(rezip_path, in_zip_file, out_zip_file):
62 rename_cmd = [
63 rezip_path,
64 'dropdescriptors',
65 in_zip_file,
66 out_zip_file,
67 ]
68 build_utils.CheckOutput(rename_cmd) 74 build_utils.CheckOutput(rename_cmd)
69 75
70 76
71 def main(): 77 def main():
72 parser = optparse.OptionParser() 78 parser = optparse.OptionParser()
73 build_utils.AddDepfileOption(parser) 79 build_utils.AddDepfileOption(parser)
74 80
81 parser.add_option('--rezip-jar-path', help='Path to the RezipApk jar file.')
rmcilroy 2014/09/30 09:36:32 rezip-apk-jar-path
Anton 2014/09/30 10:30:01 Done.
75 parser.add_option('--zipalign-path', help='Path to the zipalign tool.') 82 parser.add_option('--zipalign-path', help='Path to the zipalign tool.')
76 parser.add_option('--rezip-path', help='Path to the rezip executable.') 83 parser.add_option('--rezip-path', help='Path to the rezip executable.')
77 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.') 84 parser.add_option('--unsigned-apk-path', help='Path to input unsigned APK.')
78 parser.add_option('--final-apk-path', 85 parser.add_option('--final-apk-path',
79 help='Path to output signed and aligned APK.') 86 help='Path to output signed and aligned APK.')
80 parser.add_option('--key-path', help='Path to keystore for signing.') 87 parser.add_option('--key-path', help='Path to keystore for signing.')
81 parser.add_option('--key-passwd', help='Keystore password') 88 parser.add_option('--key-passwd', help='Keystore password')
82 parser.add_option('--key-name', help='Keystore name') 89 parser.add_option('--key-name', help='Keystore name')
83 parser.add_option('--stamp', help='Path to touch on success.') 90 parser.add_option('--stamp', help='Path to touch on success.')
84 parser.add_option('--load-library-from-zip-file', type='int', 91 parser.add_option('--load-library-from-zip-file', type='int',
85 help='If non-zero, build the APK such that the library can be loaded ' + 92 help='If non-zero, build the APK such that the library can be loaded ' +
86 'directly from the zip file using the crazy linker. The library ' + 93 'directly from the zip file using the crazy linker. The library ' +
87 'will be renamed, uncompressed and page aligned.') 94 'will be renamed, uncompressed and page aligned.')
88 95
89 options, _ = parser.parse_args() 96 options, _ = parser.parse_args()
90 97
91 with tempfile.NamedTemporaryFile() as signed_apk_path_tmp, \ 98 with tempfile.NamedTemporaryFile() as signed_apk_path_tmp, \
92 tempfile.NamedTemporaryFile() as apk_to_sign_tmp, \ 99 tempfile.NamedTemporaryFile() as apk_to_sign_tmp, \
93 tempfile.NamedTemporaryFile() as apk_without_descriptors_tmp, \ 100 tempfile.NamedTemporaryFile() as uncompress_lib_apk_tmp:
94 tempfile.NamedTemporaryFile() as aligned_apk_tmp:
95 101
96 if options.load_library_from_zip_file: 102 if options.load_library_from_zip_file:
97 # We alter the name of the library so that the Android Package Manager 103 # We alter the name of the library so that the Android Package Manager
98 # does not extract it into a separate file. This must be done before 104 # does not extract it into a separate file. This must be done before
99 # signing, as the filename is part of the signed manifest. 105 # signing, as the filename is part of the signed manifest. At the same
106 # time we uncompress the library, which is necessary so that it can be
107 # loaded directly from the APK.
108 uncompress_lib_apk_path = uncompress_lib_apk_tmp.name
109 RenameAndUncompressLibInApk(
110 options.rezip_path, options.unsigned_apk_path,
111 uncompress_lib_apk_path)
100 apk_to_sign = apk_to_sign_tmp.name 112 apk_to_sign = apk_to_sign_tmp.name
101 RenameLibInApk(options.rezip_path, options.unsigned_apk_path, apk_to_sign) 113 # Move the library to a page boundary by adding a page alignment file.
114 AddPageAlignment(
115 options.rezip_jar_path, uncompress_lib_apk_path, apk_to_sign)
102 else: 116 else:
103 apk_to_sign = options.unsigned_apk_path 117 apk_to_sign = options.unsigned_apk_path
104 118
105 signed_apk_path = signed_apk_path_tmp.name 119 signed_apk_path = signed_apk_path_tmp.name
106 SignApk(options.key_path, options.key_name, options.key_passwd, 120 JarSigner(options.key_path, options.key_name, options.key_passwd,
107 apk_to_sign, signed_apk_path) 121 apk_to_sign, signed_apk_path)
108 122
109 if options.load_library_from_zip_file: 123 if options.load_library_from_zip_file:
110 # Signing adds data descriptors to the APK. These are redundant 124 # Reorder the contents of the APK. This re-establishes the canonical
111 # information. We remove them as otherwise they can cause a 125 # order which means the library will be back at its page aligned location.
112 # miscalculation in the page alignment. 126 # This step also aligns uncompressed items to 4 bytes.
113 apk_to_align = apk_without_descriptors_tmp.name 127 ReorderAndAlignApk(
114 DropDataDescriptorsInApk( 128 options.rezip_jar_path, signed_apk_path, options.final_apk_path)
115 options.rezip_path, signed_apk_path, apk_to_align)
116 aligned_apk = aligned_apk_tmp.name
117 else: 129 else:
118 apk_to_align = signed_apk_path 130 # Align uncompressed items to 4 bytes
119 aligned_apk = options.final_apk_path 131 AlignApk(options.zipalign_path, signed_apk_path, options.final_apk_path)
120
121 # Align uncompress items to 4 bytes
122 AlignApk(options.zipalign_path, apk_to_align, aligned_apk)
123
124 if options.load_library_from_zip_file:
125 # Uncompress the library and make sure that it is page aligned.
126 UncompressLibAndPageAlignInApk(
127 options.rezip_path, aligned_apk, options.final_apk_path)
128 132
129 if options.depfile: 133 if options.depfile:
130 build_utils.WriteDepfile( 134 build_utils.WriteDepfile(
131 options.depfile, build_utils.GetPythonDependencies()) 135 options.depfile, build_utils.GetPythonDependencies())
132 136
133 if options.stamp: 137 if options.stamp:
134 build_utils.Touch(options.stamp) 138 build_utils.Touch(options.stamp)
135 139
136 140
137 if __name__ == '__main__': 141 if __name__ == '__main__':
138 sys.exit(main()) 142 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698