Search code examples
androidandroid-studiogradletransitive-dependencygradle-dependencies

How to export dependencies in android library


Since version 3.0.0, the Android Gradle Plugin allows you to export a module's dependencies to other modules.

So in my android library module I should be able to declare a dependency using api <dependency declaration> and access THAT dependency as an exported transitive dependency in my main app project, where I've declared my library module as a dependency.

I'm also using static file dependencies.

As an example:

I have a class NeededEverywhere, which is defined in its own gradle module everywhere-module. This module is in the same project as my library module.

//library module's build.gradle:
dependencies {
    api project(':everywhere-module')
}

In my app's build.gradle (which is in a different Android Studio project), I declare my dependency on the library, but not the everywhere-module. This should mean that everywhere-module is an exported transitive dependency.

//app project's build.gradle
dependencies {
    implementation files("path/to/my/library/file.aar")
}

However, I can't access the class NeededEverywhere in my app.

What am I doing wrong?


Solution

  • You are directly referencing an AAR.

    Dependency information is not in an AAR, any more than dependency information is in a JAR. Dependency information is held in the metadata of an artifact repository, such as the POM file of a Maven-style repository.

    So, the project with the library module needs to publish its AAR to an artifact repository. That could be one local to your machine (e.g., mavenLocal()). Then, projects that need to depend upon the library module do so from the repository, not via file(). Then, Gradle can get the transitive dependency information from the repository and make use of it.