skip to Main Content

I try to build a release apk for my project, but I face an exception:
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':flutter_barcode_scanner'.
> 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. 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.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
Running Gradle task 'assembleRelease'...                         1,849ms
Gradle task assembleRelease failed with exit code 1

I searched for solution in GitHub and I got this solution :

For now it seems that package doesn't work with AGP and Gradle 8, I downgraded agp to 7.4.2 and gradle to 7.5

So I make solutions in build.gradle and gradle-wrapper.properties:

build.gradle:

buildscript {
    ext.kotlin_version = '1.9.25' 
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2' 
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.1'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

gradle-wrapper.properties:

#Tue Apr 20 01:37:31 IST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip

But after this solution, I faced the same exception, and I asked ChatGPT, and he told me to make changes in build.gradle in the package file:
build.gradle before edits:

group 'com.amolg.flutterbarcodescanner'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
    }
}

rootProject.allprojects {
    repositories {
        google()
        jcenter()
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 30

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 30
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    lintOptions {
        disable 'InvalidPackage'
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'com.google.android.gms:play-services-vision:20.1.3'
}

build.gradle after edits:

group 'com.amolg.flutterbarcodescanner'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
    }
}

rootProject.allprojects {
    repositories {
        google()
        jcenter()
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 34

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 34
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    lintOptions {
        disable 'InvalidPackage'
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'com.google.android.gms:play-services-vision:20.1.3'
}

2

Answers


  1. Just go to app/build.gradle inside the flutter_barcode_scanner package, and then add the following line to solve the issue:

       android { namespace "add flutter_barcode_scannerpackage namespace" ---------}
    

    To get the correct namespace, check the package name of flutter_barcode_scanner inside the AndroidManifest.xml file.


    Also, check this answer👇🏻 other solution

    You can resolve the namespace issue after updating to AGP 8.+ by adding the following script in the android/build.gradle file:

    subprojects {
       afterEvaluate { project ->
           if (project.hasProperty('android')) {
               project.android {
                   if (namespace == null) {
                       namespace project.group
                   }
               }
           }
       }
    }
    

    Check here

    Login or Signup to reply.
  2. To fix the issue with missing namespace declarations in dependency projects, add the following code to your android/build.gradle:

    subprojects {
        afterEvaluate { project ->
            if (project.hasProperty('android')) {
                project.android {
                    if (namespace == null) {
                        namespace project.group
                    }
                }
            }
        }
    }
    

    Note: Make sure that any afterEvaluate call comes before this code, like so:

    subprojects {
        project.evaluationDependsOn(":app")
    }
    

    Click here to check code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search