9

I am writing a library project in Android Studio. My gradle file includes gson volley play-services etc...
When embedding my library in a project I get:

com.android.dex.DexException: Multiple dex files define Lcom/google/gson/JsonSerializer;

Could someone explain how gradle works when creating library projects?
What should I explain the developer who integrates my SDK, how does exclude module work, and why it is not working in the app that is including my aar?

4
  • check your lib project has play services/not Aug 10, 2016 at 13:40
  • It looks like you have different versions of the gson library in the dependecies of your app.
    – Thommy
    Aug 10, 2016 at 13:47
  • I am the one compiling the library project and it need goog play services.
    – Noam Segev
    Aug 11, 2016 at 11:42
  • My problem is when i take my compiled aar and put it in a project with gson in the gradle file i get that message. I was under the impression that gradle should solve conflicts
    – Noam Segev
    Aug 11, 2016 at 11:44

4 Answers 4

7
+25

There two cases that need to consider on

  • First, consider to remove Gson if you have declared it in app/build.gradle

    dependencies { compile 'com.google.code.gson:gson:2.4' }

  • Second, if you have not declare it in your app/build.gradle You may need to investigate which libraries that has duplicate declare Gson dependency. Then you can exclude the Gson from that library. You may want to check this Excluding transitive dependencies

Here, I will provide an example of exclude appcompat-v7 from a library

Run this command to see the dependencies chart tree

./gradlew app:dependencies

It will display the dependencies tree like the sample below

|    \--- com.mikepenz:materialdrawer:4.6.3
|         +--- com.android.support:appcompat-v7:23.1.1 (*)
|         +--- com.android.support:recyclerview-v7:23.1.1 (*)
|         +--- com.mikepenz:materialize:0.5.1
|         |    \--- com.android.support:appcompat-v7:23.1.1 (*)
|         +--- com.mikepenz:iconics-core:2.5.3
|         |    \--- com.android.support:appcompat-v7:23.1.1 (*)
|         \--- com.android.support:support-annotations:23.1.1

After you found library declared duplicate dependency. You can start exclude it.

dependencies {
    compile("com.mikepenz:materialdrawer:4.6.3") {
        exclude module: 'appcompat-v7'
    }
}
4
  • Can you elaborate on "exclude module" ?, I don't have the app project. This is what I need to tell the app developer who integrates my sdk to put in the build gradle if they want to use another version of gson ? @thann
    – Noam Segev
    Aug 22, 2016 at 6:50
  • In your case, you have a problem with Gson. So, I think you should inform them to exclude it. compile ('your_sdk_dependency'){ exclude group: 'com.google.code.gson', module: 'gson' } Aug 22, 2016 at 7:04
  • Or you might try this if your sdk project is not available in maven or jcenter. compile(project(':you_sdk')) { exclude group: 'com.google.code.gson', module: 'gson' } Aug 22, 2016 at 7:07
  • You solved me 1 week of debugging and searching! Thanks!
    – E. Atzeni
    Sep 14, 2018 at 17:17
1

If remove GSON is not an option for you, try enablin multidex support into your build.gradle file:

android {
   ...
   ...
         defaultConfig {
             ...
             ...
             // Enabling multidex support.
             multiDexEnabled true
         }
}
0

Gradle can't magically solve this problem - there can be different versions of the same library in your SDK and in user's application - those can't be merged or somehow differentiated.

I also developed SDK which used several popular third party libraries. I just declared that I used them and that user doesn't have to provide jars for those dependencies in his own application if he also needs them. I think that it's fine as long as you provide last versions of those libraries in your SDK.

  1. The only good way to avoid collisions that I can think of is to change class names in imported libraries - but that's really dull and tiring. Also you can use something like gradle shadow plugin to relocate packages but that seems risky to me. It looks like this:

shadowJar { relocate 'com.google.gson', 'shadow.com.google.gson' }

  1. Also your developer can unjar your library, remove gson and re-jar it back. But if he uses another version of gson, your library can break.

  2. Oh, yeah, you can also distribute your SDK as source, not as JAR - then users will be able to add it inside their application and use any libraries they want. But usually you don't want to put your SDK's code in public (even through library jars are very easy to reverse...).

2
  • Firts of all thanks, I don't understand, if i use gson in my library project I can't tell the host app not to use gson it is crazy. This is strange bhaviour I am looking for a gradle solution like exclude which did not work for me.
    – Noam Segev
    Aug 16, 2016 at 14:11
  • You can tell host app developer to use your built in version of gson. He won't be able to include his own gson library but he will be able to use gson version, included in your library. As for magic exclude solution - if you manage to exclude gson from your library (yeah, it is possible) - then it will stop working unless user adds completely same gson version to his app.
    – Jehy
    Aug 16, 2016 at 14:20
0

The --multi-dex option to dx is incompatible with pre-dexing library projects. So if your app uses library projects, you need to disable pre-dexing before you can use --multi-dex.

OR

Update your IDE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.