Android library project com.android.dex.DexException: multiple dex files define Lcom / google / gson / JsonSerializer

I am writing a library project in Android Studio. My gradle file includes gson volley game services, etc.

When I embed my library in a project, I get:

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

Can someone explain how gradle works when creating library projects?
What should I explain to the developer who integrates my SDK, how to exclude the module from working and why it does not work in the application that includes my aar?

+8
android android-gradle dependencies aar
source share
4 answers

Here are two cases to consider

  • First, consider removing Gson if you declared it in app / build.gradle

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

  • Secondly, if you did not declare it in your app/build.gradle , you may need to investigate which libraries duplicate to declare a Gson dependency. Then you can exclude Gson from this library. You can check this Transitive Dependency Exception

Here I will give an example of an exception of appcompat-v7 from the library

Run this command to see the dependency diagram tree

 ./gradlew app:dependencies 

It will display a dependency tree, as an example 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 

Once you find that the library has declared a duplicate dependency. You can start to exclude it.

 dependencies { compile("com.mikepenz:materialdrawer:4.6.3") { exclude module: 'appcompat-v7' } } 
+4
source share

Gradle cannot magically solve this problem - in your SDK and in the user application there may be different versions of the same library - they cannot be combined or somehow differentiated.

I also developed an SDK that used several popular third-party libraries. I just announced that I used them, and this user should not provide banks for these dependencies in their own application, if they also need it. I think this is fine as long as you provide the latest versions of these libraries in your SDK.

  • The only good way to avoid collisions that I can think of is to change the class names in the imported libraries, but it is really boring and tedious. You can also use something like the Gradle shadow plugin to move packages, but that seems risky to me. It looks like this:

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

  1. Alternatively, your developer can untie your library, remove gson, and re-commit it. But if it uses a different version of gson, your library may break.

  2. Oh, yes, you can also distribute your SDK as a source, not as a JAR, so users can add it to their application and use any libraries they want. But usually you don’t want to publish your SDK code (even through library banks it is very easy to cancel ...).

0
source share

The -multi-dex option for dx is not compatible with pre-decoding library projects. Therefore, if your application uses library projects, you need to disable preprocessing before you can use --multi-dex.

OR

Update your IDE

0
source share

If GSON removal is not an option for you, try enablin multidex support in the build.gradle file:

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

All Articles