flutter firebase app not running on Android: Namespace not specified
Asked Answered
H

4

8

I keep getting an error when trying to run on an Android Simulator. I have firebasecore and Square in app payments in this app. This is the error:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':package_info_plus'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

     android {
         namespace 'com.example.namespace'
     }

     If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.

* 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 4s

This is what I have done so far:

This is my gradlewrapper.properties file

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip

This is my Android level build.gradle file:

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

    dependencies {
        classpath("com.android.tools.build:gradle:8.0.0")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:google-services:4.3.15"
    }
}

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
}

Here is my App level build.gradle file

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

//I added this here for signingConfig
   def keystoreProperties = new Properties()
   def keystorePropertiesFile = rootProject.file('key.properties')
   if (keystorePropertiesFile.exists()) {
       keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
   }

android {
    compileSdkVersion 33
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.juliapak.coffeesociety"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 28
        targetSdkVersion 33
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

   //added for signing app
      signingConfigs {
       release {
           keyAlias keystoreProperties['keyAlias']
           keyPassword keystoreProperties['keyPassword']
           storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
           storePassword keystoreProperties['storePassword']
       }
   }
   buildTypes {
       release {
           signingConfig signingConfigs.release
       }
   }
  namespace 'com.juliapak.coffeesociety'
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:32.0.0')
    implementation 'com.google.firebase:firebase-analytics'
    }

I have tried switching distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip to 7.6.1 but it did not work either. As you can see I've also assigned a namespace. What do you recommend I do?

Hittite answered 14/5, 2023 at 20:46 Comment(2)
As far as I know, we do not need namespace to be specified in the app module's build.gradle file for a Flutter project. We do not need to specify the gradle dependencies for Firebase BoM in any gradle file as well. You might want to double-check the flutter firebase dependencies in pubspec.yaml. Please refer to this link.Geostrophic
Have you run FlutterFire configure?Porbeagle
S
19

from experimentation, the problem is that 'namespace' needs to be specified in all modules, which includes ones from flutter packages..

coincidentally, i also had the same problem with package_info_plus.

with this SO thread, i managed to fix this specific error by adding this in the global build.grade:

subprojects{
    afterEvaluate {
        android {
            namespace 'add your namespace here'
        }
    }
}
Sine answered 30/5, 2023 at 12:43 Comment(5)
thanks, It worked overriding the namespace of the subprojects but now it is throwing an error saying to remove the "package" from the AndroidManifest; do you know if is there a way to do that? "Incorrect package="dev.fluttercommunity.plus.connectivity" found in source AndroidManifest.xml" thanks in advance —Thick
Not working for me... Gives Cannot run Project.afterEvaluate(Closure) when the project is already evaluated.Raquelraquela
@PabloPantaleon There is a package= property inside the Manifests' <manifest tag that you need to remove if you put the namespace declaration in app/build.gradle.Chaise
@PabloPantaleon could you solved it? I'm on the same situation right now, but i don't want to downgrade my gradle version and AGP. What will be the way to remove the package= inside AndroidManifest.xml on every package?Mikvah
I also faced Oleg's error. Abdur's solution below worked for meRival
E
17

There is a problem with the above most-voted answer: https://mcmap.net/q/1238449/-flutter-firebase-app-not-running-on-android-namespace-not-specified

Problem

Many packages are still using package= definition inside their AndroidManifest.xml so if you add namespace directly it will give error for all those packages to remove the package= property, which obviously is not feasible.

Solution

If you are using Gradle v8.x.x above then you have two options. Either:

  1. [RECOMMENDED] Downgrade your gradle to v7.x.x something. Not all packages support v8 at the moment so you can upgrade it later.
  2. If for any reason you don't want to downgrade. You can add the following subprojects script. NOTE: Using subprojects is a workaround and not a good practice. If not absolutely required I would suggest doing option 1.

Add this to your android/build.gradle file:

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
// The below script
subprojects {
   afterEvaluate { project ->
        if (project.hasProperty('android')) {
            project.android {
                if (namespace == null) {
                    namespace project.group
                }
            }
        }
    }
}
// till here
subprojects {
    project.evaluationDependsOn(':app')
}

This will add the namespace to only any packages that can support it.

Elevated answered 31/3 at 9:40 Comment(1)
Option 2 saved me. I took a whole night to find this life saver. Thanks a lot @Abdur Rafay Saleem.Nephrotomy
R
4

The problem can be the version of Gradle, not all package support version 8.x.x yet.

I suggest you stay on version 7.

Reisch answered 15/5, 2023 at 15:32 Comment(3)
I was thinking that too. what version should I use? What will this line be 'distributionUrl= services.gradle.org/distributions/gradle-8.1.1-bin.zip' and version do i replace it with in build.gradle ' classpath("com.android.tools.build:gradle:8.0.0")'Hittite
If you use android studio you can use the migration tool to downgrade Gradle version. It set all automaticallyReisch
You can find it in tool>AGP Migrate AssistantReisch
C
0

I have similar problem with you, the namespace already added in build.gradle (:app) but error still appear.

I resolve it by downgrade just the Android Gridle Plugin Version 8.1.2 to 7.4.2.

Before, my version is Android Gradle Plugin Version : 8.1.2 and Gradle Version : 8.4

Open your project in android studio:

  1. file > project structure
  2. menu side choose 'Project'
  3. on Android Gradle Plugin Version, click white small button on the right side or shortkey shift + enter
  4. will popup extract variable, change 'value' to 7.4.2
  5. ok > apply then, ok
Canister answered 24/10, 2023 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.