Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include another project using Gradle? [duplicate]

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.

like image 954
Ryan R Avatar asked Jun 10 '14 19:06

Ryan R


People also ask

What is buildSrc in Gradle?

buildSrc is a separate build whose purpose is to build any tasks, plugins, or other classes which are intended to be used in build scripts of the main build, but don't have to be shared across builds.

What is a transitive dependency Gradle?

Transitive dependencyA variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.


3 Answers

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')
}
like image 188
harmanjd Avatar answered Sep 21 '22 20:09

harmanjd


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)

like image 37
user1568967 Avatar answered Sep 21 '22 20:09

user1568967


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.

like image 24
botteaap Avatar answered Sep 22 '22 20:09

botteaap