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

68,078

Solution 1

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'
}

Solution 2

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 should look like this

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

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

dependencies {
  compile project(':my-sub-project')
}

Then, 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.

Solution 3

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:

Solution 4

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')
}

Solution 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.

Share:
68,078
Alexey Zakharov
Author by

Alexey Zakharov

Lead Android Engineer at Lyft.

Updated on September 16, 2021

Comments

  • Alexey Zakharov
    Alexey Zakharov over 2 years

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

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

  • Andrejs
    Andrejs over 8 years
    If you want the latest master then use the version -SNAPSHOT jitpack.io/docs/#snapshots
  • Eric
    Eric almost 8 years
    this is spam i guess. but i really want to say that jitpack.io is really really really cool..............
  • Sergei Ledvanov
    Sergei Ledvanov almost 7 years
    Make sure you put this to the main section, not to the buildscript
  • Makalele
    Makalele over 6 years
    What is "TAG"??
  • sunnyday
    sunnyday over 6 years
    @Makalele, "TAG" is a git tag (any tagged commit, for example, some release). docs
  • Stan
    Stan almost 6 years
    Even thou Jitpack is great, it's not really an option for many people, as it's paid for private repositories.
  • L.Butz
    L.Butz over 5 years
    I don't normally write this in the comments, but in this case I just have to say "Holy shit, dude! That's so cool, thanks man!" :-)
  • hcabral
    hcabral about 5 years
    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.
  • Mister Smith
    Mister Smith almost 5 years
    Please note that Jitpack is only free to use for Open Source projects
  • Mister Smith
    Mister Smith almost 5 years
    Gradle now supports source dependencies with certain restrictions. See my answer here
  • gabry
    gabry almost 5 years
    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.
  • AO_
    AO_ almost 5 years
    I agree with @MisterSmith, Git submodules often come back to bite you and others..
  • humblerookie
    humblerookie about 4 years
    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. android.googlesource.com/platform/tools/base/+/… I ended up having to commit my local.properties however i ran into other issues as well
  • lasec0203
    lasec0203 almost 4 years
    Add the JitPack repository to build.gradle at the end of repositories: that part actually matters otherwise you may get weird build fails.
  • Johann
    Johann about 3 years
    A lot of work for something that should be simple.
  • Grant Birchmeier
    Grant Birchmeier about 3 years
    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.
  • Delark
    Delark over 2 years
    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.
  • chitgoks
    chitgoks over 2 years
    what if your repo is private?
  • Recessive
    Recessive over 2 years
    ':my-app' being what? It's only mentioned once. What does it syntactically mean?
  • Martin Braun
    Martin Braun about 2 years
    @chitgoks You can get private repositories to work using this advice (ignore the latter half though). However, you cannot use new key formats (<- unless this is closed) and bypassing this by using an RSA key is impossible, because GitHub disallows it. Once the issue is fixed it should work without the requirement to add any key file or modify your SSH config.
  • chitgoks
    chitgoks about 2 years
    ok. Ill see if this works. Currently I decided to use my private local repository.
  • Ovi Trif
    Ovi Trif about 2 years
    This should be the accepted answer.