Skip to content

Commit 59f472b

Browse files
committed
feat: add support for RN entry point introduced in RN 0.80
1 parent 523f585 commit 59f472b

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.callstack.react.brownfield.exceptions
2+
3+
class NameSpaceNotFound(message: String) :
4+
RuntimeException(message)

gradle-plugins/react/brownfield/src/main/kotlin/com/callstack/react/brownfield/plugin/RNSourceSets.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.callstack.react.brownfield.plugin
22

33
import com.android.build.gradle.LibraryExtension
4+
import com.callstack.react.brownfield.exceptions.NameSpaceNotFound
45
import com.callstack.react.brownfield.utils.Extension
56
import org.gradle.api.Project
7+
import org.gradle.api.Task
68
import org.gradle.api.file.Directory
79
import org.gradle.api.tasks.Copy
10+
import java.io.File
811

912
object RNSourceSets {
1013
private lateinit var project: Project
@@ -55,6 +58,46 @@ object RNSourceSets {
5558
}
5659
}
5760

61+
private fun getLibraryNameSpace(): String {
62+
val nameSpace = androidExtension.namespace
63+
return nameSpace ?: throw NameSpaceNotFound("namespace must be defined in your android library build.gradle")
64+
}
65+
66+
private fun patchRNEntryPoint(
67+
task: Task,
68+
path: String,
69+
) {
70+
val rnEntryPointTaskName = "generateReactNativeEntryPoint"
71+
val rnEntryPointTask = appProject.tasks.named(rnEntryPointTaskName)
72+
73+
/**
74+
* If `generateReactNativeEntryPoint` task does not exist, we early return. It means
75+
* the consumer library is running on RN version < 0.80
76+
*/
77+
if (!rnEntryPointTask.isPresent) {
78+
return
79+
}
80+
81+
task.dependsOn(rnEntryPointTask)
82+
val sourceFile = File(moduleBuildDir.toString(), "$path/com/facebook/react/ReactNativeApplicationEntryPoint.java")
83+
task.doLast {
84+
if (sourceFile.exists()) {
85+
var content = sourceFile.readText()
86+
val nameSpace = getLibraryNameSpace()
87+
88+
/**
89+
* We use look-ahead regex to replace any occurrences with Build.Config referenced via the old(app) package
90+
*
91+
* \b[\w.]+ → matches the old package
92+
* (?=\.BuildConfig) → only if it’s immediately followed by that suffix
93+
*/
94+
val regex = Regex("""\b[\w.]+(?=\.BuildConfig)""")
95+
content = content.replace(regex, nameSpace)
96+
sourceFile.writeText(content)
97+
}
98+
}
99+
}
100+
58101
private fun configureTasks() {
59102
val projectName = project.name
60103
val appProjectName = appProject.name
@@ -64,6 +107,8 @@ object RNSourceSets {
64107
it.dependsOn(":$appProjectName:generateAutolinkingPackageList")
65108
it.from("$appBuildDir/$path")
66109
it.into("$moduleBuildDir/$path")
110+
111+
patchRNEntryPoint(it, path)
67112
}
68113

69114
androidExtension.buildTypes.forEach { buildType ->

0 commit comments

Comments
 (0)