Gradle - inherit repositories from a module

2019-05-23 01:09发布

问题:

First of all, I explain my project setups. I use the words Project/Module as used in Android Studio.

I have a setup of my projects like following:

  • LibraryProject => a project that just groups my libraries, that I use in other projects, if necessary

    • BackupLibrary (Module)
    • DatabseLibrary (Module)
    • MainUtilityLibrary (Module)
    • DialogLibrary (Module)
  • MyProject1

    • App (Project)
    • DialogLibrary (Module, referenced from DIRFFERENT project)

My Project1 now looks like following:

1) I reference the module from the other project, that I want to use (done in settings.gradle (Project Settings) file:

include ':app'

include ':dialogLibrary '
project(':dialogLibrary ').projectDir = new File(settingsDir, '../LibraryProject/DialogLibrary ')

2) I add the foreign library module as a dependency to my app's build.gradle:

dependencies {
    compile project(':dialogLibrary ')
}

My LibraryProject looks like following:

I add the repository and the dependency directly to the module's `build.gradle' (this is enough to build the module on it's own without a problem):

allprojects {
    repositories {
        maven { url 'https://dl.bintray.com/drummer-aidan/maven' }
    }
}

dependencies {
    compile 'com.afollestad:material-dialogs:0.7.5.2'
}

Problem

The setup so far does not work for MyProject1. I have to add maven { url 'https://dl.bintray.com/drummer-aidan/maven' } to MyProject1 as well, the compile project(':dialogLibrary ') is not enough. I have to add the dependency that should somehow be inherited from the LibraryModule to MyProject1 Project build.gradleagain like following:

allprojects {
    repositories {
        mavenCentral()
        maven { url 'https://dl.bintray.com/drummer-aidan/maven' } // com.afollestad:material-dialogs
    }
}

Otherwise, i can't compile my project. This way seems like doing something twice that probably can be done automatically. But how? This way, whenever I include a module from my LibraryProject, I have to check if I need an additional maven repo...

回答1:

I think it has to do with the fact that Gradle has no knowledge of the LibraryProject's settings.gradle file while building MyProject1. So it includes the dialogLibrary as if it is a normal subproject of it's own. This explains the reason why the Maven repo is not know to MyProject; LibraryProject's configuration has not been loaded.

Maybe you can include the dialogLibrary through the LibraryProject? Although I did some testing and could NOT get it to work. It probably has to do with a limitation of Gradle. There is a StackOverflow post about multiple settings.gradle files here.

A work around could be to build the Modules of the LibraryProject and use them instead. For example build the DialogLibrary to a jar, upload it to a Maven repository and than in your App project just include DialogLibrary as normal dependency (don't forget to include the Maven repo).