-
Notifications
You must be signed in to change notification settings - Fork 66
/
build.gradle
344 lines (313 loc) · 15.9 KB
/
build.gradle
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
cppFlags ""
arguments "-DANDROID_STL=c++_shared"
}
}
ndk {
abiFilters = project(":app").android.defaultConfig.ndk.abiFilters
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
sourceSets {
main {
jniLibs.srcDirs 'libnode/bin/'
}
main.assets.srcDirs += '../install/resources/nodejs-modules'
}
lintOptions {
abortOnError false
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.facebook.react:react-native:+'
}
task CopyNodeProjectAssetsFolder (type:Sync) {
description "Copies the Node Project to a build folder for manipulation."
from "${rootProject.projectDir}/../nodejs-assets/nodejs-project"
into "${rootProject.buildDir}/nodejs-assets/nodejs-project/"
exclude '**/*~' // temporary files
exclude '**/.*' // files and dirs starting with .
exclude '**/*.gz' // gzip files will cause errors on aapt when merging assets.
}
task GenerateNodeProjectAssetsLists {
dependsOn "CopyNodeProjectAssetsFolder"
description "Generates a list for runtime copying"
inputs.file "${rootProject.buildDir}/nodejs-assets/"
outputs.file "${rootProject.buildDir}/nodejs-assets/file.list"
outputs.file "${rootProject.buildDir}/nodejs-assets/dir.list"
doLast{
delete "${rootProject.buildDir}/nodejs-assets/file.list"
delete "${rootProject.buildDir}/nodejs-assets/dir.list"
String file_list = "";
String dir_list = "";
def assets_tree = fileTree(dir: "${rootProject.buildDir}/nodejs-assets/")
assets_tree.include('nodejs-project/**') // Include the node project.
assets_tree.exclude('**/.*') // Exclude files and dirs starting with .
assets_tree.exclude('**/*~') // Exclude temporary files.
assets_tree.visit { assetFile ->
if (assetFile.isDirectory()) {
dir_list += "${assetFile.relativePath}\n"
} else {
file_list += "${assetFile.relativePath}\n"
}
}
def file_list_path = new File( "${rootProject.buildDir}/nodejs-assets/file.list")
file_list_path.write file_list
def dir_list_path = new File( "${rootProject.buildDir}/nodejs-assets/dir.list")
dir_list_path.write dir_list
}
}
project.android.sourceSets.main.assets.srcDirs+="${rootProject.buildDir}/nodejs-assets/"
tasks.getByPath(":${project.name}:preBuild").dependsOn GenerateNodeProjectAssetsLists
import org.gradle.internal.os.OperatingSystem;
String shouldRebuildNativeModules = System.getenv('NODEJS_MOBILE_BUILD_NATIVE_MODULES');
if (shouldRebuildNativeModules==null) {
// If the environment variable is not set right now, check if it has been saved to a file.
def nativeModulesPreferenceFile = file("${rootProject.projectDir}/../nodejs-assets/BUILD_NATIVE_MODULES.txt");
if (nativeModulesPreferenceFile.exists()) {
shouldRebuildNativeModules=nativeModulesPreferenceFile.text.trim();
}
}
if ("1".equals(shouldRebuildNativeModules)) {
String npmCommandName = 'npm';
String nodeCommandName = 'node';
if (OperatingSystem.current().isMacOsX()) {
// On macOS, npm's and node's locations may not be in the PATH environment variable if gradle is being run
// by Android Studio. We need npm to build native modules and node to run node-pre-gyp patches, so we use
// helper scripts that are created when the plugin is installed to run npm and node with the PATH members that
// were available during the plugin's install.
try {
def commandResult = exec {
commandLine 'command', '-v', 'npm'
ignoreExitValue = true
}
if ( commandResult.getExitValue() != 0 ) {
// If npm is not found by command, use the helper script.
logger.warn("Couldn't find npm in the PATH for building native modules. Will try to use a helper script.");
npmCommandName = '../build-native-modules-MacOS-helper-script-npm.sh';
}
commandResult = exec {
commandLine 'command', '-v', 'node'
ignoreExitValue = true
}
if ( commandResult.getExitValue() != 0 ) {
// If node is not found by command, use the helper script.
logger.warn("Couldn't find node in the PATH for building native modules. Will try to use a helper script.");
nodeCommandName = '../build-native-modules-MacOS-helper-script-node.sh';
}
} catch ( Exception e ) {
throw new GradleException('Something went wrong looking for npm and node by running "command".', e)
}
}
task ApplyPatchScriptToModules (type:Exec) {
dependsOn "CopyNodeProjectAssetsFolder"
description "Apply patches to modules to improve compatibility."
doFirst {
if (OperatingSystem.current().isMacOsX()) {
// Copy the helper script for calling node when building in Android Studio on macOS.
copy {
from "${rootProject.projectDir}/../nodejs-assets/build-native-modules-MacOS-helper-script-node.sh"
into "${rootProject.buildDir}/nodejs-assets/"
}
}
}
workingDir "${rootProject.buildDir}/nodejs-assets/nodejs-project/"
commandLine nodeCommandName, "${project.projectDir}/../scripts/patch-package.js", "${rootProject.buildDir}/nodejs-assets/nodejs-project/node_modules/"
doLast {
if (OperatingSystem.current().isMacOsX()) {
// Deletes the helper script so it doesn't get included in the APK.
delete "${rootProject.buildDir}/nodejs-assets/build-native-modules-MacOS-helper-script-node.sh"
}
}
}
GenerateNodeProjectAssetsLists.dependsOn "ApplyPatchScriptToModules"
android.defaultConfig.ndk.abiFilters.each { abi_name ->
String temp_arch = {
switch (abi_name) {
case 'armeabi-v7a':
'arm'
break
case 'arm64-v8a':
'arm64'
break
default:
abi_name
break
}
}()
String temp_cc_ver = '4.9';
String temp_dest_cpu;
String temp_v8_arch;
String temp_suffix;
String temp_toolchain_name;
switch ( temp_arch )
{
case 'arm':
temp_dest_cpu = "${temp_arch}"
temp_v8_arch = "${temp_arch}"
temp_suffix = "${temp_arch}-linux-androideabi"
temp_toolchain_name = "${temp_suffix}"
break
case 'x86':
temp_dest_cpu = 'ia32'
temp_v8_arch = 'ia32'
temp_suffix = 'i686-linux-android'
temp_toolchain_name = "${temp_arch}"
break
case 'x86_64':
temp_dest_cpu = 'x64'
temp_v8_arch = 'x64'
temp_suffix = "${temp_arch}-linux-android"
temp_toolchain_name = "${temp_arch}"
break
case 'arm64':
temp_dest_cpu = "${temp_arch}"
temp_v8_arch = "${temp_arch}"
temp_suffix = 'aarch64-linux-android'
temp_toolchain_name = 'aarch64'
break
default:
throw new GradleException("Unsupported architecture for nodejs-mobile native modules: ${temp_arch}")
break
}
String ndk_bundle_path = android.ndkDirectory
String standalone_toolchain = "${rootProject.buildDir}/standalone-toolchains/${temp_toolchain_name}"
String npm_toolchain_add_to_path = "${rootProject.buildDir}/bin"
String npm_toolchain_ar = "${standalone_toolchain}/bin/${temp_suffix}-ar"
String npm_toolchain_cc = "${standalone_toolchain}/bin/${temp_suffix}-clang"
String npm_toolchain_cxx = "${standalone_toolchain}/bin/${temp_suffix}-clang++"
String npm_toolchain_link = "${standalone_toolchain}/bin/${temp_suffix}-clang++"
String npm_gyp_defines = "target_arch=${temp_arch}"
npm_gyp_defines += " v8_target_arch=${temp_v8_arch}"
npm_gyp_defines += " android_target_arch=${temp_arch}"
if (OperatingSystem.current().isMacOsX()) {
npm_gyp_defines += " host_os=mac OS=android"
} else if (OperatingSystem.current().isLinux()) {
npm_gyp_defines += " host_os=linux OS=android"
} else {
throw new GradleException("Unsupported opperating system for nodejs-mobile native builds: ${OperatingSystem.current().getName()}")
}
task "CopyNodeProjectAssets${abi_name}" {
description = "Copying node assets and apply patches to build native modules for ${abi_name}."
inputs.files fileTree (
dir: "${rootProject.projectDir}/../nodejs-assets/nodejs-project/"
).exclude({
details -> // We shouldn't need to rebuild native code if there are only changes in the Node.js project javascript files.
!details.isDirectory() &&
details.getPath().endsWith('.js') &&
!details.getPath().startsWith('node_modules/')
})
outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
doLast {
delete "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
copy {
from "${rootProject.projectDir}/../nodejs-assets/nodejs-project/"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
}
if (OperatingSystem.current().isMacOsX()) {
// Copy the helper scripts for calling npm and node when building in Android Studio on macOS.
copy {
from "${rootProject.projectDir}/../nodejs-assets/build-native-modules-MacOS-helper-script-node.sh"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
}
copy {
from "${rootProject.projectDir}/../nodejs-assets/build-native-modules-MacOS-helper-script-npm.sh"
into "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/"
}
}
exec {
workingDir "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
commandLine nodeCommandName, "${project.projectDir}/../scripts/patch-package.js", "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/node_modules/"
}
new File("${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp").text = "${new Date().format('yyyy-MM-dd HH:mm:ss')}"
}
}
task "MakeToolchain${abi_name}" (type:Exec) {
description = "Building a native toolchain to compile nodejs-mobile native modules for ${abi_name}."
executable = "${ndk_bundle_path}/build/tools/make-standalone-toolchain.sh"
args "--toolchain=${temp_toolchain_name}-${temp_cc_ver}", "--arch=${temp_arch}", "--install-dir=${standalone_toolchain}", "--stl=libc++", "--force", "--platform=android-22"
outputs.file "${standalone_toolchain}"
}
task "BuildNpmModules${abi_name}" (type:Exec) {
dependsOn "CopyNodeProjectAssets${abi_name}"
dependsOn "MakeToolchain${abi_name}"
description = "Building native modules for ${abi_name}."
inputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/copy.timestamp"
outputs.file "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
workingDir "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
commandLine npmCommandName, '--verbose', 'rebuild', '--build-from-source'
environment ('npm_config_node_engine', 'v8' )
environment ('npm_config_nodedir', "${project.projectDir}/libnode/" )
environment ('npm_config_node_gyp', "${project.projectDir}/../../nodejs-mobile-gyp/bin/node-gyp.js" )
environment ('npm_config_arch', temp_arch)
environment ('npm_config_platform', 'android')
environment ('npm_config_format', 'make-android')
environment ('TOOLCHAIN',"${standalone_toolchain}")
environment ('AR',"${npm_toolchain_ar}")
environment ('CC',"${npm_toolchain_cc}")
environment ('CXX',"${npm_toolchain_cxx}")
environment ('LINK',"${npm_toolchain_link}")
environment ('GYP_DEFINES',"${npm_gyp_defines}")
}
task "CopyBuiltNpmAssets${abi_name}" (type:Sync) {
dependsOn "BuildNpmModules${abi_name}"
description = "Copying node assets with build native modules for ${abi_name}."
from "${rootProject.buildDir}/nodejs-native-assets-temp-build/nodejs-native-assets-${abi_name}/nodejs-project/"
into "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
includeEmptyDirs = false
include '**/*.node'
}
task "GenerateNodeNativeAssetsLists${abi_name}" {
dependsOn "CopyBuiltNpmAssets${abi_name}"
description "Generates a list for runtime copying"
inputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/"
outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
outputs.file "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
doLast{
delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list"
delete "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list"
String file_list = "";
String dir_list = "";
def assets_tree = fileTree(dir: "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/" )
assets_tree.visit { assetFile ->
if (assetFile.isDirectory()) {
dir_list+="${assetFile.relativePath}\n"
} else {
file_list+="${assetFile.relativePath}\n"
}
}
def file_list_path = new File( "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/file.list")
file_list_path.write file_list
def dir_list_path = new File( "${rootProject.buildDir}/nodejs-native-assets/nodejs-native-assets-${abi_name}/dir.list")
dir_list_path.write dir_list
}
}
tasks.getByPath(":${project.name}:preBuild").dependsOn "GenerateNodeNativeAssetsLists${abi_name}"
}
project.android.sourceSets.main.assets.srcDirs+="${rootProject.buildDir}/nodejs-native-assets/"
}