|
| 1 | +package com.callstack.react.brownfield.plugin |
| 2 | + |
| 3 | +import com.android.build.gradle.LibraryExtension |
| 4 | +import com.callstack.react.brownfield.utils.Extension |
| 5 | +import org.gradle.api.Project |
| 6 | +import org.gradle.api.file.Directory |
| 7 | +import org.gradle.api.tasks.Copy |
| 8 | + |
| 9 | +object RNSourceSets { |
| 10 | + private lateinit var project: Project |
| 11 | + private lateinit var extension: Extension |
| 12 | + private lateinit var androidExtension: LibraryExtension |
| 13 | + private lateinit var appProject: Project |
| 14 | + private lateinit var appBuildDir: Directory |
| 15 | + private lateinit var moduleBuildDir: Directory |
| 16 | + |
| 17 | + fun configure( |
| 18 | + project: Project, |
| 19 | + extension: Extension, |
| 20 | + ) { |
| 21 | + /** |
| 22 | + * Do not configure sourceSets for our example library. |
| 23 | + * The reason is that we expect some RN specific tasks to |
| 24 | + * be present on the consuming library, which is not the case |
| 25 | + * with our example library. |
| 26 | + */ |
| 27 | + if (project.name == "example-android-library") { |
| 28 | + return |
| 29 | + } |
| 30 | + this.project = project |
| 31 | + this.extension = extension |
| 32 | + |
| 33 | + androidExtension = RNSourceSets.project.extensions.getByName("android") as LibraryExtension |
| 34 | + appProject = RNSourceSets.project.rootProject.project(RNSourceSets.extension.appProjectName) |
| 35 | + appBuildDir = appProject.layout.buildDirectory.get() |
| 36 | + moduleBuildDir = RNSourceSets.project.layout.buildDirectory.get() |
| 37 | + |
| 38 | + configureSourceSets() |
| 39 | + configureTasks() |
| 40 | + } |
| 41 | + |
| 42 | + private fun configureSourceSets() { |
| 43 | + androidExtension.sourceSets.getByName("main") { |
| 44 | + it.assets.srcDirs("$appBuildDir/generated/assets/createBundleReleaseJsAndAssets") |
| 45 | + it.res.srcDirs("$appBuildDir/generated/res/createBundleReleaseJsAndAssets") |
| 46 | + it.java.srcDirs("$moduleBuildDir/generated/autolinking/src/main/java") |
| 47 | + } |
| 48 | + |
| 49 | + androidExtension.sourceSets.getByName("release") { |
| 50 | + it.jniLibs.srcDirs("libsRelease") |
| 51 | + } |
| 52 | + |
| 53 | + androidExtension.sourceSets.getByName("debug") { |
| 54 | + it.jniLibs.srcDirs("libsDebug") |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private fun configureTasks() { |
| 59 | + val projectName = project.name |
| 60 | + val appProjectName = appProject.name |
| 61 | + |
| 62 | + project.tasks.register("copyAutolinkingSources", Copy::class.java) { |
| 63 | + val path = "generated/autolinking/src/main/java" |
| 64 | + it.dependsOn(":$appProjectName:generateAutolinkingPackageList") |
| 65 | + it.from("$appBuildDir/$path") |
| 66 | + it.into("$moduleBuildDir/$path") |
| 67 | + } |
| 68 | + |
| 69 | + androidExtension.buildTypes.forEach { buildType -> |
| 70 | + val capitalisedBuildType = buildType.name.replaceFirstChar { it.titlecase() } |
| 71 | + val codegenTaskName = "generateCodegenSchemaFromJavaScript" |
| 72 | + val strippedNativeLibsPath = "$appBuildDir/intermediates/stripped_native_libs" |
| 73 | + val strippedDebugSymbolsPath = "strip${capitalisedBuildType}DebugSymbols/out/lib" |
| 74 | + |
| 75 | + val copyLibTask = |
| 76 | + project.tasks.register("copy${capitalisedBuildType}LibSources", Copy::class.java) { |
| 77 | + it.dependsOn(":$appProjectName:$codegenTaskName") |
| 78 | + it.dependsOn(":$appProjectName:strip${capitalisedBuildType}DebugSymbols") |
| 79 | + it.dependsOn(":$projectName:$codegenTaskName") |
| 80 | + |
| 81 | + it.from( |
| 82 | + "$strippedNativeLibsPath/${buildType.name}/$strippedDebugSymbolsPath", |
| 83 | + ) |
| 84 | + it.into(project.rootProject.file("$projectName/libs$capitalisedBuildType")) |
| 85 | + it.include("**/libappmodules.so", "**/libreact_codegen_*.so") |
| 86 | + } |
| 87 | + |
| 88 | + project.tasks.named("preBuild").configure { |
| 89 | + it.dependsOn("copyAutolinkingSources") |
| 90 | + it.dependsOn(copyLibTask) |
| 91 | + if (capitalisedBuildType == "Release") { |
| 92 | + it.dependsOn(":${appProject.name}:createBundleReleaseJsAndAssets") |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments