firebasereact-nativefirebase-cloud-messagingandroid-push-notification

Gradle Issues with Firebase implementation (buildScript syntax)


I have a React Native (0.68.6) application that I want to implement Firebase push notification. I'm using mac and I wanted to implement & test on android first.

I created Firebase account and created new Project. Also added google-service.json to the [project_folder] > android > app

I cannot build app when I try to add Firebase SDK.

On Firebase it says; if you still use buildscript syntax follow next steps;

  1. add following to the dependencies (buildscript > dependencies) in [project_folder] > android > build.gradle
classpath 'PLUGIN_MAVEN_COORDINATES:PLUGIN_VERSION'
  1. add plugin to the app level gradle file;
plugins {
    id 'com.android.application'

    // Add the ID of the plugin
    id 'FIREBASE_PLUGIN_ID'
    ...
}

Values;

PLUGIN_MAVEN_COORDINATES : com.google.gms:google-services
PLUGIN_VERSION : 4.4.0
FIREBASE_PLUGIN_ID: com.google.gms.google-services

The changes I made;

[project_folder] > android > build.gradle

buildscript {
...
dependencies {
        ...
        classpath("com.google.gms:google-services:4.4.0")
    }
...
}

[project_folder] > android > app > build.gradle

plugins {
    id "com.android.application"
    id "com.google.gms.google-services"
}

now I cannot build my app with the configurations and it's gave errors following;

FAILURE: Build failed with an exception.

java.lang.NoClassDefFoundError: com/android/build/api/extension/AndroidComponentsExtension

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.

BUILD FAILED in 6s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

java.lang.NoClassDefFoundError: com/android/build/api/extension/AndroidComponentsExtension

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.

BUILD FAILED in 6s

at makeError ([project_folder]/node_modules/execa/index.js:174:9)
at [project_folder]/node_modules/execa/index.js:278:16
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async runOnAllDevices ([project_folder]/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5)
at async Command.handleAction ([project_folder]/node_modules/@react-native-community/cli/build/index.js:192:9)

info Run CLI with --verbose flag for more details. error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I tried different syntax rules when adding dependency and plugin but no luck.

Can anyone help me? Thanks


Solution

  • I solved the issue by using different syntax and version. If you encounter the same issue, you can follow the steps below;

    1. add lines below to the [project_folder] > android > app > build.gradle
        buildscript {
          dependencies {
            ...
            classpath 'com.google.gms:google-services:4.4.0'
          }
        }
    
    1. add lines below to the [project_folder] > android > build.gradle
        plugins {
          id 'com.android.application'
          id 'com.google.gms.google-services'
        }
    
        dependencies {
        ...
          implementation 'com.google.firebase:firebase-messaging:21.1.0'
        ...
        }
    
    
    1. clean gradlew

    these steps solved the isuue.