Skip to content

Commit d695144

Browse files
authored
feat(Android): Prepare auto-linking for double publishing (#2076)
1 parent fc87a75 commit d695144

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

Diff for: packages/cli-platform-android/native_modules.gradle

+31-12
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ set(CMAKE_VERBOSE_MAKEFILE on)
7575
7676
{{ libraryIncludes }}
7777
78-
set(AUTOLINKED_LIBRARIES
78+
set(AUTOLINKED_LIBRARIES
7979
{{ libraryModules }}
8080
)
8181
"""
@@ -143,18 +143,20 @@ class ReactNativeModules {
143143
private ArrayList<HashMap<String, String>> reactNativeModules
144144
private ArrayList<String> unstable_reactLegacyComponentNames
145145
private HashMap<String, ArrayList> reactNativeModulesBuildVariants
146+
private String reactNativeVersion
146147

147148
private static String LOG_PREFIX = ":ReactNative:"
148149

149150
ReactNativeModules(Logger logger, File root) {
150151
this.logger = logger
151152
this.root = root
152153

153-
def (nativeModules, reactNativeModulesBuildVariants, androidProject) = this.getReactNativeConfig()
154+
def (nativeModules, reactNativeModulesBuildVariants, androidProject, reactNativeVersion) = this.getReactNativeConfig()
154155
this.reactNativeModules = nativeModules
155156
this.reactNativeModulesBuildVariants = reactNativeModulesBuildVariants
156157
this.packageName = androidProject["packageName"]
157158
this.unstable_reactLegacyComponentNames = androidProject["unstable_reactLegacyComponentNames"]
159+
this.reactNativeVersion = reactNativeVersion
158160
}
159161

160162
/**
@@ -180,7 +182,7 @@ class ReactNativeModules {
180182
if (reactNativeModulesBuildVariants.containsKey(nameCleansed)) {
181183
reactNativeModulesBuildVariants
182184
.get(nameCleansed)
183-
.forEach { buildVariant ->
185+
.forEach { buildVariant ->
184186
if(dependencyConfiguration != null) {
185187
"${buildVariant}${dependencyConfiguration}"
186188
} else {
@@ -218,7 +220,7 @@ class ReactNativeModules {
218220
// Before adding the package replacement mechanism,
219221
// BuildConfig and R classes were imported automatically
220222
// into the scope of the file. We want to replace all
221-
// non-FQDN references to those classes with the package name
223+
// non-FQDN references to those classes with the package name
222224
// of the MainApplication.
223225
//
224226
// We want to match "R" or "BuildConfig":
@@ -324,7 +326,7 @@ class ReactNativeModules {
324326
result += it.componentDescriptors.collect {
325327
" providerRegistry->add(concreteComponentDescriptorProvider<${it}>());"
326328
}.join('\n')
327-
}
329+
}
328330
result
329331
}.join("\n")
330332
}
@@ -403,7 +405,7 @@ class ReactNativeModules {
403405

404406
ArrayList<HashMap<String, String>> reactNativeModules = new ArrayList<HashMap<String, String>>()
405407
HashMap<String, ArrayList> reactNativeModulesBuildVariants = new HashMap<String, ArrayList>()
406-
408+
407409
/**
408410
* Resolve the CLI location from Gradle file
409411
*
@@ -427,6 +429,7 @@ class ReactNativeModules {
427429
}
428430
def dependencies = json["dependencies"]
429431
def project = json["project"]["android"]
432+
def reactNativeVersion = json["version"]
430433

431434
if (project == null) {
432435
throw new Exception("React Native CLI failed to determine Android project configuration. This is likely due to misconfiguration. Config output:\n${json.toMapString()}")
@@ -440,7 +443,7 @@ class ReactNativeModules {
440443

441444
if (androidConfig != null && androidConfig["sourceDir"] != null) {
442445
this.logger.info("${LOG_PREFIX}Automatically adding native module '${name}'")
443-
446+
444447
HashMap reactNativeModuleConfig = new HashMap<String, String>()
445448
def nameCleansed = name.replaceAll('[~*!\'()]+', '_').replaceAll('^@([\\w-.]+)/', '$1_')
446449
reactNativeModuleConfig.put("name", name)
@@ -465,14 +468,14 @@ class ReactNativeModules {
465468
}
466469

467470
this.logger.trace("${LOG_PREFIX}'${name}': ${reactNativeModuleConfig.toMapString()}")
468-
471+
469472
reactNativeModules.add(reactNativeModuleConfig)
470473
} else {
471474
this.logger.info("${LOG_PREFIX}Skipping native module '${name}'")
472475
}
473476
}
474477

475-
return [reactNativeModules, reactNativeModulesBuildVariants, json["project"]["android"]];
478+
return [reactNativeModules, reactNativeModulesBuildVariants, json["project"]["android"], reactNativeVersion];
476479
}
477480
}
478481

@@ -485,6 +488,21 @@ def projectRoot = rootProject.projectDir
485488

486489
def autoModules = new ReactNativeModules(logger, projectRoot)
487490

491+
def reactNativeVersionRequireNewArchEnabled(autoModules) {
492+
def rnVersion = autoModules.reactNativeVersion
493+
def regexPattern = /^(\d+)\.(\d+)\.(\d+)(?:-(\w+(?:[-.]\d+)?))?$/
494+
495+
if (rnVersion =~ regexPattern) {
496+
def result = (rnVersion =~ regexPattern).findAll().first()
497+
498+
def major = result[1].toInteger()
499+
if (major > 0 && major < 1000) {
500+
return true
501+
}
502+
}
503+
return false
504+
}
505+
488506
/** -----------------------
489507
* Exported Extensions
490508
* ------------------------ */
@@ -516,15 +534,16 @@ ext.applyNativeModulesAppBuildGradle = { Project project, String root = null ->
516534

517535
task generateNewArchitectureFiles {
518536
doLast {
519-
autoModules.generateCmakeFile(generatedJniDir, "Android-rncli.cmake", cmakeTemplate)
537+
autoModules.generateCmakeFile(generatedJniDir, "Android-rncli.cmake", cmakeTemplate)
520538
autoModules.generateRncliCpp(generatedJniDir, "rncli.cpp", rncliCppTemplate)
521539
autoModules.generateRncliH(generatedJniDir, "rncli.h", rncliHTemplate)
522540
}
523541
}
524542

525543
preBuild.dependsOn generatePackageList
526-
527-
if (project.hasProperty("newArchEnabled") && project.newArchEnabled == "true") {
544+
def isNewArchEnabled = (project.hasProperty("newArchEnabled") && project.newArchEnabled == "true") ||
545+
reactNativeVersionRequireNewArchEnabled(autoModules)
546+
if (isNewArchEnabled) {
528547
preBuild.dependsOn generateNewArchitectureFiles
529548
}
530549

0 commit comments

Comments
 (0)