-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
build.gradle
279 lines (241 loc) · 9.39 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
import java.nio.file.Paths
// android/build.gradle
// based on:
//
// * https://github.com/facebook/versioned-react-native/blob/0.60-stable/template/android/build.gradle
// previous location:
// - https://github.com/facebook/versioned-react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/build.gradle
//
// * https://github.com/facebook/versioned-react-native/blob/0.60-stable/template/android/app/build.gradle
// previous location:
// - https://github.com/facebook/versioned-react-native/blob/0.58-stable/local-cli/templates/HelloWorld/android/app/build.gradle
// FBJNI build is based on:
// https://github.com/facebookincubator/fbjni/blob/main/docs/android_setup.md
// These defaults should reflect the SDK versions used by
// the minimum React Native version supported.
def DEFAULT_COMPILE_SDK_VERSION = 28
def DEFAULT_BUILD_TOOLS_VERSION = '28.0.3'
def DEFAULT_MIN_SDK_VERSION = 16
def DEFAULT_TARGET_SDK_VERSION = 28
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
apply plugin: 'com.android.library'
static def findNodeModules(baseDir) {
def basePath = baseDir.toPath().normalize()
// Node's module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
return nodeModulesPath.toString()
}
basePath = basePath.getParent()
}
throw new GradleException("React-Native-Skia: Failed to find node_modules/ path!")
}
def nodeModules = Paths.get(projectDir.getPath(), '../../../../..').toString()
logger.warn("react-native-skia: node_modules/ found at: ${nodeModules}")
def sourceBuild = false
def defaultDir = null
def androidSourcesDir = null
def androidSourcesName = 'React Native sources'
def reactNativeArchitectures() {
def value = project.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}
def buildType = "debug"
tasks.all({ task ->
if (task.name == "buildCMakeRelease") {
buildType = "release"
}
})
if (rootProject.ext.has('reactNativeAndroidRoot')) {
defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
androidSourcesDir = defaultDir.parentFile.toString()
} else if (findProject(':ReactAndroid') != null) {
sourceBuild = false
defaultDir = project(':ReactAndroid').projectDir
androidSourcesDir = defaultDir.parentFile.toString()
} else {
defaultDir = file("$nodeModules/versioned-react-native/android")
androidSourcesDir = defaultDir.parentFile.toString()
}
if (!defaultDir.exists()) {
throw new GradleException(
"${project.name}: React Native android directory (node_modules/versioned-react-native/android) does not exist! Resolved node_modules to: ${nodeModules}"
)
}
def prebuiltDir = sourceBuild
? Paths.get(findProject(":ReactAndroid").getProjectDir().toString(), "build", "intermediates", "library_*", "*", "jni")
: "$buildDir/reactandroid-abi*/jni"
def reactProperties = new Properties()
file("$defaultDir/gradle.properties").withInputStream { reactProperties.load(it) }
def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME").split("\\.")[1].toInteger()
logger.warn("react-native-skia: React Native version: ${REACT_NATIVE_VERSION}")
logger.warn("react-native-skia: Is Source build: ${sourceBuild}")
logger.warn("react-native-skia: Prebuilt dir: ${prebuiltDir}")
logger.warn("react-native-skia: Build type: ${buildType}")
buildscript {
// The Android Gradle plugin is only required when opening the android folder stand-alone.
// This avoids unnecessary downloads and potential conflicts when the library is included as a
// module dependency in an application project.
// ref: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:build_script_external_dependencies
if (project == rootProject) {
repositories {
google()
}
dependencies {
// This should reflect the Gradle plugin version used by
// the minimum React Native version supported.
classpath 'com.android.tools.build:gradle:3.4.1'
}
}
}
android {
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
versionCode 1
versionName "1.0"
externalNativeBuild {
cmake {
cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID"
abiFilters (*reactNativeArchitectures())
arguments '-DANDROID_STL=c++_shared',
"-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}",
"-DNODE_MODULES_DIR=${nodeModules}",
"-DPREBUILT_DIR=${prebuiltDir}"
}
}
}
lintOptions {
abortOnError false
}
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
}
}
packagingOptions {
excludes = [
"**/libc++_shared.so",
"**/libfbjni.so",
"**/libjsi.so",
"**/libreact_nativemodule_core.so",
"**/libturbomodulejsijni.so"
]
}
// Create new configurations that can be referred to in dependencies.
// The Android Gradle Plugin 3.* does not allow hooking into existing
// configurations like `implementation`.
configurations {
extractHeaders
extractJNI
}
}
repositories {
// ref: https://www.baeldung.com/maven-local-repository
jcenter()
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/versioned-react-native/android"
}
maven {
// Android JSC is installed from npm
url "$rootDir/../node_modules/jsc-android/dist"
}
google()
}
dependencies {
//noinspection GradleDynamicVersion
implementation 'host.exp:reactandroid-abi46_0_0:1.0.0'
compileOnly 'com.facebook.fbjni:fbjni:0.2.2'
compileOnly 'com.facebook.yoga:proguard-annotations:1.19.0'
compileOnly 'androidx.annotation:annotation:+'
// From node_modules
//noinspection GradleDynamicVersion
extractHeaders("com.facebook.fbjni:fbjni:0.2.2:headers")
//noinspection GradleDynamicVersion
extractJNI("com.facebook.fbjni:fbjni:0.2.2")
if(REACT_NATIVE_VERSION < 69) {
def rnAAR = fileTree("${rootDir}/versioned-abis").matching({ include "**/reactandroid-abi46_0_0/**/*.aar" }).singleFile
extractJNI(files(rnAAR))
} else {
// React Native >= 0.69
def rnAarMatcher = "**/versioned-react-native/**/*${buildType}.aar"
def rnAAR = fileTree("${rootDir}/versioned-abis").matching({ include "**/reactandroid-abi46_0_0/**/*.aar" })
if (rnAAR.any()) {
extractJNI(files(rnAAR))
}
}
}
afterEvaluate { project ->
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
include '**/*.java'
}
android.libraryVariants.all { variant ->
def name = variant.name.capitalize()
def javaCompileTask = variant.javaCompileProvider.get()
task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
from javaCompileTask.destinationDir
}
}
artifacts {
archives androidSourcesJar
}
}
task extractAARHeaders {
doLast {
configurations.extractHeaders.files.each {
def file = it.absoluteFile
copy {
from zipTree(file)
into "$buildDir/$file.name"
include "**/*.h"
}
}
}
}
task extractJNIFiles {
doLast {
configurations.extractJNI.files.each {
def file = it.absoluteFile
copy {
from zipTree(file)
into "$buildDir/$file.name"
include "jni/**/*"
}
}
}
}
extractJNIFiles.mustRunAfter extractAARHeaders
def nativeBuildDependsOn(dependsOnTask, variant) {
def buildTasks = tasks.findAll({ task ->
!task.name.contains("Clean") && (task.name.contains("externalNative") || task.name.contains("CMake")) })
if (variant != null) {
buildTasks = buildTasks.findAll({ task -> task.name.contains(variant) })
}
buildTasks.forEach { task -> task.dependsOn(dependsOnTask) }
}
afterEvaluate {
nativeBuildDependsOn(extractAARHeaders, null)
nativeBuildDependsOn(extractJNIFiles, null)
}
tasks.whenTaskAdded { task ->
if (!task.name.contains("Clean") && (task.name.contains('externalNativeBuild') || task.name.startsWith('configureCMake'))) {
if (sourceBuild) {
def currentBuildType = task.name.endsWith('Debug') ? 'Debug' : 'Release'
task.dependsOn(":ReactAndroid:copy${currentBuildType}JniLibsProjectOnly")
}
} else if (task.name.startsWith('generateJsonModel') && sourceBuild) {
def currentBuildType = task.name.endsWith('Debug') ? 'Debug' : 'Release'
task.dependsOn(":ReactAndroid:copy${currentBuildType}JniLibsProjectOnly")
}
}