androidgradleandroid-librarymulti-module

Android: Multi module library doesn't work with implementation project without publishing all modules


I'm trying to publish a multi-module library but I only want to publish only want one module as follows:

-module main
-module data
-module domain

enter image description here

Sample app should implement only main module (as this is the gate of the library)

BUT main module depends on data and domain modules

At first I was using implementation (project(":data/domain"))

implementation(project(":domain"))
implementation(project(":data"))

but when I tried to publish the main module (maven local for now) and use the main artifact in the sample app I got the error message that data and domain dependencies could not be found.

I had to publish data and domain for it to be found despite declaring them as implementation project

What am I doing wrong? I can't believe we need to publish all of the modules of a library for the main module to be usable by an app...


Solution

  • So Maven, either local or global, depends on declaring dependencies in POM format XML files. Even if you publish with Gradle or any other tool, it will always build pom.xml files for the modules you want to publish because that is how Maven works.

    That means that the Maven itself is limited by the POM protocol. Knowing that we can safely assume all the dependencies declared in the pom.xml generated via publishing the library would comply with the POM dependencies rules.

    If you read through them, you'll notice that there is no way to include any dependency that is not published into the library except for the system dependency scope, which can only be a precompiled JAR file. So, any modules your library depends on that are not compiled should be properly published into Maven. Or be precompiled JAR files. There is no gray area.

    That means you should either publish all the modules of your multimodular project or compile them into jars and import them accordingly. There is no way around that.