invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.63k stars 2.2k forks source link

Firebase Performance Plugin duplicates com.reactnativecommunity.geolocation.BuildConfig in Android #5171

Closed JanicePrendas closed 3 years ago

JanicePrendas commented 3 years ago

My react-native project (no Expo) was working just fine. However, once I installed @react-native-firebase/perf, my Android build failed. I followed all the instructions and iOS works fine.

This is the error that I get:

Type com.reactnativecommunity.geolocation.BuildConfig is defined multiple times: myProject/android/app/build/intermediates/transforms/FirebasePerformancePlugin/dev/debug/156.jar:com/reactnativecommunity/geolocation/BuildConfig.class, myProject/android/app/build/intermediates/transforms/FirebasePerformancePlugin/dev/debug/163.jar:com/reactnativecommunity/geolocation/BuildConfig.class

Here's my code:

package.json:

{
  "name": ...,
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "test:watch": "jest --watch",
    "precommit": "lint-staged",
    "compile": "tsc --noEmit -p . --pretty",
    "format": "npm-run-all format:*",
    "format:js": "prettier --write {.,**}/*.js",
    "format:json": "prettier --write {.,**}/*.json",
    "format:md": "prettier --write {.,**}/*.md",
    "format:ts": "prettier --write {.,**}/*.{ts,tsx}",
    "hack:types-react-navigation": "rimraf node_modules/@types/react-navigation/node_modules/@types",
    "hack:types-react-native": "rimraf node_modules/@types/react-native/node_modules/@types",
    "hack:types-react-test-renderer": "rimraf node_modules/@types/react-test-renderer/node_modules/@types",
    "lint": "eslint index.js app/ --ext .js,.ts,.tsx",
    "patch": "patch-package",
    "postinstall": "node ./bin/postInstall",
    "prepare": "npm-run-all patch hack:*",
    "storybook": "start-storybook -p 9001 -c ./storybook",
    "adb": "adb reverse tcp:9090 tcp:9090 && adb reverse tcp:3000 tcp:3000 && adb reverse tcp:9001 tcp:9001 && adb reverse tcp:8081 tcp:8081"
  },
  "dependencies": {
    "@mdi/font": "^5.0.45",
    "@ptomasroos/react-native-multi-slider": "^2.2.2",
    "@react-native-community/async-storage": "^1.5.1",
    "@react-native-community/eslint-config": "^2.0.0",
    "@react-native-community/geolocation": "^2.0.2",
    "@react-native-community/masked-view": "0.1.5",
    "@react-native-community/push-notification-ios": "^1.2.2",
    "@react-native-community/slider": "^3.0.3",
    "@react-native-firebase/analytics": "^11.2.0",
    "@react-native-firebase/app": "^11.2.0",
    "@react-native-firebase/crashlytics": "^11.2.0",
    "@react-native-firebase/dynamic-links": "^11.2.0",
    "@react-native-firebase/in-app-messaging": "^11.2.0",
    "@react-native-firebase/messaging": "^11.2.0",
    "@react-native-firebase/perf": "^11.2.0",
    "@react-navigation/native": "^5.1.3",
    "@types/enzyme": "^3.10.8",
    "@types/lodash": "^4.14.168",
    "@types/react-native-snap-carousel": "^3.8.2",
    "@types/semver": "^7.3.4",
    "@typescript-eslint/eslint-plugin-tslint": "^4.20.0",
    "apisauce": "1.0.3",
    "axios": "^0.19.2",
    "formik": "^2.1.4",
    "fractional": "^1.0.0",
    "i18n-js": "^3.0.11",
    "ignite-cli": "^3.5.1",
    "lodash": "^4.17.15",
    "lodash.throttle": "4.1.1",
    "mobx": "^4.13.0",
    "mobx-react-lite": "^1.4.1",
    "mobx-state-tree": "^3.15.0",
    "moment": "^2.24.0",
    "ramda": "0.26.1",
    "react": "16.9.0",
    "react-native": "0.61.2",
    "react-native-auth0": "2.3.0",
    "react-native-datepicker": "^1.7.2",
    "react-native-device-info": "^8.0.7",
    "react-native-document-picker": "^4.0.0",
    "react-native-dotenv": "^0.2.0",
    "react-native-dropdown-autocomplete": "^1.0.18",
    "react-native-elements": "^3.3.2",
    "react-native-fbsdk": "^3.0.0",
    "react-native-flash-message": "^0.1.15",
    "react-native-formik": "^1.7.8",
    "react-native-gesture-handler": "^1.5.0",
    "react-native-keyboard-accessory": "^0.1.11",
    "react-native-keyboard-aware-scroll-view": "^0.9.1",
    "react-native-keychain": "3.1.3",
    "react-native-localize": "^1.0.0",
    "react-native-material-dropdown": "^0.11.1",
    "react-native-material-textfield": "^0.16.1",
    "react-native-material-ui": "^1.30.1",
    "react-native-multiple-select": "^0.5.5",
    "react-native-paper": "^4.6.0",
    "react-native-pie-chart": "^1.0.13",
    "react-native-push-notification": "^3.5.2",
    "react-native-radio-buttons-group": "^1.0.7",
    "react-native-reanimated": "^1.13.2",
    "react-native-safe-area-context": "^3.2.0",
    "react-native-safe-area-view": "1.0.0",
    "react-native-screens": "^2.0.0-alpha.6",
    "react-native-splash-screen": "^3.2.0",
    "react-native-vector-icons": "^7.1.0",
    "react-native-webview": "^10.9.2",
    "react-navigation": "4.0.10",
    "react-navigation-drawer": "^2.6.0",
    "react-navigation-stack": "2.0.9",
    "react-navigation-tabs": "^2.10.1",
    "react-redux": "^7.2.0",
    "reactotron-mst": "^3.1.1",
    "redux": "^4.0.5",
    "semver": "^6.3.0",
    "smartlook-react-native-wrapper": "^0.39.0",
    "validate.js": "0.13.1",
    "yup": "^0.28.3"
  },
  "devDependencies": {
    "@babel/core": "^7.13.14",
    "@babel/plugin-proposal-decorators": "^7.0.0",
    "@babel/plugin-proposal-optional-catch-binding": "^7.0.0",
    "@babel/runtime": "^7.13.10",
    "@storybook/addon-storyshots": "5.2.8",
    "@storybook/react-native": "5.2.8",
    "@storybook/react-native-server": "5.2.8",
    "@types/i18n-js": "^3.8.0",
    "@types/jest": "24.0.18",
    "@types/ramda": "0.26.18",
    "@types/react": "16.8.24",
    "@types/react-native": "0.60.19",
    "@types/react-native-dotenv": "^0.2.0",
    "@types/react-test-renderer": "16.8.3",
    "@typescript-eslint/eslint-plugin": "^4.20.0",
    "@typescript-eslint/parser": "^4.20.0",
    "babel-jest": "^24.1.0",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2",
    "eslint": "^7.23.0",
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-airbnb-typescript": "^12.3.1",
    "eslint-config-prettier": "^6.15.0",
    "eslint-config-standard": "^13.0.1",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-node": "^9.1.0",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-react": "^7.22.0",
    "eslint-plugin-react-hooks": "^4.2.0",
    "eslint-plugin-react-native": "^3.6.0",
    "eslint-plugin-standard": "^4.0.0",
    "husky": "^4.2.3",
    "ignite-bowser": "^4.13.0",
    "jest": "^24.9.0",
    "jetifier": "^1.6.1",
    "lint-staged": "^10.0.8",
    "metro-react-native-babel-preset": "^0.51.1",
    "npm-run-all": "4.1.5",
    "patch-package": "6.1.2",
    "postinstall-prepare": "1.0.1",
    "prettier": "^1.19.1",
    "react-devtools-core": "3.6.3",
    "react-powerplug": "1.0.0",
    "react-test-renderer": "^16.8.1",
    "reactotron-react-native": "^4.0.3",
    "redux-devtools": "^3.5.0",
    "rimraf": "2.6.3",
    "solidarity": "2.3.1",
    "typescript": "3.5.3"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "./app/*.{ts,tsx}": [
      "npx prettier --write",
      "eslint ./app/*.{ts,tsx} --quiet --fix-dry-run"
    ]
  },
  "eslintIgnore": [
    "!.*",
    "ios",
    "android",
    "node_modules/*"
  ],
  "jest": {
    "preset": "react-native",
    "setupFiles": [
      "<rootDir>/test/set-up.ts",
      "<rootDir>/node_modules/react-native/jest/setup.js",
      "<rootDir>/test/setup.ts",
      "./node_modules/react-native-gesture-handler/jestSetup.js"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/e2e"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-native|react-navigation|@react-navigation|@storybook|@react-native-community)"
    ]
  }
}

android/build.gradle:

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 29
        targetSdkVersion = 29
    }
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath('com.android.tools.build:gradle:3.6.4')
        classpath 'com.google.gms:google-services:4.3.5'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
        classpath 'com.google.firebase:perf-plugin:1.3.5'
    }
}

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        maven {
            url "https://sdk.smartlook.com/android/release"
        }
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

android/app/build.gradle:

apply plugin: "com.android.application"
apply plugin: "com.google.gms.google-services"
apply plugin: "com.google.firebase.crashlytics"
apply plugin: 'com.google.firebase.firebase-perf'

import com.android.build.OutputFile project.ext.react = [
entryFile: "index.js",
enableHermes: false,  // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"
enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false
def jscFlavor = 'org.webkit:android-jsc:+'
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId ...
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 55
        versionName "1.14.2"
        multiDexEnabled true
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        release {
            ...
        }
        debug {
            ...
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }

    }
    flavorDimensions "env"
   productFlavors {
        dev {
            ...
        }
        prod {
            ...
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            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-fbsdk')
    implementation project(':@react-native-community_geolocation')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-device-info')
    implementation project(':react-native-splash-screen')

    implementation 'com.android.support:multidex:1.0.3'
    implementation project(':react-native-webview')
    implementation project(':react-native-auth0')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation platform('com.google.firebase:firebase-bom:26.4.0')
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation 'com.google.firebase:firebase-analytics:18.0.3'
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-inappmessaging-display'
    implementation 'com.google.firebase:firebase-messaging:21.1.0'
    implementation 'com.facebook.android:facebook-android-sdk:8.2.0'
    implementation 'com.google.firebase:firebase-perf'
    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

This is the error that I get:

Type com.reactnativecommunity.geolocation.BuildConfig is defined multiple times: myProject/android/app/build/intermediates/transforms/FirebasePerformancePlugin/dev/debug/156.jar:com/reactnativecommunity/geolocation/BuildConfig.class, myProject/android/app/build/intermediates/transforms/FirebasePerformancePlugin/dev/debug/163.jar:com/reactnativecommunity/geolocation/BuildConfig.class

I already have multiDexEnabled true, I already tried to clean, rebuild, invalidate caches and restart... But nothing has worked. I also tried to delete the duplicated files manually. However, those files are created again automatically when I rebuild the project.

What can I do?

mikehardy commented 3 years ago

Interesting...

Looks a bit like this: https://stackoverflow.com/a/64979232/9910298

Note that this is lower-level than react-native-firebase, it's firebase-android-sdk + gradle, I don't believe there is anything we can do in this module to affect this at all, though if there was a minimum reproducible example repo (https://stackoverflow.com/help/minimal-reproducible-example) - perhaps build from https://github.com/mikehardy/rnfbdemo/blob/master/make-demo.sh - that implicated this module we could take a look

mikehardy commented 3 years ago

I don't believe this will immediately turn up the root cause but I will note that react-native 0.61.2 was from October 2019 https://github.com/react-native-community/releases/commit/ecf8715ebecd2b98ab5b8e3055d1c34f09ac4778 - in general problems like this, that is low-level toolchain issues, are the sorts of things I expect to start seeing when the toolchain itself (react-native, it's underlying gradle plugin and gradle distribution etc) is out of date. That's really vague though, I am quick to admit - even you updated all of that there may still be something subtle going on, but - this is not reproduced in any projects I'm associated with at the moment

JanicePrendas commented 3 years ago

I already solved it! These are the steps that I followed: