forked from Themaister/Granite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_android_build.py
executable file
·190 lines (165 loc) · 8.23 KB
/
create_android_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
import os
import sys
import argparse
from shutil import copyfile, copytree, rmtree
def canonical_path(p):
if not os.path.isabs(p):
p = os.path.join(os.getcwd(), p)
return p
def transform_gradle_path(p):
return ':' + p.replace('/', ':').replace('\\', ':')
def find_relative_path(from_path, to_path):
from_path = os.path.dirname(canonical_path(from_path))
to_path = canonical_path(to_path)
relpath = os.path.relpath(to_path, from_path)
return relpath
def main():
parser = argparse.ArgumentParser(description = 'Script for automatically creating Android Gradle builds')
parser.add_argument('--output-gradle',
help = 'Path where the Gradle build shall be placed',
required = True,
default = 'app')
parser.add_argument('--resource-dir',
help = 'Directory where the application provided res/drawable-* folders are found',
type = str)
parser.add_argument('--activity-icon-drawable',
help = 'The name of the logo drawable ID found in the res/ folders',
type = str,
default = 'icon')
parser.add_argument('--application-id',
required = True,
help = 'The application app ID, like com.foo.shinyapp')
parser.add_argument('--granite-dir',
help = 'Path to a Granite checkout',
default = 'granite')
parser.add_argument('--native-target',
help = 'The CMake target to build',
required = True,
type = str)
parser.add_argument('--app-name',
help = 'The app name, as shown to the user',
type = str,
required = True)
parser.add_argument('--abis',
nargs = '+',
help = 'Add native ABIs. Default is just arm64-v8a.',
type = str)
parser.add_argument('--version-code',
help = 'Version code',
type = str,
default = "1")
parser.add_argument('--version-name',
help = 'Version name',
type = str,
default = "1.0")
parser.add_argument('--cmake-lists-toplevel',
help = 'Path to the top-level CMakeLists.txt',
type = str,
default = 'CMakeLists.txt')
parser.add_argument('--assets',
help = 'The assets folder of the app',
type = str,
default = 'assets')
parser.add_argument('--audio',
help = 'Enable audio support',
action = 'store_true')
parser.add_argument('--physics',
help = 'Enable physics support',
action = 'store_true')
args = parser.parse_args()
abis = ['arm64-v8a'] if args.abis is None else args.abis
gradle_base = os.path.join(args.granite_dir, 'application/platforms/android/gradle/')
if not os.path.isdir(gradle_base):
print('Cannot find', gradle_base, 'in file system.', file = sys.stderr)
sys.exit(1)
manifest = os.path.join(gradle_base, 'AndroidManifest.xml')
build_gradle = os.path.join(gradle_base, 'build.gradle')
settings_gradle = os.path.join(gradle_base, 'settings.gradle')
toplevel_gradle = os.path.join(gradle_base, 'toplevel.build.gradle')
gradle_properties = os.path.join(gradle_base, 'gradle.properties')
if (not os.path.isfile(manifest)) or \
(not os.path.isfile(build_gradle)) or \
(not os.path.isfile(settings_gradle)) \
or (not os.path.isfile(toplevel_gradle)):
print('Cannot find template files in file system.', file = sys.stderr)
sys.exit(1)
os.makedirs(args.output_gradle, exist_ok = True)
os.makedirs(os.path.join(args.output_gradle, 'res'), exist_ok = True)
os.makedirs(os.path.join(args.output_gradle, 'res/values'), exist_ok = True)
output_toplevel_build_gradle = 'build.gradle'
output_gradle_properties = 'gradle.properties'
output_settings_gradle = 'settings.gradle'
resource_dir = os.path.join(args.granite_dir, 'application/platforms/android/gradle/res') if not args.resource_dir else args.resource_dir
granite_android_activity = find_relative_path(output_settings_gradle, os.path.join(args.granite_dir,
'application/platforms/android'))
# Write out AndroidManifest.xml
with open(manifest, 'r') as f:
manifest_data = f.read()
manifest_data = manifest_data \
.replace('$$ICON$$', args.activity_icon_drawable) \
.replace('$$NATIVE_TARGET$$', args.native_target) \
.replace('$$VERSION_CODE$$', args.version_code) \
.replace('$$VERSION_NAME$$', args.version_name)
target_manifest_path = os.path.join(args.output_gradle, 'AndroidManifest.xml')
with open(target_manifest_path, 'w') as dump_file:
print(manifest_data, file = dump_file)
# Write out strings.xml
with open(os.path.join(args.output_gradle, 'res/values/strings.xml'), 'w') as f:
print('<?xml version="1.0" encoding="utf-8"?>', file = f)
print('<resources>', file = f)
print('\t<string name="app_name">{}</string>'.format(args.app_name), file = f)
print('</resources>', file = f)
# Write out build.gradle
with open(build_gradle, 'r') as f:
target_build_gradle = os.path.join(args.output_gradle, 'build.gradle')
data = f.read()
cmakelists = find_relative_path(target_build_gradle, args.cmake_lists_toplevel)
assets = find_relative_path(target_build_gradle, args.assets)
granite_assets = find_relative_path(target_build_gradle, os.path.join(args.granite_dir, 'assets'))
granite_fsr2_assets = find_relative_path(target_build_gradle, os.path.join(args.granite_dir, 'third_party/fsr2/src/ffx-fsr2-api/shaders'))
external_jni = find_relative_path(target_build_gradle, os.path.join(args.granite_dir,
'application/platforms/android/external_layers'))
target_abis = ', '.join(["'" + x + "'" for x in abis])
data = data \
.replace('$$NAMESPACE$$', args.application_id) \
.replace('$$TARGET$$', args.native_target) \
.replace('$$CMAKELISTS$$', cmakelists) \
.replace('$$ASSETS$$', assets) \
.replace('$$GRANITE_ASSETS$$', granite_assets) \
.replace('$$GRANITE_FSR2_ASSETS$$', granite_fsr2_assets) \
.replace('$$EXTERNAL_JNI$$', external_jni) \
.replace('$$ABIS$$', target_abis) \
.replace('$$AUDIO$$', 'ON' if args.audio else 'OFF') \
.replace('$$PHYSICS$$', 'ON' if args.physics else 'OFF')
with open(target_build_gradle, 'w') as dump_file:
print(data, file = dump_file)
copyfile(toplevel_gradle, output_toplevel_build_gradle)
copyfile(gradle_properties, output_gradle_properties)
# Write out settings.gradle
with open(settings_gradle, 'r') as f:
data = f.read()
granite_app = find_relative_path(output_settings_gradle, args.output_gradle)
granite_app = transform_gradle_path(granite_app)
data = data \
.replace('$$APP$$', granite_app) \
.replace('$$GRANITE_ANDROID_ACTIVITY_PATH$$', granite_android_activity)
with open(output_settings_gradle, 'w') as dump_file:
print(data, file = dump_file)
drawables = [
'drawable-mdpi',
'drawable-hdpi',
'drawable-xhdpi',
'drawable-xxhdpi',
'drawable-xxxhdpi'
]
target_res = os.path.join(args.output_gradle, 'res')
for drawable in drawables:
src = os.path.join(resource_dir, drawable)
dst = os.path.join(target_res, drawable)
if os.path.isdir(src):
if os.path.isdir(dst):
rmtree(dst)
copytree(src, dst)
if __name__ == '__main__':
main()