How to include sources of one module inside another without copying the jar with Gradle?

293 Views Asked by At

I would like to split an Android library project into modules. The intended project structure is:

├─ androidLibraryModule
│
├─ kotlinLibraryModuleA
│
└─ kotlinLibraryModuleB

Where androidLibraryModule depends on kotlinLibraryModuleA and kotlinLibraryModuleB:

dependencies {
    implementation project(":kotlinLibraryModuleA")
    implementation project(":kotlinLibraryModuleB")
}

The only thing I'll publish is the .aar which is the result of building androidLibraryModule and I would like it to include the code from kotlinLibraryModuleA and kotlinLibraryModuleA. The only way I've found so far to achieve this goal is by copying the .jar files of kotlinLibraryModuleA and kotlinLibraryModuleB into the libs folder of androidLibraryModule this way:

task copyJar(type: Copy) {
    from jar
    into "${project(":androidLibraryModule").projectDir.absolutePath}/libs"
}

jar.finalizedBy(copyJar)

This solution works but feels a bit like a hack. Is this the only solution or is there a better way to achieve this result?

EDIT: if androidLibraryModule was an Android application module, this would just work without the need for a copy task.

1

There are 1 best solutions below

1
On

The thing is, the aar by default only contains code and resources from its module. When you create an app, the library module will pass on it's dependencies to the app, so all classes are included in the final apk.

If you want to publish just a library as an aar, you have 2 options:

  • Include the dependencies as jars inside - this is called a fat jar/aar (a quick search found multiple plugins like, but I have no experience with any of them, so you will need to see for yourself)
  • If you publish the library to maven, you can specify it's dependencies in pom.xml, there is eg. this answer: Android library dependencies missing from POM with Gradle