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.
Related
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.
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 a library 'LibA' which has dependencies on many 3rd party libraries like RecyclerView, EventBus etc. When i tried to include it in another project as an aar, Library was included successfully but dependencies did not came in aar.
Q1 How can I include dependencies in LibA, so that when some other project includes this library, it should not worry about internal dependencies of my library.
Q2 How does gradle manages dependencies of libraries, does it downloads all depenedencies at once, or first checks which are already available in the main project?
Q3 When someone includes a library from jcenter, does that brings all the dependencies with it?
Any help will very much appreciated. :)
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.
Q1 How can I include dependencies in LibA, so that when some other project includes this library, it should not worry about internal dependencies of my library.
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.
Q2 How does gradle manages dependencies of libraries, does it downloads all depenedencies at once, or first checks which are already available in the main project?
Gradle handles the dependencies for you. It doesn't add the same dependency twice or more.
Q3 When someone includes a library from jcenter, does that brings all the dependencies with it?
As said before, in this case the dependency has a pom file which describes all the nested dependencies. Gradle downloads it automatically.
Q1 How can I include dependencies in LibA, so that when some other project includes this library, it should not worry about internal dependencies of my library.
You should use some Dependency manager. For example Maven Central which has .pom file which defines all additional dependencies which should be used
Q2 How does gradle manages dependencies of libraries, does it downloads all depenedencies at once, or first checks which are already available in the main project?
gradle downloads all necessary dependencies. It creates a graph for dependencies, try to solve conflicts and download them
Q3 When someone includes a library from jcenter, does that brings all the dependencies with it?
If additional dependencies were not defined in pom file they will not be downloaded, only library
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.