Prohibit direct dependency to submodule in a multi-module android library

41 views Asked by At

I have a multi-module android library that consist of multiple android and kotlin only modules.

I can publish each module as an artifact and when a host-app declares a dependency to my root module, gradle fetches all the rest and sets up everything. This works good, but I want to make sure that the host-app does not declare a direct dependency on one of my submodules.

How can I do that?

To illustrate the issue further, imagine the following example.

└─ corp.acme:sdk:v1.0.0
   ├─ implementation corp.acme:sdk-moduleA:v1.0.0
   ├─ implementation corp.acme:sdk-moduleB:v1.0.0
   │  └─ implementation corp.acme:sdk-moduleA:v1.0.0
   └─ implementation corp.acme:sdk-moduleC:v1.0.0
      ├─ implementation corp.acme:sdk-moduleD:v1.0.0
      └─ implementation corp.acme:sdk-moduleE:v1.0.0

Once the host-app declares

dependencies {
  implementation corp.acme:sdk:v1.0.0
}

Gradle will pull all of the dependencies of the sdk to the app and only the public interface within corp.acme:sdk:v1.0.0 will be accessible to the host app, but none of the parts in moduleA-E as they are declared as runtime implementation dependencies.

However, if I follow this approach the host-app can directly depend on one of the modules as I am forced to publish them in a repository if I follow this approach. For example

dependencies {
  implementation corp.acme:sdk:v1.0.0
  implementation corp.acme:sdk-moduleA:v1.0.0
}

Is there a way to avoid this situation? I don't see a way to package a fat-aar as the plugins I have found are not supported anymore (for a long time now) and additionally it seems to be discouraged following the Android documentation.

Finally, I can use a single module library and work with the language visibility modifiers, but I was wondering if there is a way to have the best of both worlds?

0

There are 0 answers