React-native-firebase: ./gradlew build fails

Created on 24 Apr 2018  路  15Comments  路  Source: invertase/react-native-firebase

Issue

I've posted about this issue over in react-native-firebase-starter as well since it looks like someone has the same problem there, but I believe the issue is something to do with react-native-firebase itself as I have the same problem when adding RNF to my existing project. There seem to be a few issues with migrating to the new version of gradle that are not currently well documented in the RNF setup documentation that might be very helpful until they are better resolved in react native core.

If using react-navigation, you currently have to add android.enableAapt2=false to your gradle.properties file in order to get it to compile. You might have to do this for other libraries as well. Getting the appcompat-v7 library versions to line up across all dependencies was a bit tricky as well. I had to add the following to my android/build.gradle file:

allprojects {
    repositories {
        ....
        configurations.all {
            resolutionStrategy {
                force "com.android.support:appcompat-v7:27.0.2"
                force "com.android.support:support-v4:27.0.2"
            }
        }
    }
}

I also set in my dependencies:

    implementation "com.android.support:appcompat-v7:27.0.2"

Some documentation notes about this library version and normalizing it across all project dependencies would be very helpful. Without it you get linter errors about "All com.android.support libraries must use the exact same version specification".

Now here's the big problem that's causing the build task to fail for me currently:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native-firebase:lint'.
> Lint found errors in the project; aborting build.

  Fix the issues identified by lint, or add the following to your build script to proceed with errors:
  ...
  android {
      lintOptions {
          abortOnError false
      }
  }
  ...

  The first 3 errors (out of 9) were:
  /Users/mose/Projects/app/node_modules/react-native-firebase/android/src/main/java/io/invertase/firebase/analytics/RNFirebaseAnalytics.java:34: Error: Missing permissions required by FirebaseAnalytics.getInstance: android.permission.ACCESS_NETWORK_STATE and android.permission.WAKE_LOCK [MissingPermission]
      FirebaseAnalytics.getInstance(getReactApplicationContext()).logEvent(name, Arguments.toBundle(params));
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /Users/mose/Projects/app/node_modules/react-native-firebase/android/src/main/java/io/invertase/firebase/analytics/RNFirebaseAnalytics.java:42: Error: Missing permissions required by FirebaseAnalytics.getInstance: android.permission.ACCESS_NETWORK_STATE and android.permission.WAKE_LOCK [MissingPermission]
      FirebaseAnalytics.getInstance(getReactApplicationContext()).setAnalyticsCollectionEnabled(enabled);
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  /Users/mose/Projects/app/node_modules/react-native-firebase/android/src/main/java/io/invertase/firebase/analytics/RNFirebaseAnalytics.java:58: Error: Missing permissions required by FirebaseAnalytics.getInstance: android.permission.ACCESS_NETWORK_STATE and android.permission.WAKE_LOCK [MissingPermission]
            FirebaseAnalytics.getInstance(getReactApplicationContext()).setCurrentScreen(activity, screenName, screenClassOverride);
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What's really confusing me is that my project is not using firebase analytics library at all. I'm not loading that module, so I don't know why the linter is trying to check permissions for it in the first place. I'm only using firebase core, messaging, and notifications.

I did try adding these permissions to my AndroidManifest.xml file, but the linter still fails with the same error:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />

What's also strange here is that while ./gradlew build fails to work, I am still able to build my app for debugging and run it fine without any issues using react-native run-android.

Environment

Android

  1. Application Target Platform:

Android

  1. Development Operating System:

Android Studio on macOS

  1. Build Tools:

27.0.3

  1. React Native version:

0.55.3

  1. RNFirebase Version:

4.0.4

  1. Firebase Module:

Core, Messaging, Notifications

Build Error Bug Android

Most helpful comment

Update.
The react native firebase starter also results in the same errors. If you follow the steps through setting adding your google-services.json, then you make the following changes:

add android.enableAapt2=false in gradle.properties
add the following to android/app/build.gradle

configurations.all {
    resolutionStrategy.force 'com.android.support:support-v4:27.0.2'
}

add implementation 'com.android.support:support-media-compat:27.0.2' to the dependencies in android/app/build.gradle

I also commented out all firebase related dependencies except for project(':react-native-firebase'), firebase-core, play-services-base

Even after adding

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

it still results in the errors during :react-native-firebase:lint saying that the permissions WAKE_LOCK and ACCESS_INTERNET_STATE are missing from the manifest.

All 15 comments

Can we see your apps build gradle please, thanks

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion 27

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        applicationId "my.app.id"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 5
        versionName "1.2.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        debug {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "false"]
        }
        release {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "true"]
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation(project(':react-native-firebase')) {
        transitive = false
    }
    implementation 'com.google.firebase:firebase-core:12.0.1'
    implementation 'com.google.android.gms:play-services-base:12.0.1'
    implementation 'com.google.firebase:firebase-messaging:12.0.1'
    implementation 'me.leolin:ShortcutBadger:1.1.21@aar'
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-fs')
    implementation project(':realm')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:27.0.2"
    implementation 'com.facebook.react:react-native:+'  // From node_modules
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

@hayatae Have you had any luck with this? Firebase Analytics is installed by default as part of firebase-core

I still have not been able to resolve this issue. The standard build command continues to fail on linting errors due to the analytics code thinking that the right permissions are not enabled for some reason.

I was also having issues last night getting ./gradlew assembleRelease command to succeed and produce a release APK. Fortunately I was able to resolve that by adding the following to my android/build.gradle file:

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 27
                buildToolsVersion '27.0.3'
            }
        }
    }
}

My whole top-level build file now looks like this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath 'com.google.gms:google-services:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
        google()
    }
}

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 27
                buildToolsVersion '27.0.3'
            }
        }
    }
}

Also to remove another warning, I had to add implementation "com.android.support:support-v4:27.0.2" to my app's build.gradle file. The whole file for that now looks like this:

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion 27

    defaultConfig {
        applicationId "my.app.id"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 5
        versionName "1.2.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        debug {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "false"]
        }
        release {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "true"]
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation(project(':react-native-firebase')) {
        transitive = false
    }
    implementation 'com.google.firebase:firebase-core:12.0.1'
    implementation 'com.google.android.gms:play-services-base:12.0.1'
    implementation 'com.google.firebase:firebase-messaging:12.0.1'
    implementation 'me.leolin:ShortcutBadger:1.1.21@aar'
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-fs')
    implementation project(':realm')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:27.0.2"
    implementation "com.android.support:support-v4:27.0.2"
    implementation 'com.facebook.react:react-native:+'  // From node_modules
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

+1 also facing this issue

any update for this issue ?

I have still yet to find a solution for this issue. I've at least been able to work around it in the mean time since with my current build scripts I am still able to do react-native run-android and ./gradlew assembleRelease.

Have you tried updating to v4.1.0 to see if the new android libraries prevent this error? https://github.com/invertase/react-native-firebase/releases/tag/v4.1.0

The fact that it's working with some commands and not others implies that this is definitely a mis-configuration somewhere along the line and lends itself to more of a gradle issue than a react-native-firebase one.

Same problem with v4.1.0. If it's a configuration issue, I'd like to see it found so that the RN-Firebase documentation can be improved so that others don't run into these problems as well. Unfortunately I have no idea where to look or what to change beyond what is currently outlined in the Android setup documentation on rnfirebase.io.

Without a way of reproducing this error, it鈥檚 very difficult for us to be able to help.

If you鈥檙e able to push up a repo with a small example where this doesn鈥檛 work then we would be able to take a look in more detail.

Closing this as there has been no response for over a week. If this is still an issue, please push a small repo for us to take a look at...

@chrisbianca Hey Chris, I too am experiencing this issue, I'll be pushing up a small repo tomorrow afternoon after work for you to take a look at, thanks for the help so far by the way.

Update.
The react native firebase starter also results in the same errors. If you follow the steps through setting adding your google-services.json, then you make the following changes:

add android.enableAapt2=false in gradle.properties
add the following to android/app/build.gradle

configurations.all {
    resolutionStrategy.force 'com.android.support:support-v4:27.0.2'
}

add implementation 'com.android.support:support-media-compat:27.0.2' to the dependencies in android/app/build.gradle

I also commented out all firebase related dependencies except for project(':react-native-firebase'), firebase-core, play-services-base

Even after adding

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

it still results in the errors during :react-native-firebase:lint saying that the permissions WAKE_LOCK and ACCESS_INTERNET_STATE are missing from the manifest.

Same issue here on 4.2.0. Any news?

Hey all,

Just published a fix for this in v4.3.6. Additionally if you face any linting errors in your own app about [GradleCompatible] (RN Issue) or [InvalidPackage] (GRPC issue) you can disable them like I've just done on this commit on the starter:

https://github.com/invertase/react-native-firebase-starter/commit/00ec0d6a5136be7b17766242883125555280ecec#diff-dc46f9f5dfe204e394fb41395ccd03cfR99


Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:

Was this page helpful?
0 / 5 - 0 ratings