Last active
September 2, 2024 14:16
-
-
Save fishchev/e1ec28cbb962eb996f1707aafa608b9a to your computer and use it in GitHub Desktop.
Remove package attributre from AndroidManifest as gradle build step (so you don't have to explicitly clone/modify third party modules)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
credits: https://github.com/SabriGhazali | |
source: https://github.com/proyecto26/react-native-inappbrowser/issues/451#issuecomment-2275538714 | |
At project-level, android/build.gradle | |
allprojects { | |
subprojects { | |
afterEvaluate { project -> | |
if (project.hasProperty('android')) { | |
project.android { | |
if (namespace == null || namespace.isEmpty()) { | |
def defaultNamespace = project.group.toString().replace('.', '_') | |
namespace = defaultNamespace | |
} | |
buildFeatures { | |
buildConfig = true | |
} | |
} | |
// Task to ensure namespace and remove package attribute | |
project.tasks.register("fixManifestsAndNamespace") { | |
doLast { | |
// Ensure namespace in build.gradle | |
def buildGradleFile = file("${project.projectDir}/build.gradle") | |
if (buildGradleFile.exists()) { | |
def buildGradleContent = buildGradleFile.getText('UTF-8') | |
def manifestFile = file("${project.projectDir}/src/main/AndroidManifest.xml") | |
if (manifestFile.exists()) { | |
def manifestContent = manifestFile.getText('UTF-8') | |
def packageName = manifestContent.find(/package="([^"]+)"/) { match, p -> p } | |
if (packageName && !buildGradleContent.contains("namespace")) { | |
println "Setting namespace in ${buildGradleFile}" | |
buildGradleContent = buildGradleContent.replaceFirst( | |
/android\s*\{/, "android {\n namespace '${packageName}'" | |
) | |
buildGradleFile.write(buildGradleContent, 'UTF-8') | |
} | |
} | |
} | |
// Remove package attribute from AndroidManifest.xml | |
def manifests = fileTree(dir: project.projectDir, includes: ['**/AndroidManifest.xml']) | |
manifests.each { File manifestFile -> | |
def manifestContent = manifestFile.getText('UTF-8') | |
if (manifestContent.contains('package=')) { | |
println "Removing package attribute from ${manifestFile}" | |
manifestContent = manifestContent.replaceAll(/package="[^"]*"/, '') | |
manifestFile.write(manifestContent, 'UTF-8') | |
} | |
} | |
} | |
} | |
// Ensure the task runs before the build process | |
project.tasks.matching { it.name.startsWith("preBuild") }.all { | |
dependsOn project.tasks.named("fixManifestsAndNamespace") | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment