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.
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 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
In the Android Studio IDE you can add a .jar file. You can also add entire projects that are 'Library Projects'. And there are also the Android libraries (from Gradle) added as External Libraries.
.jar
Library Project
External Library
I get that .jars are just that. But then how come other libraries (Library Projects) need to get added that have entire build files of their own (like gradle, res, src etc). And further complicating my understanding, the Gradle downloaded ones are added as 'External Libraries' and those have .jar files and a res folder.
Could you explain why libraries can be added as .jar, entire projects, or as external libraries?
Could you explain why libraries can be added as .jar, entire projects, or as external libraries?
You have different possibilities, because you can have different cases. Often the library is built by other team and you can't decide how it is distributed.
You can have a own library or a fork locally. In this case you have the code and you can add this library as a module
In this case just add in the module/build.gradle:
apply plugin: 'com.android.library'
and add the dependency in the main project like:
dependencies{
compile project(':module')
}
You can use a maven dependency. In this case someone uploaded the library in a maven repository.
Currently it is the best solution in my opinion.
In this case just add a dependency in your project
dependencies{
compile 'group:name:version'
}
This dependency can be a aar file, but also a jar file.
Also you can publish in a public or private maven your own libraries.
You can add an aar file in your project
In this case define your flat repository:
repositories {
flatDir {
dirs 'libs'
}
}
and add the dependency:
dependencies {
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}
You can add a jar file in your project
In this case usually you can put all jars in the libs folder and add the dependency:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
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')
}