Skip to content
2 changes: 1 addition & 1 deletion docs/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ An array of iOS script phases to add to the project. Specifying a `path` propert
module.exports = {
dependency: {
platforms: {
ios: {
ios: {
scriptPhases: [
{
name: '[MY DEPENDENCY] My Script',
Expand Down
34 changes: 30 additions & 4 deletions packages/platform-android/native_modules.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,37 @@ class ReactNativeModules {
String packageClassInstances = ""

if (packages.size() > 0) {
packageImports = "import ${packageName}.BuildConfig;\nimport ${packageName}.R;\n\n"
packageImports = packageImports + packages.collect {
"// ${it.name}\n${it.packageImportPath}"
def interpolateDynamicValues = {
it
// Before adding the package replacement mechanism,
// BuildConfig and R classes were imported automatically
// into the scope of the file. We want to replace all
// non-FQDN references to those classes with the package name
// of the MainApplication.
//
// We want to match "R" or "BuildConfig":
// - new Package(R.string…),
// - Module.configure(BuildConfig);
// ^ hence including (BuildConfig|R)
// but we don't want to match "R":
// - new Package(getResources…),
// - new PackageR…,
// - new Royal…,
// ^ hence excluding \w before and after matches
// and "BuildConfig" that has FQDN reference:
// - Module.configure(com.acme.BuildConfig);
// ^ hence excluding . before the match.
.replaceAll(~/([^.\w])(BuildConfig|R)([^\w])/, {
wholeString, prefix, className, suffix ->
"${prefix}${packageName}.${className}${suffix}"
})
}
packageImports = packages.collect {
"// ${it.name}\n${interpolateDynamicValues(it.packageImportPath)}"
}.join('\n')
packageClassInstances = ",\n " + packages.collect { it.packageInstance }.join(",\n ")
packageClassInstances = ",\n " + packages.collect {
interpolateDynamicValues(it.packageInstance)
}.join(",\n ")
}

String generatedFileContents = generatedFileContentsTemplate
Expand Down