Diamond Dependency Problem

I have a diamond dependency problem.

I have two libraries that need to be assembled into Android Archives (AARs), let’s call them library A & B. Library A and B depend on library C. These libraries are modules inside one Android project. I would like to keep them inside a single project, but still assemble the libraries separately. The A & B libraries (AARs) should be able to be used independently or together, by consuming applications. The libraries should also be able to be used as modules by a top level phone module inside the Android project. What is the best way to achieve this?

I have considered creating separate jar files for library C, possibly changing the namespace or name mangling the classes somehow, but have not yet implemented anything.

I am a little confused why you would need to change the packages or class names.

Unless I am missing something, you just need to structure you code with subprojects:


I am not familiar with Android projects, but basically in a Java/Kotlin project, your structure would be like this, with each subproject producing its own JAR, and each subproject having its own src dir:


rootProject.name = 'some-project'
include 'android-app', 'lib-c', 'lib-a', 'lib-b',


dependencies {
    implementation project(':lib-a')
    implementation project(':lib-b')


dependencies {
    implementation project(':lib-c')


dependencies {
    implementation project(':lib-c')

Thanks for response!

I can run directly to the phone module, and things work fine, but when I assemble library A or B, classes from library C are not included. I think this is a limitation that Gradle does not support creating FAT AARs.

There are plugins to accomplish creating FAT AARs, however, and when I try to embed library C, this is when the diamond dependency problem occurs.

What solutions are out there for having a shared module between two libraries that will be assembled into AARs and consumed by other applications?

Can you explain exactly the problem that is happening as a result of this diamond dependency issue?

I think I am missing something here, because if libs A, B, and C all have unique classes, then there should not be a problem when making a fat archive. Probably my Android ignorance is also playing a factor here.

Are libs A and B also fat or only the main AAR?

The error I get is that Library C’s BuildConfig is already defined. I am using a plugin that allows dependencies to be embedded inside the AAR. Trying to embed library C in both Library A and B is what causes this diamond dependency problem. This is a compile time issue.

I am trying to make AARs that include their dependencies (FAT), out of Library A and B. I do not need to assemble and AAR out of the main (phone) module.

OK. Then I would try to follow my original suggestion. Make everything subprojects. Each subproject will have its own JAR, except of course the main module that will be an AAR. Then just just project dependencies as demonstrated.

Thanks, but I already have it set up this way. It doesn’t work.

What if you create “core” subprojects that do not contain any Android information. Just basic classes with functionality. Then you could have like “lib-A-core”, “lib-A-android”, then you would include “lib-A-core”, “lib-B-core”, “lib-C-core” as dependencies into your fat JAR?

Or else maybe just filter out the classes causing the conflict during the fat JAR build?

I think the core module could work if I wrote a gradle task to copy the files from the library module into the core module. Do you know how I would get started writing a gradle task to copy the files, from the Lib C module, into the core module?

Also, any idea of how to write a gradle task to filter out conflicting classes?

The idea with the core module is that you would include it as a dependency where needed. Not manually copy the files somewhere.