Since version 3.0.0, the Android Plugin for Gradle allows you to export a module's dependencies to other modules.
As I understand it, this means that 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?
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.
Related
I have created a module library (i.e aar ) and have uploaded the same in Jcenter. When I add the library as the Gradle dependency it fetches the other dependencies automatically whereas when I add the aar file as module dependency, I have to explicitly add the dependencies to the project Gradle file. Is there any way that I can avoid adding the dependencies that are already added to the library to the main project.
Awaiting reply.
Thanks.
I have an Android application. It contains two modules of app and pax-lib. app module depends on pax-lib module.
I have libs folder under pax-lib that contains some jar files. I have linked them in to gradle file of this module and use it across this module without any issue. This is how I have defined them:
dependencies {
...
// Local libs not in Maven Central
implementation files('libs/commons-io-1.3.2.jar')
implementation files('libs/commons-lang3-3.2.1.jar')
implementation files('libs/httpclientandroidlib-4.3.0.jar')
implementation files('libs/Kahuna_442.jar')
implementation files('libs/mapquest-android-sdk-1.0.5.jar')
...
}
This is how I defined this dependency in gradle file of app module.
dependencies {
implementation project(':pax-lib')
...
}
I am able to use all classes I have defined in pax-lib without any issue, however, I am not able to use .jar files that have defined in Gradle file of pax-lib module. My expectation is to be able to use them as I was in Gradle version below 3.0.
I must be able to copy/paste these jar files under app module but I want to make sure I am not doing something wrong first.
Use api rather than implementation
I have created a android library and created aar file which has gradle dependencies of Glide library and when I use this aar as a library in my main project.But when i use the part of glide code in the library, it gives an error as no class found.When add the gradle dependency of glide library in the main project it works fine.Will this cause multidex issues?
The aar file doesn't contain the nested (or transitive) dependencies and doesn't have a pom file which describes the dependencies used by the library.
It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.
You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.
I created the my Android Archive Library aar file that have it's own dependency in it's Gradle.
I use this library in many projects of mine, but why I should add aar dependency to my application dependencies? as we know benefit of using aar vs jar is aar can have and hold it's own resources and dependencies.
The aar file doesn't contain the nested (or transitive) dependencies and doesn't have a pom file which describes the dependencies used by the library.
It means that, if you are importing a aar file using a flatDir repo you have to specify the dependencies also in your project.
You should use a maven repository (you have to publish the library in a private or public maven repo), you will not have the same issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.
I've created an Android library project found here: https://github.com/dbotha/Android-Photo-Picker
The photo-picker library project itself has several dependencies of it's own:
// library build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
}
I've made this library project available on Maven Central so that it can be easily added to applications as a dependency:
// application build.gradle
dependencies {
compile 'ly.kite:photo-picker:1.1.2#aar'
}
The problem though is that when I come to add it as a dependency to a new Android application project it crashes because it can't find the Picasso dependency from the library project:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.squareup.picasso.Picasso" on path
It's only when I explicitly add this dependency to the applications build.gradle do things work.
My libraries POM file dependencies looks correct: https://repo1.maven.org/maven2/ly/kite/photo-picker/1.1.2/photo-picker-1.1.2.pom
So I'm curious if applications that include my photo-picker library as a dependency always need to explicitly add all the photo-picker library dependencies too?
You are using the #aar notation.
It means that you want to download only the aar artifact, and no dependencies.
You can check this part of documentation:
Check the 1.4.1.2. Artifact only notation section:
An artifact only notation creates a module dependency which downloads only the artifact file with the specified extension. Existing module descriptors are ignored.
Using the #aar notation if you want to download the dependencies, you should add transitive=true.
I'd expect that omitting #aar it should work.