react-native-background-geolocation: Build fails with error `Error while generating the main dex list`

Your Environment

  • Platform: Android
  • React Native version (react-native -v): 0.56.0

Expected Behavior

App should compile

Actual Behavior

Gradle fails with error:

Execution failed for task ':app:transformClassesWithMultidexlistForDebug'.
> com.android.build.api.transform.TransformException: Error while generating the main dex list.

Steps to Reproduce

Run the app with react-native run-android

Context

build.gradle

ext {
    buildToolsVersion = "26.0.3"
    minSdkVersion = 16
    compileSdkVersion = 26
    targetSdkVersion = 26
    supportLibVersion = "26.1.0"
    googlePlayServicesVersion = "11.6.2"
}

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

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        google()
        maven { url "$rootDir/../node_modules/react-native/android" }
        maven { url "$rootDir/../node_modules/react-native-background-geolocation/android/libs" }
        maven { url "$rootDir/../node_modules/react-native-background-fetch/android/libs" }
        maven { url "https://raw.githubusercontent.com/layerhq/releases-android/master/releases/" }
        maven { url "https://jitpack.io" }
        maven { url "https://raw.githubusercontent.com/layerhq/releases-android/master/releases/" }
        maven { url "https://raw.githubusercontent.com/layerhq/Atlas-Android/master/releases/" }
    }
}

app/build.gradle

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
        bundleInStaging: true,
        devDisabledInStaging: true,
        inputExcludes: ["ios/**", "__tests__/**", "bundle_out/**"]
]

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

task switchToDebug(type: Copy) {
    description = 'Switches to DEBUG google-services.json'
    from "src/debug"
    include "google-services.json"
    into "."
}

task switchToRelease(type: Copy) {
    description = 'Switches to RELEASE google-services.json'
    from "src/release"
    include "google-services.json"
    into "."
}

afterEvaluate {
    processDebugGoogleServices.dependsOn switchToDebug
    processReleaseGoogleServices.dependsOn switchToRelease
    processStagingGoogleServices.dependsOn switchToDebug
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "------"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode -----
        versionName -----
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        multiDexEnabled true
    }
    signingConfigs {
        release {
            if (project.hasProperty(----)) {
                storeFile file(----)
                storePassword ----
                keyAlias ----
                keyPassword ----
            }
        }
    }
    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
        }

        staging {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
            matchingFallbacks = ['debug', '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 {
    compile project(':react-native-background-geolocation')
    compile project(':react-native-background-fetch')    
    compile project(':bugsnag-react-native')
    compile(project(':react-native-maps')) {
        exclude group: 'com.google.android.gms'
    }
    compile(project(':react-native-fcm')) {
        exclude group: "com.google.firebase"
    }
    compile(project(':react-native-device-info')) {
        exclude group: "com.google.android.gms"
    }
    compile 'com.rollbar:rollbar-android:0.2.1'
    compile project(':react-native-sensitive-info')
    compile project(':react-native-i18n')
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.facebook.react:react-native:+'
    compile 'org.slf4j:slf4j-nop:1.7.25'
    compile 'com.layer.atlas:layer-atlas:0.4.20'
    compile 'io.intercom.android:intercom-sdk-base:4.+'
    compile 'io.intercom.android:intercom-sdk-fcm:4.+'
    compile 'com.mixpanel.android:mixpanel-android:5.+'
    compile('com.google.android.gms:play-services-base:11.6.2') {
        force = true;
    }
    compile('com.google.android.gms:play-services-maps:11.6.2') {
        force = true;
    }
    compile('com.google.android.gms:play-services-gcm:11.6.2') {
        force = true;
    }
    compile('com.google.firebase:firebase-core:11.6.2') {
        force = true;
    }
    compile('com.google.firebase:firebase-messaging:11.6.2') {
        force = true;
    }
    compile('com.android.support:support-v4:26.1.0') {
        force = true;
    }

    compile('com.android.support:multidex:1.0.3')

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

// 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'

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

Post your 2 updated gradle files. For app/build.gradle, I’m only interested in the dependencies block.

Any idea on how this can be fixed?

What do you think we’re doing here?

You have many issues in your app/build.gradle.

You’re aware of the purpose of the gradle config params in your Android/build.gradle? So that you don’t hardcore important dependence versions, such as these:

compile('com.google.android.gms:play-services-base:11.6.2') {
        force = true;
    }
    compile('com.google.android.gms:play-services-maps:11.6.2') {
        force = true;
    }
    compile('com.google.android.gms:play-services-gcm:11.6.2') {
        force = true;
    }
    compile('com.google.firebase:firebase-core:11.6.2') {
        force = true;
    }
    compile('com.google.firebase:firebase-messaging:11.6.2') {
        force = true;
    }
    compile('com.android.support:support-v4:26.1.0') {
        force = true;
    }

Are you aware that many popular modules are also aware of those same gradle config params, so you no longer have to do all that exclude / force monkey business?

Your build.gradle is a mess.

@christocracy Managed to get the build done by excluding the org.slf4 from the other dependency. Seems to be working now so far. Will close the issue. Thanks