Search code examples
androidfirebasereact-nativegradlebuild.gradle

A problem occurred evaluating project ':react-native-firebase_app'. It is too late to set namespace It has already been read to configure this project


React native trying to build, clean, or sync gradle results in this error.

Error: `> Configure project :react-native-firebase_app :react-native-firebase_app package.json found at /Users/{name}/Documents/GitHub/{project}/node_modules/@react-native-firebase/app/package.json

FAILURE: Build failed with an exception.

  • Where: Build file '/Users/{name}/Documents/GitHub/{project}/node_modules/@react-native-firebase/app/android/build.gradle' line: 66

  • What went wrong: A problem occurred evaluating project ':react-native-firebase_app'.

It is too late to set namespace It has already been read to configure this project. Consider either moving this call to be during evaluation, or using the variant API.`

@react-native-firebase/app/android/build.gradle':

import io.invertase.gradle.common.PackageJson

buildscript {
    // The Android Gradle plugin is only required when opening the android folder stand-alone.
    // This avoids unnecessary downloads and potential conflicts when the library is included as a
    // module dependency in an application project.
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
        }

        dependencies {
            classpath("com.android.tools.build:gradle:8.1.2")
        }
    }
}

plugins {
    id "io.invertase.gradle.build" version "1.5"
}

def packageJson = PackageJson.getForProject(project)
def firebaseBomVersion = packageJson['sdkVersions']['android']['firebase']
def playServicesAuthVersion = packageJson['sdkVersions']['android']['playServicesAuth']
def jsonMinSdk = packageJson['sdkVersions']['android']['minSdk']
def jsonTargetSdk = packageJson['sdkVersions']['android']['targetSdk']
def jsonCompileSdk = packageJson['sdkVersions']['android']['compileSdk']

project.ext {
    set('react-native', [
            versions: [
                    android: [
                            minSdk: jsonMinSdk,
                            targetSdk: jsonTargetSdk,
                            compileSdk: jsonCompileSdk,
                    ],

                    firebase: [
                            bom: firebaseBomVersion,
                    ],

                    play: [
                            "play-services-auth": playServicesAuthVersion,
                    ],
            ],
    ])
}

apply from: file('./firebase-json.gradle')

// If data collection isn't specifically disabled, default is enabled
String dataCollectionDefaultEnabled = 'true'

if (rootProject.ext && rootProject.ext.firebaseJson) {
    if (rootProject.ext.firebaseJson.isFlagEnabled('app_data_collection_default_enabled', true) == false) {
        dataCollectionDefaultEnabled = 'false'
    }
}

android {
    def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()

    // Set namespace here
    if (agpVersion >= 7) {
        namespace 'io.invertase.firebase'
    }

    defaultConfig {
        multiDexEnabled true
        manifestPlaceholders = [
                firebaseJsonDataCollectionDefaultEnabled: dataCollectionDefaultEnabled
        ]
    }

    buildFeatures {
        // AGP 8 no longer builds config by default
        buildConfig true
    }

    lintOptions {
        disable 'GradleCompatible'
        abortOnError false
    }

    if (agpVersion < 8) {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_11
            targetCompatibility JavaVersion.VERSION_11
        }
    }

    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'src/reactnative/java']
        }
    }
}

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation platform("com.google.firebase:firebase-bom:${ReactNative.ext.getVersion("firebase", "bom")}")
    implementation "com.google.firebase:firebase-common"
    implementation "com.google.android.gms:play-services-auth:${ReactNative.ext.getVersion("play", "play-services-auth")}"
}

ReactNative.shared.applyPackageVersion()
ReactNative.shared.applyDefaultExcludes()
ReactNative.module.applyAndroidVersions()
ReactNative.module.applyReactNativeDependency("api")

build.gradle:

buildscript {
    ext {
        buildToolsVersion = "34.0.0"
        minSdkVersion = 21
        compileSdkVersion = 34
        targetSdkVersion = 34
        ndkVersion = "25.1.8937393"
        kotlinVersion = "1.8.0" // Make sure this matches the version you wish to use
    }
    ext.kotlin_version = "1.8.0"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:8.4.1"
        classpath "com.google.gms:google-services:4.4.0"
        classpath "com.facebook.react:react-native-gradle-plugin:+"
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'  
apply plugin: 'com.facebook.react.rootproject'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    namespace {namespace} 

    defaultConfig {
        applicationId {same as namespace}
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }


    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"
    implementation project(':react-native-firebase_app')
}

gradle.wrapper.properties:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

I deleted node modules and re installed. I upgraded the gradle version. I tried upgrading android studio. I tried modifying @react-native-firebase/app/android/build.gradle'. None of it worked except when I modified the @react-native-firebase/app/android/build.gradle' file and commented out a lot then I was able to get it to sync but not build. This is what I commented out for that result:

  if (agpVersion >= 7) {
    namespace 'io.invertase.firebase'
  }

  lintOptions {
    disable 'GradleCompatible'
    abortOnError false
  }

dependencies {
  implementation platform("com.google.firebase:firebase-bom:${ReactNative.ext.getVersion("firebase", "bom")}")
  implementation "com.google.firebase:firebase-common"
  implementation "com.google.android.gms:play-services-auth:${ReactNative.ext.getVersion("play", "play-services-auth")}"
}

ReactNative.shared.applyPackageVersion()
ReactNative.shared.applyDefaultExcludes()
ReactNative.module.applyAndroidVersions()
ReactNative.module.applyReactNativeDependency("api")

Solution

  • The main problem came from non compatible versions. Here are the versions I landed on to fix the problem:

    Kotlin: 1.8.0 Gradle: gradle-8.2-all.zip Java: 17

    Also in my android/buil.gradle setting the plugin dependency to not be dynamic so classpath "com.facebook.react:react-native-gradle-plugin:8.2"

    Then lastly in my android/app/build.gradle I set this as my dependencies:

    dependencies {
        implementation 'com.facebook.react:react-native:+'
    
        // Exclude Flipper integration if not used
        implementation('com.facebook.react:react-native:+') {
            exclude group: 'com.facebook.flipper'
        }
    
        implementation 'com.facebook.soloader:soloader:0.10.1+'
    }