-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathbuild.gradle
More file actions
315 lines (274 loc) · 10.7 KB
/
Copy pathbuild.gradle
File metadata and controls
315 lines (274 loc) · 10.7 KB
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
import java.nio.file.Paths
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
apply plugin: "io.gitlab.arturbosch.detekt"
buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['DdSdkReactNative_kotlinVersion']
repositories {
mavenCentral()
google()
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.3.1'
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:11.5.1"
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.23.8"
classpath 'com.github.bjoernq:unmockplugin:0.7.9'
// Uncomment here and in settings.gradle for getting rid of IDE errors for new architecture:
// classpath "com.facebook.react:react-native-gradle-plugin"
}
}
apply plugin: 'de.mobilej.unmock'
def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}
apply plugin: 'com.android.library'
if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}
apply plugin: 'kotlin-android'
apply plugin: 'org.jlleitschuh.gradle.ktlint'
/**
* Finds the path of the installed npm package with the given name using Node's
* module resolution algorithm, which searches "node_modules" directories up to
* the file system root.
* This enables us to handle monorepos without requiring the node executable.
*
* The search begins at the given base directory (a File object). The returned
* path is a string.
*/
static def findNodeModulePath(baseDir, packageName) {
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 candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
if (candidatePath.toFile().exists()) {
return candidatePath.toString()
}
basePath = basePath.getParent()
}
return null
}
def resolveReactNativeDirectory() {
def reactNativeLocation = rootProject.hasProperty("reactNativeDir") ? rootProject.getProperty("reactNativeDir") : null
if (reactNativeLocation != null) {
return file(reactNativeLocation)
}
def reactNativePath = findNodeModulePath(projectDir, "react-native")
if (reactNativePath != null) {
return file(reactNativePath)
}
throw new Exception(
"${project.name}: Failed to resolve 'react-native' in the project. " +
"Altenatively, you can specify 'reactNativeDir' with the path to 'react-native' in your 'gradle.properties' file."
)
}
def reactNativeRootDir = resolveReactNativeDirectory()
def reactProperties = new Properties()
file("$reactNativeRootDir/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
def reactNativeVersion = reactProperties.getProperty("VERSION_NAME")
def (reactNativeMajorVersion, reactNativeMinorVersion) = reactNativeVersion.split("\\.").collect { it.isInteger() ? it.toInteger() : it }
def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['DdSdkReactNative_' + name]
}
def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['DdSdkReactNative_' + name]).toInteger()
}
android {
compileSdk getExtOrIntegerDefault('compileSdkVersion')
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
buildToolsVersion getExtOrDefault('buildToolsVersion')
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
namespace = "com.datadog.reactnative"
}
if (agpVersion.tokenize('.')[0].toInteger() >= 8) {
buildFeatures {
buildConfig = true
}
}
if (agpVersion.tokenize('.')[0].toInteger() < 8) {
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.majorVersion
}
}
defaultConfig {
compileSdk getExtOrIntegerDefault('compileSdkVersion')
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
versionCode 1
versionName "1.0"
buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
}
sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += ['src/newarch/kotlin']
} else {
java.srcDirs += ['src/oldarch/kotlin']
}
}
test {
java.srcDir("src/test/kotlin")
}
}
testOptions {
unitTests {
returnDefaultValues = true
}
}
buildTypes {
release {
minifyEnabled false
}
}
lintOptions {
disable 'GradleCompatible'
}
}
repositories {
mavenCentral()
google()
maven { url "https://jitpack.io" }
mavenLocal()
if (reactNativeMajorVersion == 0 && reactNativeMinorVersion < 71) {
def androidSourcesDir = file("$reactNativeRootDir/android")
def androidSourcesName = "React Native sources"
if (androidSourcesDir.exists()) {
maven {
url androidSourcesDir.toString()
name androidSourcesName
}
}
}
}
def kotlin_version = getExtOrDefault('kotlinVersion')
dependencies {
if (reactNativeMajorVersion == 0 && reactNativeMinorVersion < 71) {
// noinspection GradleDynamicVersion
api 'com.facebook.react:react-native:+'
} else {
// We specify the $reactNativeVersion, like it's done on the react-native-gradle-plugin, as the plugin is not applied in tests.
// There is no impact for apps as we apply the same logic:
// https://github.com/facebook/react-native/blob/e1a1e6aa8030bf11d691c3dcf7abd13b25175027/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/DependencyUtils.kt
api "com.facebook.react:react-android:$reactNativeVersion"
}
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compileOnly "com.squareup.okhttp3:okhttp:3.12.13"
// dd-sdk-android-rum requires androidx.metrics:metrics-performance.
// From 2.21.0, it uses 1.0.0-beta02, which requires Gradle 8.6.0.
// This breaks builds if the React Native target is below 0.76.0. as it relies on Gradle 8.5.0.
// To avoid this, we enforce 1.0.0-beta01 on RN < 0.76.0
if (reactNativeMinorVersion < 76) {
implementation("com.datadoghq:dd-sdk-android-rum:3.12.1") {
exclude group: "androidx.metrics", module: "metrics-performance"
}
implementation "androidx.metrics:metrics-performance:1.0.0-beta01"
// dd-sdk-android requires androidx.core:core and androidx.core:core-ktx
// From 3.8.0, it uses 1.15.0, which requires compileSdk 35.
// However, React Native versions below 0.76.0 use compileSdk 34,
// so we pin 1.13.1 on those, since it's the last version compatible with compileSdk 34.
// Use dependency constraints (strictly) so this requirement propagates to consumers.
implementation("androidx.core:core") {
version {
strictly("1.13.1")
}
}
implementation("androidx.core:core-ktx") {
version {
strictly("1.13.1")
}
}
} else {
implementation "com.datadoghq:dd-sdk-android-rum:3.12.1"
}
implementation "com.datadoghq:dd-sdk-android-logs:3.12.1"
implementation "com.datadoghq:dd-sdk-android-trace:3.12.1"
implementation "com.datadoghq:dd-sdk-android-webview:3.12.1"
implementation "com.datadoghq:dd-sdk-android-ndk:3.12.1"
implementation "com.datadoghq:dd-sdk-android-flags:3.12.1"
implementation "com.datadoghq:dd-sdk-android-internal:3.12.1"
implementation "com.datadoghq:dd-sdk-android-rum-prelaunch:3.12.1"
implementation "com.google.code.gson:gson:2.11.0"
testImplementation "org.junit.platform:junit-platform-launcher:1.6.2"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.2"
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.6.2"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.6.2"
testImplementation "org.mockito:mockito-junit-jupiter:3.4.6"
testImplementation "org.assertj:assertj-core:3.18.1"
testImplementation "com.github.xgouchet.Elmyr:core:1.3.1"
testImplementation "com.github.xgouchet.Elmyr:inject:1.3.1"
testImplementation "com.github.xgouchet.Elmyr:junit5:1.3.1"
testImplementation "com.github.xgouchet.Elmyr:jvm:1.3.1"
testImplementation "org.mockito.kotlin:mockito-kotlin:5.1.0"
testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation("org.mockito:mockito-inline:5.2.0")
unmock 'org.robolectric:android-all:4.4_r1-robolectric-r2'
detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:1.23.8"
detektPlugins "io.gitlab.arturbosch.detekt:detekt-rules-libraries:1.23.8"
}
unMock {
keep("android.os.Looper")
keep("android.os.MessageQueue")
keep("android.os.SystemProperties")
keep("android.view.Choreographer")
keep("android.view.DisplayEventReceiver")
keepStartingWith("org.json.")
}
tasks.withType(Test) {
useJUnitPlatform {
includeEngines("spek", "junit-jupiter", "junit-vintage")
}
jvmArgs(
"--add-opens=java.base/java.lang.reflect=ALL-UNNAMED",
"--add-opens=java.base/java.util.concurrent=ALL-UNNAMED"
)
reports {
junitXml.required.set(true)
html.required.set(true)
}
}
tasks.named("check") {
dependsOn("ktlintCheck")
dependsOn("detekt")
}
ktlint {
debug.set(false)
android.set(true)
outputToConsole.set(true)
ignoreFailures.set(false)
enableExperimentalRules.set(false)
filter {
exclude("**/generated/**")
include("**/kotlin/**")
}
}
detekt {
input = files("$projectDir/src/main/kotlin")
config = files("$projectDir/detekt.yml")
reports {
xml {
enabled = true
destination = file("build/reports/detekt.xml")
}
}
}
// This is needed to make unmock work. Choreographer class was loaded from android.jar of Android SDK
// (which contains only public API and no internals), which with unmock plugin it should be loaded from the special jar provided by unmock.
// The code below is from the unmock plugin. It seems like something (from RN tooling maybe?) overwrites the location of Choreographer
// to the default one, so we apply the unmock code once again
project.afterEvaluate {
def outputJarPath = "${project.buildDir}/intermediates/unmocked-android${project.name}.jar"
def unMockTask = project.tasks.findByName("unMock")
def outputJarDependency = project.files(outputJarPath).builtBy(unMockTask)
project.android.unitTestVariants.all { variant ->
variant.registerPreJavacGeneratedBytecode(outputJarDependency)
}
}