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

Android build failed - Namespace not specified #186

Open
kouz75 opened this issue Jul 18, 2024 · 2 comments
Open

Android build failed - Namespace not specified #186

kouz75 opened this issue Jul 18, 2024 · 2 comments

Comments

@kouz75
Copy link

kouz75 commented Jul 18, 2024

Hi,

I try to build my project for Android. (with gradle 8.x)
Build failed with the message :
A problem occurred configuring project ':callkeep'.

Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
Namespace not specified. Specify a namespace in the module's build file: /Users/xxx/.pub-cache/hosted/pub.dev/callkeep-0.3.3/android/build.gradle. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

 If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

any idea on how to fix it ?

@moazh
Copy link

moazh commented Jul 27, 2024

To address the issue, add the following code to the CallKeep project's Gradle configuration. If you have a custom CI/CD workflow that builds the application in the cloud, you may need to fork the project and create your own version until the changes are merged into the library.
android { if (project.android.hasProperty("namespace")) { namespace "com.github.cloudwebrtc.flutter_callkeep" } }

@vanyasem
Copy link

Here is a workaround. Modify android/build.gradle like that:

allprojects {
    repositories {
        ...
    }

    // Add this:
    // https://github.com/proyecto26/react-native-inappbrowser/issues/451#issuecomment-2275538714
    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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants