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

后端 未结 5 457
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 19:27

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

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

5条回答
  •  佛祖请我去吃肉
    2020-11-30 19:29

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

提交回复
热议问题