Create a library with dependencies in android - android

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

Related

Avoid using repetitive dependency while adding aar file as Modular Dependencies

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.

Include local maven dependencies when exporting android library

I know from some other answers that including dependencies into an aar library is discouraged as the one using the aar should be able to include them by himself.
But I have a bunch of local maven dependencies that I would like to include with the aar. I could of course just include the .jar's. But there is a quite a few dependencies and if I update them I need to get all separate jars and update them as well in my aar. And using compile 'maven repo...' is quite handy.
So my question is, can I some how force my local maven repos to be included in the .aar?

why i should add dependencies that in aar file to my project?

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.

Multiple AAR files

I am using Android Studio 1.2
I create a private library I want to use that one in another application.
To use it I create an AAR files, but this AAR don't work. I have in my library a dependency to an AAR file.
The AAR files do not the dependencies?
If I use the jar and I includ ans create all the dependencies the project works fine.
NOTE :
I know how to importe the AAR file. The problem is to use an AAR in the AAR..
Thanks.
If I'm understanding your question correctly, there are 3 projects involved:
Library Project 2 --> Library Project 1 --> Application Project
You are editing "Library Project 1" and have added to it's app/build.grade a dependency on the Library Project 2's aar. Something like this: compile 'com.arasthel:gnavdrawer-library:1.1.5'
I am not sure where you are running into an issue, but I'll attempt an answer anyway. If I'm completely off-base, can you please elaborate on how the AAR dependency is not working? Any error messages?, a class/resource not found, etc.
I think it's unlikely you are unable to use a class from Library Project 2 inside Library Project 1, because I just tried this myself and it seems to be working just fine. It's worth noting that the Library Project 1 aar file will NOT include classes or resources from Library Project 2. Library Project 2 will be noted as a dependency in Library Project 1's pom if published using gradle's maven plugin to publish Library Project 1.
My guess is that you are having a problem in the Application Project? Perhaps the class from Library Project 2 is not found in the Application Project?
If that is correct, then there are two possible solutions:
Enable transitive dependencies on the aar dependency in the Application project's app/build.gradle: Instead of compile 'com.example:myLibrary:versionX', make it compile('com.example:myLibrary:versionX'){transitive=true}. I just verified this causes gradle to read Library Project 1's pom and automatically add dependencies found there into the Application Project.
If you would like to use transitive dependencies, your Library Project will need to be generating a pom and publishing it along with the aar. See https://stackoverflow.com/a/30085677/431296 for some additional information on how I have this working.
Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.
Add following code to you project build.gradle file, and you should put you AAR file to the libs folder.
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
And finally add compile info to your dependencies:
dependencies {
compile(name:'AARFileName', ext:'aar')
}

Packaging Android AAR that has dependencies

I want to package my library as an aar.
The library has several dependencies (universal image loader, ORMLite, guava...)
It also has a dependency for another library that I wrote (call it 'library B').
I have 2 questions:
Will everyone who will use my library need to add dependencies according to the library's dependencies (universal image loader, ORMLite, guava...)
Do I need to create a separate aar for 'library B', and have users of my lib have a separate dependency for it?
Will everyone who will use my library need to add dependencies according to the library's dependencies (universal image loader, ORMLite, guava...)
Not if you are distributing your AAR as an artifact in a repository with appropriate metadata (e.g., Maven-style POM file). The metadata will point to your dependencies, and build systems (e.g., Gradle) will pull in the dependencies.
Do I need to create a separate aar for 'library B'
Yes, otherwise nobody will have access to it, unless you eliminate it and fold its code into your first library.
and have users of my lib have a separate dependency for it?
See above for setting up dependencies.
You don not need do that,and you only do the next:
if you aar libray have thirty dependencies:
you should make others use your aar library in the way:
for example:
compile('com.android:com.android.download:1.3')
or
compile('com.android:com.android.download:1.3#aar'){
transitive = true
}
if your aar libray have not thirty dependencies:
compile('com.android:com.android.download:1.3#aar')
In a word:#aar have conflict with the default vault of transitive

Categories

Resources