13
13
# limitations under the License.
14
14
"""Combines some base files and the given files into an aar file."""
15
15
16
+ import re
17
+ import shutil
18
+ import tempfile
16
19
import os
17
20
import zipfile
18
21
from absl import app
37
40
flags .DEFINE_string ("classes_jar" , None ,
38
41
"Location of the classes.jar file to include " +
39
42
"in the aar. A default is used if not provided." )
43
+ flags .DEFINE_string ("manifest_package_name" , None ,
44
+ "Package name to overwrite the AndroidManifest with." )
40
45
41
46
42
47
def main (unused_argv ):
@@ -59,6 +64,21 @@ def main(unused_argv):
59
64
if FLAGS .classes_jar :
60
65
classes_jar_file = os .path .normcase (FLAGS .classes_jar )
61
66
67
+ # Edit the AndroidManifest file, replacing the package name
68
+ # with the provided one
69
+ temp_dir = tempfile .mkdtemp ()
70
+ patched_manifest = shutil .copy (android_manifest_file , temp_dir )
71
+ if FLAGS .manifest_package_name :
72
+ with open (patched_manifest , "r" ) as new_file :
73
+ contents = new_file .read ()
74
+
75
+ contents = re .sub ('package=".+"' ,
76
+ 'package="%s"' % FLAGS .manifest_package_name ,
77
+ contents )
78
+
79
+ with open (patched_manifest , "w" ) as new_file :
80
+ new_file .write (contents )
81
+
62
82
# Delete the aar file, if it already exists
63
83
if os .path .exists (output_file ):
64
84
os .remove (output_file )
@@ -79,7 +99,7 @@ def main(unused_argv):
79
99
80
100
with zipfile .ZipFile (output_file , "w" ) as myzip :
81
101
# Write the generic base files that are required in an aar file.
82
- myzip .write (android_manifest_file , "AndroidManifest.xml" )
102
+ myzip .write (patched_manifest , "AndroidManifest.xml" )
83
103
myzip .write (classes_jar_file , "classes.jar" )
84
104
myzip .write (os .path .join (file_dir , "R.txt" ), "R.txt" )
85
105
myzip .writestr ("res/" , "" )
0 commit comments