Android : Android Gradle App and Libraries jar conflicts

on Monday, October 27, 2014


I want to migrate a big application from Ant to Gradle. Since the app has been (wrongly) created as several libraries, the first migration will keep the same Modules (it will refactored in a second step) and it will probably end up in this structure:




-- Project/
|-- App/
|-- libs/
|-- src/...
|-- build.gradle
|-- Module1/
|-- libs/
|-- lib1.jar
|-- lib2.jar
|-- src/...
|-- build.gradle
|-- Module2/
|-- libs/
|-- lib2.jar
|-- lib3.jar
|-- src/...
|-- build.gradle
build.gradle
settings.gradle


The App build.gradle is something like this:




...

apply plugin: 'com.android.application'

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

compile project(':Module1')
compile project(':Module2')
}

...


And then each Module build.gradle will be something like this:




...

apply plugin: 'com.android.library'

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

...


Every module compiles correctly, but the problem happens when building the APK, because lib2.jar is copied 2 times, resulting on an error:


Error:Gradle: Execution failed for task ':app:packageDebug'.



Duplicate files copied in APK META-INF/ASL2.0 and then the path to the 2 JARs.



How can I tell Gradle to do not copy multiple times the same JARs when building the APK from each module? At this moment, I cannot move dependencies to Maven central repo, even if I will in the future. Maybe adding all the libs in the parent App? It does not look very nice to me... And in this case, how can I specify that dependencies in the build.gradle Modules?


0 comments:

Post a Comment