Skip to content

Commit 2e973cd

Browse files
committed
Use the library build directory for Android codegen autolinking
1 parent 4615319 commit 2e973cd

3 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,12 @@ class ReactPlugin : Plugin<Project> {
360360
// by ReactSettingsPlugin.kt
361361
val rootGeneratedAutolinkingFile =
362362
project.rootProject.layout.buildDirectory.file("generated/autolinking/autolinking.json")
363-
val pureCxxDependencies =
364-
getPureCxxCodegenDependencies(rootGeneratedAutolinkingFile.get().asFile)
363+
val dependencies =
364+
JsonUtils.fromAutolinkingConfigJson(rootGeneratedAutolinkingFile.get().asFile)
365+
?.dependencies
366+
?.values
367+
.orEmpty()
368+
val pureCxxDependencies = getPureCxxCodegenDependencies(dependencies)
365369
val pureCxxCodegenTasks =
366370
configurePureCxxDependenciesCodegen(
367371
project,
@@ -402,6 +406,23 @@ class ReactPlugin : Plugin<Project> {
402406
task.autolinkInputFile.set(rootGeneratedAutolinkingFile)
403407
task.generatedOutputDirectory.set(generatedAutolinkingJniDir)
404408

409+
// The CLI assumes the default Gradle build directory when it reports a generated path.
410+
// Resolve only that path against the library's layout; custom CMake projects own theirs.
411+
val codegenFile = "generated/source/codegen/jni/CMakeLists.txt"
412+
for (dependency in dependencies) {
413+
val android = dependency.platforms?.android ?: continue
414+
if (android.isPureCxxDependency == true || android.libraryName == null) continue
415+
val defaultPath = File(android.sourceDir, "build/$codegenFile").invariantSeparatorsPath
416+
if (android.cmakeListsPath != defaultPath) continue
417+
val library = project.rootProject.findProject(":${dependency.nameCleansed}") ?: continue
418+
task.generatedCmakePaths.put(
419+
dependency.name,
420+
library.layout.buildDirectory.file(codegenFile).map {
421+
it.asFile.invariantSeparatorsPath
422+
},
423+
)
424+
}
425+
405426
if (pureCxxDependencies.isNotEmpty()) {
406427
task.generatedPureCxxSourceDirectory.set(generatedPureCxxSourceDir)
407428
}
@@ -476,10 +497,9 @@ class ReactPlugin : Plugin<Project> {
476497
}
477498

478499
internal fun getPureCxxCodegenDependencies(
479-
autolinkingFile: File,
500+
dependencies: Collection<ModelAutolinkingDependenciesJson>,
480501
): List<ModelAutolinkingDependenciesJson> {
481-
val model = JsonUtils.fromAutolinkingConfigJson(autolinkingFile)
482-
return model?.dependencies?.values?.filter { dependency ->
502+
return dependencies.filter { dependency ->
483503
val android = dependency.platforms?.android
484504

485505
if (android?.isPureCxxDependency != true || android.libraryName == null) {
@@ -489,7 +509,7 @@ class ReactPlugin : Plugin<Project> {
489509
val packageJson = File(dependency.root, "package.json")
490510
val codegenConfig = JsonUtils.fromPackageJson(packageJson)?.codegenConfig
491511
codegenConfig != null && codegenConfig.includesGeneratedCode != true
492-
} ?: emptyList()
512+
}
493513
}
494514

495515
internal fun taskNameSuffixForDependency(dependency: ModelAutolinkingDependenciesJson): String =

‎packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateAutolinkingNewArchitecturesFileTask.kt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import java.io.File
1414
import org.gradle.api.DefaultTask
1515
import org.gradle.api.file.DirectoryProperty
1616
import org.gradle.api.file.RegularFileProperty
17+
import org.gradle.api.provider.MapProperty
18+
import org.gradle.api.tasks.Input
1719
import org.gradle.api.tasks.InputDirectory
1820
import org.gradle.api.tasks.InputFile
1921
import org.gradle.api.tasks.Optional
@@ -28,6 +30,8 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
2830

2931
@get:InputFile abstract val autolinkInputFile: RegularFileProperty
3032

33+
@get:Input abstract val generatedCmakePaths: MapProperty<String, String>
34+
3135
@get:OutputDirectory abstract val generatedOutputDirectory: DirectoryProperty
3236

3337
@get:Optional @get:InputDirectory abstract val generatedPureCxxSourceDirectory: DirectoryProperty
@@ -50,7 +54,11 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
5054
internal fun filterAndroidPackages(
5155
model: ModelAutolinkingConfigJson?,
5256
): List<ModelAutolinkingDependenciesPlatformAndroidJson> =
53-
model?.dependencies?.values?.mapNotNull { it.platforms?.android } ?: emptyList()
57+
model?.dependencies?.values?.mapNotNull { dependency ->
58+
val android = dependency.platforms?.android ?: return@mapNotNull null
59+
val generatedPath = generatedCmakePaths.get()[dependency.name] ?: return@mapNotNull android
60+
android.copy(cmakeListsPath = generatedPath)
61+
} ?: emptyList()
5462

5563
internal fun generateCmakeFileContent(
5664
packages: List<ModelAutolinkingDependenciesPlatformAndroidJson>,

‎packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/ReactPluginTest.kt‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
package com.facebook.react
99

1010
import com.facebook.react.model.ModelAutolinkingDependenciesJson
11+
import com.facebook.react.tasks.GenerateAutolinkingNewArchitecturesFileTask
12+
import com.facebook.react.utils.JsonUtils
1113
import java.io.File
1214
import org.assertj.core.api.Assertions.assertThat
15+
import org.gradle.testfixtures.ProjectBuilder
1316
import org.intellij.lang.annotations.Language
1417
import org.junit.Rule
1518
import org.junit.Test
@@ -19,6 +22,53 @@ class ReactPluginTest {
1922

2023
@get:Rule val tempFolder = TemporaryFolder()
2124

25+
@Test
26+
fun autolinking_usesLibraryBuildDirectoryForGeneratedCmake() {
27+
val root = ProjectBuilder.builder().withProjectDir(tempFolder.root).build()
28+
val app = ProjectBuilder.builder().withName("app").withParent(root).build()
29+
val lib = ProjectBuilder.builder().withName("worklets").withParent(root).build()
30+
app.projectDir.mkdirs()
31+
lib.projectDir.mkdirs()
32+
val sourceDir = lib.projectDir.invariantSeparatorsPath
33+
val jniDir = "generated/source/codegen/jni"
34+
val config =
35+
root.layout.buildDirectory.file("generated/autolinking/autolinking.json").get().asFile
36+
config.parentFile.mkdirs()
37+
config.writeText(
38+
"""
39+
{
40+
"reactNativeVersion": "1000.0.0",
41+
"dependencies": {
42+
"worklets": {
43+
"root": "$sourceDir", "name": "worklets",
44+
"platforms": {"android": {
45+
"sourceDir": "$sourceDir", "libraryName": "rnworklets",
46+
"componentDescriptors": [], "buildTypes": [],
47+
"packageImportPath": "import test.Worklets;", "packageInstance": "new Worklets()",
48+
"cmakeListsPath": "$sourceDir/build/$jniDir/CMakeLists.txt"
49+
}}
50+
}
51+
}
52+
}
53+
"""
54+
.trimIndent()
55+
)
56+
app.plugins.apply("com.android.application")
57+
app.plugins.apply(ReactPlugin::class.java)
58+
val task =
59+
app.tasks.getByName("generateAutolinkingNewArchitectureFiles")
60+
as GenerateAutolinkingNewArchitecturesFileTask
61+
val moved = root.layout.buildDirectory.dir("modules/worklets")
62+
lib.layout.buildDirectory.set(moved)
63+
task.taskAction()
64+
val cmake =
65+
task.generatedOutputDirectory.file("Android-autolinking.cmake").get().asFile.readText()
66+
assertThat(cmake)
67+
.contains(
68+
"add_subdirectory(\"${moved.get().asFile.invariantSeparatorsPath}/$jniDir/\" rnworklets_autolinked_build)"
69+
)
70+
}
71+
2272
@Test
2373
fun getPureCxxCodegenDependencies_filtersDependenciesCorrectly() {
2474
val includesGeneratedCode = createPackage("includes-generated-code", true)
@@ -109,7 +159,9 @@ class ReactPluginTest {
109159
.trimIndent(),
110160
)
111161

112-
val result = ReactPlugin().getPureCxxCodegenDependencies(autolinkingFile)
162+
val dependencies =
163+
JsonUtils.fromAutolinkingConfigJson(autolinkingFile)?.dependencies?.values.orEmpty()
164+
val result = ReactPlugin().getPureCxxCodegenDependencies(dependencies)
113165

114166
assertThat(result.map { it.name })
115167
.containsExactly("without-generated-code", "without-includes-generated-code")

0 commit comments

Comments
 (0)