bba14f8068f0f1fe61910df5dae141b2c57237dd
[platform/framework/web/crosswalk.git] / src / xwalk / build / android / generate_xwalk_core_library_aar.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2014 Intel Corporation. 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 # pylint: disable=F0401
7
8 import optparse
9 import os
10 import shutil
11 import sys
12 import zipfile
13
14 LIBRARY_PROJECT_NAME = 'xwalk_core_library'
15 AAR_LIBRARY_NAME = 'xwalk_core_library_aar'
16
17 def AddGeneratorOptions(option_parser):
18   option_parser.add_option('-s', dest='source',
19                            help='Source directory of project root.',
20                            type='string')
21   option_parser.add_option('-t', dest='target',
22                            help='Product out target directory.',
23                            type='string')
24
25
26 def CopyProjectFiles(out_dir):
27   """cp out/Release/xwalk_core_library/AndroidManifest<file>
28         out/Release/xwalk_core_library_aar/<file>
29   """
30
31   print 'Copying library project files...'
32   files_to_copy = [
33       # AndroidManifest.xml from template.
34       'AndroidManifest.xml',
35   ]
36   for f in files_to_copy:
37     source_file = os.path.join(out_dir, LIBRARY_PROJECT_NAME, f)
38     target_file = os.path.join(out_dir, AAR_LIBRARY_NAME, f)
39     shutil.copy2(source_file, target_file)
40
41
42 def CopyBinaries(out_dir):
43   """cp out/Release/xwalk_core_library/libs/*
44         out/Release/xwalk_core_library_aar/jni/
45   """
46
47   print 'Copying binaries...'
48   # Copy jar files to classes.jar.
49   libs_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs')
50
51   source_file = os.path.join(libs_dir, 'xwalk_core_library_java.jar')
52   target_file = os.path.join(out_dir, AAR_LIBRARY_NAME, 'classes.jar')
53   shutil.copyfile(source_file, target_file)
54
55   # Copy native libraries.
56   source_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs')
57   target_dir = os.path.join(out_dir, AAR_LIBRARY_NAME, 'jni')
58   if not os.path.exists(target_dir):
59     os.makedirs(target_dir)
60
61   if os.path.exists(source_dir):
62     for item in os.listdir(source_dir):
63       sub_path = os.path.join(source_dir, item)
64       target_dir = os.path.join(target_dir, item)
65       if os.path.isdir(sub_path):
66         shutil.copytree(sub_path, target_dir)
67
68   # Copy R.txt.
69   r_source_dir = os.path.join(out_dir, 'gen', 'xwalk_core_internal_java') 
70   r_source_file = os.path.join(r_source_dir, 'java_R', 'R.txt')
71   r_target_file = os.path.join(out_dir, AAR_LIBRARY_NAME, 'R.txt')
72   shutil.copyfile(r_source_file, r_target_file) 
73
74
75 def CopyResources(out_dir):
76   print 'Copying resources...'
77   source_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'res')
78   target_dir = os.path.join(out_dir, AAR_LIBRARY_NAME, 'res')
79   shutil.copytree(source_dir, target_dir)
80
81
82 def GenerateAAR(aar_path, aar_dir):
83   zfile = zipfile.ZipFile(aar_path, 'w')
84   abs_src = os.path.abspath(aar_dir)
85   for dirname, _, files in os.walk(aar_dir):
86     for filename in files:
87       absname = os.path.abspath(os.path.join(dirname, filename))
88       relativename = absname[len(abs_src) + 1:]
89       zfile.write(absname, relativename)
90   zfile.close()
91   #delete the AAR dir.
92   shutil.rmtree(aar_dir)
93
94
95 def main(argv):
96   print 'Generating XWalkCore AAR Library...'
97   option_parser = optparse.OptionParser()
98   AddGeneratorOptions(option_parser)
99   options, _ = option_parser.parse_args(argv)
100
101   if not os.path.exists(options.source):
102     print 'Source project does not exist, please provide correct directory.'
103     sys.exit(1)
104   out_dir = options.target
105
106   # Clean the aar library.
107   aar_path = os.path.join(out_dir, 'xwalk_core_library.aar')
108   if os.path.exists(aar_path):
109     os.remove(aar_path)
110
111   aar_dir = os.path.join(out_dir, AAR_LIBRARY_NAME)
112   if os.path.exists(aar_dir):
113     shutil.rmtree(aar_dir)
114   os.mkdir(aar_dir)
115
116   # Copy Eclipse project files of library project.
117   CopyProjectFiles(out_dir)
118   # Copy binaries and resuorces.
119   CopyResources(out_dir)
120   CopyBinaries(out_dir)
121   GenerateAAR(aar_path, aar_dir)
122
123
124 if __name__ == '__main__':
125   sys.exit(main(sys.argv))