简体   繁体   中英

Intellij: Avoiding 'Multiple Dex Files Defined'

Problem:

Up until now I've been using Gradle to handle all of my dependencies, and it seems to take care of any duplicate dependencies between other Gradle modules. However, this does not seem to be the case when a duplicate dependency exists within a jar.

Question:

Considering that I have control over what goes into the jar, What is the best practices for handling these dependency conflicts using Gradle:

  1. Do not include any external dependencies in the jar, include them in the project itself using build.gradle

  2. Include all external dependcies in the jar, remove duplicates as they occur by removing them from the project build.gradle . ( NOTE: this does not seem scalable, eg if there are duplicates between jars themselves.)

  3. Something better (that hopefully handles this automatically)

EDIT: build.gradle file:

apply plugin: 'com.android.application'

android {
    signingConfigs {
        release { ... }
        debug { ... }
    }
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
    defaultConfig { ... }
    buildTypes {
        release { ... }
        debug { ... }
    } 
    sourceSets {
        instrumentTest.setRoot('tests')
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.0.0'
    compile project(':jarModule')
}

When importing external jars that have a dependency that you also have in your local app, you can do two things:

Convert your jar dependency to a Gradle dependency and exclude the dependency

For example:

testCompile('org.robospock:robospock:0.5.0')  {
     exclude module: "groovy-all" // <-- excludes groovy from robo spock
}

or

Remove the local dependency in your app and rely on the one in the .jar

For example, in your case with Gson :

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.0.0'
    compile project(':jarModule')
    // compile 'com.google.code.gson:gson:2.3.1' // <-- removed
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM