Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure namespace is specified for all the 3rd party libraries #41085

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.facebook.react.tasks.GenerateCodegenSchemaTask
import com.facebook.react.utils.AgpConfiguratorUtils.configureBuildConfigFieldsForApp
import com.facebook.react.utils.AgpConfiguratorUtils.configureBuildConfigFieldsForLibraries
import com.facebook.react.utils.AgpConfiguratorUtils.configureDevPorts
import com.facebook.react.utils.AgpConfiguratorUtils.configureNamespaceForLibraries
import com.facebook.react.utils.BackwardCompatUtils.configureBackwardCompatibilityReactMap
import com.facebook.react.utils.DependencyUtils.configureDependencies
import com.facebook.react.utils.DependencyUtils.configureRepositories
Expand Down Expand Up @@ -82,6 +83,7 @@ class ReactPlugin : Plugin<Project> {

// Library Only Configuration
configureBuildConfigFieldsForLibraries(project)
configureNamespaceForLibraries(project)
project.pluginManager.withPlugin("com.android.library") {
configureCodegen(project, extension, rootExtension, isLibrary = true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import com.facebook.react.utils.ProjectUtils.isNewArchEnabled
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.plugins.AppliedPlugin
import java.io.File
import com.android.build.gradle.LibraryExtension
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Element

@Suppress("UnstableApiUsage")
internal object AgpConfiguratorUtils {
Expand Down Expand Up @@ -63,6 +68,40 @@ internal object AgpConfiguratorUtils {
project.pluginManager.withPlugin("com.android.application", action)
project.pluginManager.withPlugin("com.android.library", action)
}
}

fun configureNamespaceForLibraries(appProject: Project) {
appProject.rootProject.allprojects { subproject ->
subproject.pluginManager.withPlugin("com.android.library") {
subproject.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { ext ->
if(ext.namespace == null){
val android = subproject.extensions.getByType(LibraryExtension::class.java)
val manifestFile = android.sourceSets.getByName("main").manifest.srcFile

manifestFile.takeIf { it.exists() }?.let { file ->
getPackageNameFromManifest(file)?.let { packageName ->
ext.namespace = packageName
}
}
}
}
}
}
}
}
const val DEFAULT_DEV_SERVER_PORT = "8081"

fun getPackageNameFromManifest(manifest: File): String? {
val factory: DocumentBuilderFactory = DocumentBuilderFactory.newInstance()
val builder: DocumentBuilder = factory.newDocumentBuilder()

try {
val xmlDocument = builder.parse(manifest)

val manifestElement = xmlDocument.getElementsByTagName("manifest").item(0) as? Element
val packageName = manifestElement?.getAttribute("package")

return if (packageName.isNullOrEmpty()) null else packageName
} catch (e: Exception) {
return null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.utils

import java.io.File
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

class AgpConfiguratorUtilsTest {

@get:Rule val tempFolder = TemporaryFolder()

@Test
fun getPackageNameFromManifest_withEmptyFile_returnsNull() {
val mainFolder = tempFolder.newFolder("awesome-module/src/main/")
val manifest = File(mainFolder, "AndroidManifest.xml").apply {
writeText("")
}

val actual = getPackageNameFromManifest(manifest)
assertNull(actual)
}

@Test
fun getPackageNameFromManifest_withMissingPackage_returnsNull() {
val mainFolder = tempFolder.newFolder("awesome-module/src/main/")
val manifest = File(mainFolder, "AndroidManifest.xml").apply {
writeText(
// language=xml
"""
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
"""
.trimIndent())
}

val actual = getPackageNameFromManifest(manifest)
assertNull(actual)
}

@Test
fun getPackageNameFromManifest_withPackage_returnsPackage() {
val mainFolder = tempFolder.newFolder("awesome-module/src/main/")
val manifest = File(mainFolder, "AndroidManifest.xml").apply {
writeText(
// language=xml
"""
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.facebook.react" >
</manifest>
"""
.trimIndent())
}

val actual = getPackageNameFromManifest(manifest)
assertNotNull(actual)
assertEquals("com.facebook.react", actual)
}
}