From 3e8984106d486fdcde74b5acf28ba34d037c4f60 Mon Sep 17 00:00:00 2001 From: Georgy Steshin Date: Sun, 24 Nov 2024 18:34:01 +0200 Subject: [PATCH] Improved android build scripts --- detox/android/rninfo.gradle | 53 ++++++++++--------- detox/android/settings.gradle | 10 ++-- detox/test/android/settings.gradle | 11 ++-- .../demo-react-native/android/settings.gradle | 10 ++-- 4 files changed, 41 insertions(+), 43 deletions(-) diff --git a/detox/android/rninfo.gradle b/detox/android/rninfo.gradle index c09d2af1d2..6e7282b52e 100644 --- a/detox/android/rninfo.gradle +++ b/detox/android/rninfo.gradle @@ -1,11 +1,24 @@ import groovy.json.JsonSlurper -def getRNVersion = { workingDir -> - println("RNInfo: workingDir=$workingDir") +def getRNPackageJsonDir() { + def currentDir = rootDir + while (currentDir != null) { + def nodeModulesDir = new File(currentDir, "node_modules/react-native") + def file = new File(nodeModulesDir, 'package.json') + if (file.exists()) { + return file.path + } + currentDir = currentDir.parentFile + } + throw new GradleException("Unable to find module: $moduleName") +} + +def getRNVersion = { rnPackageJsonPath -> + println("RNInfo: package.json=$rnPackageJsonPath") def jsonSlurper = new JsonSlurper() - def packageFile = "$workingDir/../node_modules/react-native/package.json" - println("RNInfo: reading $packageFile") - Map packageJSON = jsonSlurper.parse(new File(packageFile)) + def packageFile = + println("RNInfo: reading $rnPackageJsonPath") + Map packageJSON = jsonSlurper.parse(new File(rnPackageJsonPath)) String rnVersion = packageJSON.get('version') return rnVersion } @@ -15,24 +28,16 @@ def getMajorVersionInternal = { semanticVersion -> return rnVersionMajor } -ext.getRnMajorVersion = { workingDir -> - String rnVersion = getRNVersion(workingDir) - Integer rnVersionMajor = getMajorVersionInternal(rnVersion) - return rnVersionMajor -} - -def rnVersion = getRNVersion(rootDir) +def rnVersion = getRNVersion(getRNPackageJsonDir()) def rnMajorVer = getMajorVersionInternal(rnVersion) -if (hasProperty('project')) { - println "[$project] RNInfo: detected React Native version: $rnVersion (major=$rnMajorVer)" +println "RNInfo: detected React Native version: $rnVersion (major=$rnMajorVer)" - project.ext.rnInfo = [ - version : rnVersion, - majorVersion : rnMajorVer, - isRN69OrHigher: rnMajorVer >= 69, - isRN70OrHigher: rnMajorVer >= 70, - isRN71OrHigher: rnMajorVer >= 71, - isRN72OrHigher: rnMajorVer >= 72, - isRN73OrHigher: rnMajorVer >= 73, - ] -} +ext.rnInfo = [ + version : rnVersion, + majorVersion : rnMajorVer, + isRN69OrHigher: rnMajorVer >= 69, + isRN70OrHigher: rnMajorVer >= 70, + isRN71OrHigher: rnMajorVer >= 71, + isRN72OrHigher: rnMajorVer >= 72, + isRN73OrHigher: rnMajorVer >= 73, +] diff --git a/detox/android/settings.gradle b/detox/android/settings.gradle index 7a926a68c3..2541ebac02 100644 --- a/detox/android/settings.gradle +++ b/detox/android/settings.gradle @@ -3,12 +3,10 @@ include ':detox' println("RNInfo: rootDir=$rootDir") -def rnMajorVer = getRnMajorVersion(rootDir) -println "[settings] RNInfo: detected React Native version: (major=$rnMajorVer)" +println "[settings] RNInfo: detected React Native version: (major=${ext.rnInfo.version})" -if (rnMajorVer < 72) { - includeBuild('../node_modules/react-native-gradle-plugin') -} else { +if (ext.rnInfo.isRN72OrHigher) { includeBuild('../node_modules/@react-native/gradle-plugin') +} else { + includeBuild('../node_modules/react-native-gradle-plugin') } - diff --git a/detox/test/android/settings.gradle b/detox/test/android/settings.gradle index 118655f99b..817e335210 100644 --- a/detox/test/android/settings.gradle +++ b/detox/test/android/settings.gradle @@ -2,17 +2,14 @@ apply from: file("../../android/rninfo.gradle") rootProject.name = 'DetoxTest' include ':app' -def rnMajorVer = getRnMajorVersion(rootDir) -println "[settings] RNInfo: detected React Native version: (major=$rnMajorVer)" +println "[settings] RNInfo: detected React Native version: (major=${ext.rnInfo.version})" -if (rnMajorVer < 72) { - includeBuild('../node_modules/react-native-gradle-plugin') -} else { +if (ext.rnInfo.isRN72OrHigher) { includeBuild('../node_modules/@react-native/gradle-plugin') +} else { + includeBuild('../node_modules/react-native-gradle-plugin') } - - include ':detox' project(':detox').projectDir = new File(rootProject.projectDir, '../../android/detox') diff --git a/examples/demo-react-native/android/settings.gradle b/examples/demo-react-native/android/settings.gradle index ca091cf2fb..fb6bbf47c1 100644 --- a/examples/demo-react-native/android/settings.gradle +++ b/examples/demo-react-native/android/settings.gradle @@ -3,14 +3,12 @@ rootProject.name = 'DetoxRNExample' include ':app' -println("RNInfo: rootDir=$rootDir") -def rnMajorVer = getRnMajorVersion(rootDir) -println("[settings] RNInfo: detected React Native version: (major=$rnMajorVer)") +println("[settings] RNInfo: detected React Native version: (major=${ext.rnInfo.version})") -if (rnMajorVer < 72) { - includeBuild('../node_modules/react-native-gradle-plugin') -} else { +if (ext.rnInfo.isRN72OrHigher) { includeBuild('../node_modules/@react-native/gradle-plugin') +} else { + includeBuild('../node_modules/react-native-gradle-plugin') }