isaac.udy
isaac.udy

Reputation: 188

Dependencies in Android Studio (coming from Eclipse) Multiple Dex Files

I am migrating all of my Android projects into Android Studio, as Eclipse has been a bit of a pain to work with.

I have several Android library projects, which provide functionality to my app projects.

Imagine it like this: MyCompanyUtils

MyClientUtils uses MyCompanyUtils

MyClientApp1 uses MyClientUtils

MyClientApp2 uses MyClientUtils

"MyCompanyUtils" contains the Google GSON library, as all my projects need this library. One of my apps uses a third party library, which includes the Google GSON library within it's own jar.

This was fine in Eclipse, as all I had to do was uncheck "Export Android Private Libraries", and I would avoid all 'Multiple Dex Files Define' errors. I cannot see such an option in Android Studio.

I have looked around at all the other questions that are asking about Multiple Dex errors in Android Studio, but they all seem to be from people who have control over ALL the libraries in their project. I don't. MyCompanyUtils needs GSON, and I need to use this library in an application project.

How can I prevent these libraries clashing?

Upvotes: 1

Views: 89

Answers (2)

isaac.udy
isaac.udy

Reputation: 188

OK, so what I wanted to do can be achieved using the "Provided" dependency tag, instead of the "Compile" tag.

If I use the provided tag in "MyCompanyUtils", specifically for the GSON dependency, it tells MyCompanyUtils that one of the projects that uses MyCompanyUtils will include the GSON dependency itself.

Imagine it like this:

MyCompanyUtils uses (provided) GSON

MyClientUtils uses (compile) MyCompanyUtils

MyClientApp1 uses (compile) MyClientUtils, (compile) GSON

MyClientApp2 uses (compile) MyClientUtils, (compile) Skobbler

As the Skobbler library includes GSON, this means that MyClientApp2 doesn't need to manually compile it. Because MyCompanyUtils is expecting one of the projects that implements it to provide the GSON library, this works fine.

I now also need to include and compile the GSON dependency for MyClientApp1, which I did not need to do before. This is again because MyCompanyUtils is expecting that projects implementing it will provide a compiled version of the library.

Upvotes: 1

Andrea Thacker
Andrea Thacker

Reputation: 3470

If you have multiple library projects used by one parent project you will need to organize them in such a way that there are no circular dependencies.

So in your case you would want to structure it like this:

MyCompanyUtils -> GSON

MyClientUtils -> MyCompanyUtils

MyClientApp1 -> MyClientUtils

MyClientApp2 -> MyClientUtils

With this structure MyClientApp1 will only ever include the GSON library once when it is inherited from MyClientUtils.

Upvotes: 0

Related Questions