7

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 Jars

Library Project Library Project

External Library External Libraries

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?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Aggressor
  • 13,323
  • 24
  • 103
  • 182

1 Answers1

4

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'])
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • So there is no difference to what they end up as once compiled, its purely how it gets added to the project? – Aggressor Nov 04 '15 at 21:21
  • @Aggressor There are some difference if you have nested dependencies. Gradle handles it with maven modules, while you have to manage yourself with aar and jar files. – Gabriele Mariotti Nov 04 '15 at 21:51
  • @GabrieleMariotti could you please tell me what happens when the aar lib has a dependency like retrofit, does that make inclusion of retrofit necessary for the module/project the aar file is being linked to ? i thought it is not major enough for me to raise an independent question for this doubt – Pararth Feb 16 '17 at 09:07
  • @user2450263 It is not clear your issue. Just upload your library in maven adding the dependency of retrofit. Gradle will handle it for you automatically. – Gabriele Mariotti Feb 16 '17 at 09:10
  • @GabrieleMariotti thanks for your response, my issue exactly, explained better: [found a duplicate](http://stackoverflow.com/questions/34465099/adding-gradle-dependency-to-library-aar-package) and it does not have a solution – Pararth Feb 16 '17 at 09:11