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

AndroidAndroid StudioAndroid BuildAndroid Gradle-Plugin

Android Problem Overview


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

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

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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.

Solution 6 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexey ZakharovView Question on Stackoverflow
Solution 1 - AndroidsunnydayView Answer on Stackoverflow
Solution 2 - AndroidtrupinView Answer on Stackoverflow
Solution 3 - AndroidMister SmithView Answer on Stackoverflow
Solution 4 - AndroidGaRRaPeTaView Answer on Stackoverflow
Solution 5 - AndroidsgarmanView Answer on Stackoverflow
Solution 6 - AndroidasherbretView Answer on Stackoverflow