Skip to content

Commit 48af341

Browse files
authored
fix: dependency downloads on v3 (#3588)
## Description Fixes #3523 on v3 Fix for v2 comes with #3602 ## Changes Added `resolve<lib>` tasks. Each of them check whether `<lib>` is already downloaded by `:ReactAndroid` project - if so - they copy archive files to `reanimated` downloads directory -> this causes `download<lib>` tasks to not run, as they're up to date. To achieve this, setting dependency between `resolve<lib>` & `:ReactAndroid:download<lib>` was required. ## Test code and steps to reproduce I checked it on Reanimated v3: * [x] `FabricExample` - `rm -fr node_modules && yarn`, run the build for the first time, see that the libraries are downloaded only once by `:ReactAndroid` project. In consecutive builds libraries are not downloaded. * [x] `Example` - `rm -fr node_modules && yarn`, run the build for the first time, see that the libraries are downloaded by `:react-native-reanimated` project. In consecutive build libraries are not downloaded. * [x] Fresh React Native application (both archs) ## Checklist - [x] Ensured that CI passes
1 parent 09a697f commit 48af341

1 file changed

Lines changed: 56 additions & 9 deletions

File tree

android/build.gradle

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import com.android.Version
2+
import org.apache.tools.ant.filters.ReplaceTokens
23

3-
import java.nio.file.Files
44
import java.nio.file.Paths
5-
import java.nio.file.Path
6-
import org.apache.tools.ant.filters.ReplaceTokens
7-
import java.util.stream.Stream
85

96
/**
107
* Finds the path of the installed npm package with the given name using Node's
@@ -154,6 +151,7 @@ def downloadsDir = customDownloadsDir ? new File(customDownloadsDir) : new File(
154151
def thirdPartyNdkDir = new File("$buildDir/third-party-ndk")
155152

156153
def reactNativeThirdParty = new File("$reactNativeRootDir/ReactAndroid/src/main/jni/third-party")
154+
def reactNativeAndroidDownloadDir = new File("$reactNativeRootDir/ReactAndroid/build/downloads")
157155

158156
def _stackProtectorFlag = true
159157

@@ -389,16 +387,65 @@ task createNativeDepsDirectories(dependsOn: applyJavaPatches) {
389387
thirdPartyNdkDir.mkdirs()
390388
}
391389

392-
task downloadBoost(dependsOn: createNativeDepsDirectories, type: Download) {
390+
def resolveTaskFactory(String taskName, String artifactLocalName, File reactNativeAndroidDownloadDir, File reanimatedDownloadDir) {
391+
return tasks.create(name: taskName, dependsOn: createNativeDepsDirectories, type: Copy) {
392+
from reactNativeAndroidDownloadDir
393+
include artifactLocalName
394+
into reanimatedDownloadDir
395+
396+
onlyIf {
397+
// First we check whether the file is already in our download directory
398+
if (file("$reanimatedDownloadDir/$artifactLocalName").isFile()) {
399+
return false
400+
}
401+
402+
// If it is not the case we check whether it was downloaded by ReactAndroid project
403+
if (file("$reactNativeAndroidDownloadDir/$artifactLocalName").isFile()) {
404+
return true
405+
}
406+
407+
return false
408+
}
409+
}
410+
}
411+
412+
Task resolveBoost = resolveTaskFactory("resolveBoost", "boost_${BOOST_VERSION}.tar.gz", reactNativeAndroidDownloadDir, downloadsDir)
413+
Task resolveDoubleConversion = resolveTaskFactory("resolveDoubleConversion", "double-conversion-${DOUBLE_CONVERSION_VERSION}.tar.gz", reactNativeAndroidDownloadDir, downloadsDir)
414+
Task resolveFolly = resolveTaskFactory("resolveFolly", "folly-${FOLLY_VERSION}.tar.gz", reactNativeAndroidDownloadDir, downloadsDir)
415+
Task resolveGlog = resolveTaskFactory("resolveGlog", "glog-${GLOG_VERSION}.tar.gz", reactNativeAndroidDownloadDir, downloadsDir)
416+
417+
if (isNewArchitectureEnabled()) {
418+
def reactNativeAndroidProject = findProject(":ReactAndroid")
419+
if (reactNativeAndroidProject != null) {
420+
reactNativeAndroidProject.afterEvaluate {
421+
def resolveTasks = [resolveBoost, resolveGlog, resolveDoubleConversion, resolveFolly]
422+
resolveTasks.forEach({ task ->
423+
String reactAndroidDownloadTaskName = "download" + task.name.replace("resolve", "")
424+
def reactAndroidDownloadTask = reactNativeAndroidProject.getTasks().findByName(reactAndroidDownloadTaskName)
425+
if (reactAndroidDownloadTask != null) {
426+
task.dependsOn(reactAndroidDownloadTask)
427+
} else {
428+
logger.warn("[Reanimated] Failed to find task named `$reactAndroidDownloadTaskName` in `:ReactAndroid` project." +
429+
" Explicit dependency between it and $task.name task can not be set.")
430+
}
431+
})
432+
}
433+
} else {
434+
throw new GradleScriptException("[Reanimated] Failed to find `:ReactAndroid` project. Explicit dependency between download tasks can not be set.")
435+
}
436+
}
437+
438+
task downloadBoost(dependsOn: resolveBoost, type: Download) {
393439
def transformedVersion = BOOST_VERSION.replace("_", ".")
440+
def artifactLocalName = "boost_${BOOST_VERSION}.tar.gz"
394441
def srcUrl = "https://boostorg.jfrog.io/artifactory/main/release/${transformedVersion}/source/boost_${BOOST_VERSION}.tar.gz"
395442
if (REACT_NATIVE_MINOR_VERSION < 69) {
396443
srcUrl = "https://github.com/react-native-community/boost-for-react-native/releases/download/v${transformedVersion}-0/boost_${BOOST_VERSION}.tar.gz"
397444
}
398445
src(srcUrl)
399446
onlyIfNewer(true)
400447
overwrite(false)
401-
dest(new File(downloadsDir, "boost_${BOOST_VERSION}.tar.gz"))
448+
dest(new File(downloadsDir, artifactLocalName))
402449
}
403450

404451
task prepareBoost(dependsOn: boostPath ? [] : [downloadBoost], type: Copy) {
@@ -412,7 +459,7 @@ task prepareBoost(dependsOn: boostPath ? [] : [downloadBoost], type: Copy) {
412459
}
413460
}
414461

415-
task downloadDoubleConversion(dependsOn: createNativeDepsDirectories, type: Download) {
462+
task downloadDoubleConversion(dependsOn: resolveDoubleConversion, type: Download) {
416463
src("https://github.com/google/double-conversion/archive/v${DOUBLE_CONVERSION_VERSION}.tar.gz")
417464
onlyIfNewer(true)
418465
overwrite(false)
@@ -428,7 +475,7 @@ task prepareDoubleConversion(dependsOn: dependenciesPath ? [] : [downloadDoubleC
428475
into("$thirdPartyNdkDir/double-conversion")
429476
}
430477

431-
task downloadFolly(dependsOn: createNativeDepsDirectories, type: Download) {
478+
task downloadFolly(dependsOn: resolveFolly, type: Download) {
432479
src("https://github.com/facebook/folly/archive/v${FOLLY_VERSION}.tar.gz")
433480
onlyIfNewer(true)
434481
overwrite(false)
@@ -447,7 +494,7 @@ task prepareFolly(dependsOn: dependenciesPath ? [] : [downloadFolly], type: Copy
447494
into("$thirdPartyNdkDir/folly")
448495
}
449496

450-
task downloadGlog(dependsOn: createNativeDepsDirectories, type: Download) {
497+
task downloadGlog(dependsOn: resolveGlog, type: Download) {
451498
src("https://github.com/google/glog/archive/v${GLOG_VERSION}.tar.gz")
452499
onlyIfNewer(true)
453500
overwrite(false)

0 commit comments

Comments
 (0)