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.
15 from util
import build_utils
17 def RenameLibInApk(rezip_path
, in_zip_file
, out_zip_file
):
24 build_utils
.CheckOutput(rename_cmd
)
27 def SignApk(key_path
, key_name
, key_passwd
, unsigned_path
, signed_path
):
28 shutil
.copy(unsigned_path
, signed_path
)
31 '-sigalg', 'MD5withRSA',
33 '-keystore', key_path
,
34 '-storepass', key_passwd
,
38 build_utils
.CheckOutput(sign_cmd
)
41 def AlignApk(zipalign_path
, unaligned_path
, final_path
):
48 build_utils
.CheckOutput(align_cmd
)
51 def UncompressLibAndPageAlignInApk(rezip_path
, in_zip_file
, out_zip_file
):
58 build_utils
.CheckOutput(rename_cmd
)
61 def DropDataDescriptorsInApk(rezip_path
, in_zip_file
, out_zip_file
):
68 build_utils
.CheckOutput(rename_cmd
)
72 parser
= optparse
.OptionParser()
73 build_utils
.AddDepfileOption(parser
)
75 parser
.add_option('--zipalign-path', help='Path to the zipalign tool.')
76 parser
.add_option('--rezip-path', help='Path to the rezip executable.')
77 parser
.add_option('--unsigned-apk-path', help='Path to input unsigned APK.')
78 parser
.add_option('--final-apk-path',
79 help='Path to output signed and aligned APK.')
80 parser
.add_option('--key-path', help='Path to keystore for signing.')
81 parser
.add_option('--key-passwd', help='Keystore password')
82 parser
.add_option('--key-name', help='Keystore name')
83 parser
.add_option('--stamp', help='Path to touch on success.')
84 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 ' +
86 'directly from the zip file using the crazy linker. The library ' +
87 'will be renamed, uncompressed and page aligned.')
89 options
, _
= parser
.parse_args()
91 with tempfile
.NamedTemporaryFile() as signed_apk_path_tmp
, \
92 tempfile
.NamedTemporaryFile() as apk_to_sign_tmp
, \
93 tempfile
.NamedTemporaryFile() as apk_without_descriptors_tmp
, \
94 tempfile
.NamedTemporaryFile() as aligned_apk_tmp
:
96 if options
.load_library_from_zip_file
:
97 # 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
99 # signing, as the filename is part of the signed manifest.
100 apk_to_sign
= apk_to_sign_tmp
.name
101 RenameLibInApk(options
.rezip_path
, options
.unsigned_apk_path
, apk_to_sign
)
103 apk_to_sign
= options
.unsigned_apk_path
105 signed_apk_path
= signed_apk_path_tmp
.name
106 SignApk(options
.key_path
, options
.key_name
, options
.key_passwd
,
107 apk_to_sign
, signed_apk_path
)
109 if options
.load_library_from_zip_file
:
110 # Signing adds data descriptors to the APK. These are redundant
111 # information. We remove them as otherwise they can cause a
112 # miscalculation in the page alignment.
113 apk_to_align
= apk_without_descriptors_tmp
.name
114 DropDataDescriptorsInApk(
115 options
.rezip_path
, signed_apk_path
, apk_to_align
)
116 aligned_apk
= aligned_apk_tmp
.name
118 apk_to_align
= signed_apk_path
119 aligned_apk
= options
.final_apk_path
121 # Align uncompress items to 4 bytes
122 AlignApk(options
.zipalign_path
, apk_to_align
, aligned_apk
)
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
)
130 build_utils
.WriteDepfile(
131 options
.depfile
, build_utils
.GetPythonDependencies())
134 build_utils
.Touch(options
.stamp
)
137 if __name__
== '__main__':