17

I created a library project in Android Studio (currently 0.5.2) by choosing File > New Project... > "Mark this project as a library".

enter image description here

I have two other non-library projects that I would like to add a dependency to this library project.

-My Library
-Project 1 (depends on My Library)
-Project 2 (depends on My Library)

My goal is to keep each project independent and avoid duplicating modules/code. How can this be done without copying the library module into the other projects?

Update: Android Studio 0.6.0 allows you to Import a module, though, this simply copies the module source into the Project.

Ryan R
  • 8,342
  • 15
  • 84
  • 111
  • Have you tried creating multiple flavors for the two projects? With that you can specify flavor-specific code as well as shared resources between them – MrEngineer13 Jun 11 '14 at 16:30
  • @MrEngineer13 Unfortunately, in this case both projects are mutually exclusive and the only commonality they share is the library project. – Ryan R Jun 11 '14 at 20:43

3 Answers3

42

You can also refer to a library outside of your project folder using the project().projectDir property. If your external library is relative to your project like so

- MyLibrary
   - library
- MyProject
    - app

in MyProject/settings.gradle

include ':library'
project(':library').projectDir = new File(settingsDir, '../MyLibrary/library')

in MyProject/app/build.gradle

dependencies {
   compile project(':library')
}
harmanjd
  • 1,874
  • 19
  • 21
3

This is very similar to this question:

Sharing an Android library between multiple Android apps using Gradle

Instead of pushing to maven central you can push to your local maven repository (mavenLocal() in build.gradle)

Community
  • 1
  • 1
user1568967
  • 1,816
  • 2
  • 16
  • 18
  • 1
    I think this is the correct way of handling dependencies. If the library is not exclusively being used and developed for one project, you should make an AAR / JAR and upload it to maven / [private nexus repo](http://www.sonatype.org/nexus/) or mavenlocal on your dev machine. Doing it like this will Allow you you can always build your app even if code changes in the library cause that would be in library version 0.2 and not the version 0.1 you are using in the project. This also speeds up build time cause the library doesn't need to be compiled every time you build your app during development. – Aegis Jun 12 '14 at 06:29
  • I absolutely agree. What I've done with it libraries is use the relative path in my answer to work with a library when I'm working on a new feature, but then switch to referring to a released version in a maven repository when the library work is done. – harmanjd Nov 16 '19 at 13:59
1

Another route (if you don't want to deploy the library somewhere) is to use your VCS and check out the library within your project. Git has submodules for that, Mercurial has subrepos and SVN has external to name a few examples. Then add it to your Gradle build using a project dependency.

botteaap
  • 5,790
  • 3
  • 29
  • 35