React-native-firebase: android gradle build failed

Created on 27 Apr 2018  路  3Comments  路  Source: invertase/react-native-firebase

"react-native": "0.53.0",
"react-native-firebase": "^4.0.5",

In document https://rnfirebase.io/docs/v4.0.x/installation/android say that need Android build tools to version 3.1.0. Then I also change all my build.gradle compileSdkVersion to27 and buildToolsVersion to '27.0.3', also change all compile to implementation, but I the error below

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:preDebugBuild'.

    Android dependency 'com.android.support:support-v4' has different version for the compile (26.1.0) and runtime (27.0.2) classpath. You should manually set the same version via DependencyResolution

  • 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 23s

react-native-firebase build.gradle

buildscript {
  repositories {
    jcenter()
    google()
    maven {
      url 'https://maven.fabric.io/public'
    }
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.1.0'
    classpath 'io.fabric.tools:gradle:1.25.1'
  }
}

apply plugin: 'com.android.library'

def DEFAULT_COMPILE_SDK_VERSION = 27
def DEFAULT_BUILD_TOOLS_VERSION = "27.0.3"
def DEFAULT_TARGET_SDK_VERSION = 26
def DEFAULT_FIREBASE_VERSION = "12.0.0"
def DEFAULT_SUPPORT_LIB_VERSION = "27.0.2"

android {
  compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
  buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
  defaultConfig {
    minSdkVersion 16
    targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
  }
  buildTypes {
    release {
      minifyEnabled false
    }
  }
  productFlavors {
  }
}


allprojects {
  repositories {
    jcenter()
    mavenLocal()
    google()
  }
}

rootProject.gradle.buildFinished { buildResult ->
  if (buildResult.getFailure() != null) {
    try {
      String message = buildResult.getFailure().properties.get("reportableCauses").toString()
      if (message.contains("com.android.dex.DexException: Multiple dex files define Lcom/google/") ||
              message.contains("java.util.zip.ZipException: duplicate entry: com/google/android/gms/")) {
        logger.log(LogLevel.ERROR, "")
        logger.log(LogLevel.ERROR, " -----------------------------------------------------------")
        logger.log(LogLevel.ERROR, "|                    REACT NATIVE FIREBASE                  |")
        logger.log(LogLevel.ERROR, " ----------------------------------------------------------- ")
        logger.log(LogLevel.ERROR, "|                                                           |")
        logger.log(LogLevel.ERROR, "|  This is a common build error when using libraries that   |")
        logger.log(LogLevel.ERROR, "|  require google play services.                            |")
        logger.log(LogLevel.ERROR, "|                                                           |")
        logger.log(LogLevel.ERROR, "|  This error occurs because different versions of google   |")
        logger.log(LogLevel.ERROR, "|  play services or google play services modules are being  |")
        logger.log(LogLevel.ERROR, "|  required by different libraries.                         |")
        logger.log(LogLevel.ERROR, "|                                                           |")
        logger.log(LogLevel.ERROR, "|  A temporary fix would be to set:                         |")
        logger.log(LogLevel.ERROR, "|                                                           |")
        logger.log(LogLevel.ERROR, "|             android { multiDexEnabled true }              |")
        logger.log(LogLevel.ERROR, "|                                                           |")
        logger.log(LogLevel.ERROR, "|  inside your build gradle, however it is recommended for  |")
        logger.log(LogLevel.ERROR, "|  your prod build that you de-duplicate these to minimize  |")
        logger.log(LogLevel.ERROR, "|  bundle size.                                             |")
        logger.log(LogLevel.ERROR, "|                                                           |")
        logger.log(LogLevel.ERROR, "|  See http://invertase.link/dupe-dex for how to do this.   |")
        logger.log(LogLevel.ERROR, "|                                                           |")
        logger.log(LogLevel.ERROR, " ----------------------------------------------------------- ")
      }
    } catch (Exception exception) {}
  }
}

def firebaseVersion = rootProject.hasProperty('googlePlayServicesVersion') ? rootProject.googlePlayServicesVersion : DEFAULT_FIREBASE_VERSION
def supportVersion = rootProject.hasProperty('supportLibVersion') ? rootProject.supportLibVersion : DEFAULT_SUPPORT_LIB_VERSION

dependencies {
  // compile fileTree(include: ['*.jar'], dir: 'libs')
  api "com.facebook.react:react-native:+"  // From node_modules
  api "com.android.support:support-v4:$supportVersion"
  compileOnly 'me.leolin:ShortcutBadger:1.1.21@aar'
  compileOnly "com.google.android.gms:play-services-base:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-core:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-config:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-auth:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-database:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-storage:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-messaging:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-crash:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-perf:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-ads:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-firestore:$firebaseVersion"
  compileOnly "com.google.firebase:firebase-invites:$firebaseVersion"
  compileOnly('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
    transitive = true
  }
}

app build.gradle

apply plugin: "com.android.application"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation
 *   entryFile: "index.android.js",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

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 23
    //buildToolsVersion "23.0.1"
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        applicationId "com.company.myapp"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.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 {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }
    dexOptions {
        javaMaxHeapSize "2g"
        jumboMode = true
    }
    lintOptions {
        abortOnError false
    }
    // 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
            }
        }
    }
}

buildscript {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }

}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    compile(project(':react-native-firebase')) {
        transitive = false
    }
    implementation project(':react-native-shine-button')
    implementation project(':react-native-share')
    implementation project(':react-native-orientation')
    implementation project(':react-native-linear-gradient')
    implementation project(':react-native-fbsdk')
    implementation project(':react-native-image-picker')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-smart-splash-screen')
    implementation project(':react-native-video-exoplayer')
    implementation(project(':react-native-firebase')) {
        transitive = false
    }
    implementation "com.google.android.gms:play-services-base:12.0.0"
    implementation "com.google.firebase:firebase-core:12.0.0"
    implementation "com.google.firebase:firebase-messaging:12.0.0"
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //compile "com.android.support:appcompat-v7:23.0.1"
    implementation "com.android.support:appcompat-v7:27.1.1"
    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'

Most helpful comment

@judemanutd still the same

image

All 3 comments

cd android then gradlew clean and try running react-native run-android

@judemanutd still the same

image

@JayricMok it could be that another of your dependencies is setting an older version of this support library. As mentioned in the error message, you should look to overwrite this using dependency resolution strategy available in gradle.

There is more information here: https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

romreed picture romreed  路  3Comments

csumrell picture csumrell  路  3Comments

Draccan picture Draccan  路  3Comments

jonaseck2 picture jonaseck2  路  3Comments

n-scope picture n-scope  路  3Comments