166

I want to use master version of my lib from mavencentral.

Is it possible to declare git repository as dependency in android gradle?

Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197

6 Answers6

204

For me the best way is:

https://jitpack.io

Step 1. Add the JitPack repository to build.gradle at the end of repositories:

repositories {
    // ...
    maven { url "https://jitpack.io" }
}

Step 2. Add the dependency in the form

dependencies {
    implementation 'com.github.User:Repo:Tag'
}

It is possible to build the latest commit on the master branch, for example :

dependencies {
    implementation 'com.github.jitpack:gradle-simple:master-SNAPSHOT'
}
Prashasth Nair
  • 167
  • 2
  • 12
sunnyday
  • 2,671
  • 1
  • 13
  • 16
  • 8
    If you want the latest master then use the version `-SNAPSHOT` https://jitpack.io/docs/#snapshots – Andrejs Sep 21 '15 at 13:28
  • 10
    this is spam i guess. but i really want to say that https://jitpack.io is really really really cool.............. – Eric Jun 25 '16 at 04:58
  • 7
    Make sure you put this to the main section, not to the `buildscript` – Sergei Ledvanov Aug 07 '17 at 06:20
  • What is "TAG"?? – Makalele Dec 14 '17 at 14:07
  • 2
    @Makalele, "TAG" is a git tag (any tagged commit, for example, some release). [docs](https://git-scm.com/docs/git-tag) – sunnyday Dec 14 '17 at 14:14
  • 4
    Even thou Jitpack is great, it's not really an option for many people, as it's paid for private repositories. – Stan Jul 31 '18 at 12:04
  • One minor caveat here: I could only make my build work with the branch code by adding the `-SNAPSHOT` in the end. Otherwise it will give you an error not found. – hcabral Mar 07 '19 at 18:33
  • Please note that Jitpack is only free to use for Open Source projects – Mister Smith Jun 27 '19 at 08:55
  • Add the JitPack repository to build.gradle **at the end of repositories:** that part actually matters otherwise you may get weird build fails. – lasec0203 May 27 '20 at 04:38
  • 1
    Is there some requirement that the source git repository must meet?? I am completely out of luck with this, and I am working with a forked repo plus my own release. Also implementing a specific branch seems to be impossible. – Delark Jan 23 '22 at 20:08
  • This should be the accepted answer. – Ovi Trif Apr 20 '22 at 16:17
  • Does this also resolve transitive dependencies? – kutschkem Sep 09 '22 at 15:26
56

There is now a new feature in gradle that lets you add source dependencies from git.

You first need to define the repo in the settings.gradle file and map it to a module identifier:

sourceControl {
    gitRepository("https://github.com/gradle/native-samples-cpp-library.git") {
        producesModule("org.gradle.cpp-samples:utilities")
    }
}

You will need to use URI("https://github.com/gradle/native-samples-cpp-library.git") instead of "https://github.com/gradle/native-samples-cpp-library.git" if you're using Kotlin gradle.

And now in your build.gradle you can point to a specific tag (e.g.: 'v1.0'):

dependencies {
    ...
    
    implementation 'org.gradle.cpp-samples:utilities:v1.0'
}

Or to a specific branch:

dependencies {
    ...
    
    implementation('org.gradle.cpp-samples:utilities') {
        version {
            branch = 'release'
        }
    }
}

Caveats:

  • Gradle 4.10 or higher required
  • Does not support authentication yet

References:

Community
  • 1
  • 1
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • It will be great if this could work also with the IDE: I tried using sourceControl for one of my libraries in my android project (with Android Studio 3.4.1), the IDE is not able to do "gradle sync" and source files do not see the library... ..but if I build through the command line using "./gradlew build" everything work. – gabry Jul 03 '19 at 15:16
  • I'm not sure if it's just me but I faced a lot of issues with this, especially when the library has databinding. For one the gradle script throws an NPE and can't locate the sdk folder for the databinding compiler while auto building the git repo. https://android.googlesource.com/platform/tools/base/+/studio-master-dev/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/tasks/databinding/DataBindingCompilerArguments.kt#170 I ended up having to commit my local.properties however i ran into other issues as well – humblerookie Apr 02 '20 at 03:21
  • 4
    I am not having much luck with this. Nearly 2 years later, and it's barely documented, has very few options, no great examples, and seems to work only for "perfect" repos. – Grant Birchmeier Apr 30 '21 at 19:39
  • 2
    what if your repo is private? – chitgoks Jan 23 '22 at 23:25
  • @chitgoks You can get private repositories to work using [this advice](https://discuss.gradle.org/t/limitations-of-gradle-source-dependencies/28956/20) (ignore the latter half though). However, you [cannot use new key formats](https://github.com/gradle/gradle/issues/19028) (<- unless this is closed) and bypassing this by [using an RSA key is impossible, because GitHub disallows it](https://www.eclipse.org/forums/index.php?t=msg&th=1110330). Once the issue is fixed it should work without the requirement to add any key file or modify your SSH config. – Martin Braun Apr 11 '22 at 01:04
  • ok. Ill see if this works. Currently I decided to use my private local repository. – chitgoks Apr 11 '22 at 06:11
  • Another caveat seems to be that gradle versions of the local and remote repo need to be compatible, bc Gradle will try to configure the remote project using the local tooling – crizzis May 26 '23 at 11:49
44

Or you can register a repository as a submodule like this:

git submodule add my_sub_project_git_url my-sub-project

Then include the project in your settings.gradle file, which for example should be like:

include ':my-app', ':my-sub-project'

Note that above both my-app and my-sub-project are folder names, and said folders need to contain a build.gradle file.
Child-folders are supported with ":" (instead of /), like:

include ':my-app'
include ':my-folder:my-sub-project'

Finally, compile the project as a dependency in your application's build.gradle file, like:

buildscript {
  // ... Not here ...
}
    
// ...
    
dependencies {
  api project(':my-sub-project')

  // Or for older Gradle versions:
  // ```
  // compile project(':my-sub-project')
  // ```
}

But if you are foldering:

api project(':my-folder:my-sub-project')

Also, when cloning your project, you will only have to add the option --recursive to make git automatically clone the root repository, and all its submodules.

git clone --recursive my_sub_project_git_url

I hope it helps.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
trupin
  • 812
  • 7
  • 14
7

I don't think Gradle supports to add a git repository as a dependency. My workaround is to:

  • declare that the main project depends on another project in the filesystem
  • provide a way to automatically clone the git repo in the folder declared as a dependency

I assume that you want the library repo outside the folder of the main project repo, so each project will be independent git repos, and you can make commits to the library and main project git repositories independently.

Assuming you want to have the folder of the library project in the same folder that the folder of the main project,

You could:

In the top-level settings.gradle, declare the library repository as a project, given it's location in the filesystem

// Reference:  https://looksok.wordpress.com/2014/07/12/compile-gradle-project-with-another-project-as-a-dependency/

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

Use the gradle-git plugin to clone the library from the git repository

    import org.ajoberstar.gradle.git.tasks.*

    buildscript {
       repositories { mavenCentral() }
       dependencies { classpath 'org.ajoberstar:gradle-git:0.2.3' }
    }

    task cloneLibraryGitRepo(type: GitClone) {
            def destination = file("../library")
            uri = "https://github.com/blabla/library.git"
            destinationPath = destination
            bare = false
            enabled = !destination.exists() //to clone only once
        }

In the dependencies of your project, say that the code of your project depends on the folder of the git project

dependencies {
    compile project(':lib_project')
}
hannojg
  • 983
  • 8
  • 28
GaRRaPeTa
  • 5,459
  • 4
  • 37
  • 61
  • 1
    Gradle now supports source dependencies with certain restrictions. See my answer [here](https://stackoverflow.com/a/56787382/813951) – Mister Smith Jun 27 '19 at 08:57
5

The closest thing I have found is https://github.com/bat-cha/gradle-plugin-git-dependencies but I can't get it to work with the android plugin, keeps trying to pull from maven even after the git repos are loaded.

sgarman
  • 6,152
  • 5
  • 40
  • 44
1

@Mister Smith's answer almost worked for me, the only difference was that instead of passing the repository URI as a String it needed to be a URI, i.e.:

sourceControl {
    gitRepository(new URI("https://github.com/gradle/native-samples-cpp-library.git")) {
        producesModule("org.gradle.cpp-samples:utilities")
    }
}

I'm using Gradle 6.8.

asherbret
  • 5,439
  • 4
  • 38
  • 58