Configuring Repositories for All Projects in Gradle

I am trying to configure repositories for all subprojects.

I have the main build.gradle:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcenter()
        ...
    }
    dependencies {
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id 'base'
}

allprojects {
    apply plugin: 'base'

    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcentre()
        ...
    }

    wrapper{
        gradleVersion = '6.5.1'
        distributionType = Wrapper.DistributionType.ALL
    }

    dependencies {
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

In the subprojects build.gradle I just have:

...

dependencies {
    implementation ....
}

I am getting:

Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
   > Cannot resolve external dependency .... because no repositories are defined.
     Required by:
         project :

I want to define repositories once in the main file as these do not change in subprojects.


In the settings.gradle of the main project I have:

rootProject.name = 'main-project-name'

include 'sub-project-name'

And in the settings.gradle of the sub project I have:

rootProject.name = 'sub-project-name'

Upvotes: 5

Views: 5291

Answers (3)

Li Zheng
Li Zheng

Reputation: 731

I resovled the problem, as you can see in my APP commit RN 0.63.2 -> 0.70.5: fix Read timed out when compiling on Android , what I did is add below into settings.gradle:

// ref to https://docs.gradle.org/7.5.1/userguide/declaring_repositories.html#sub:centralized-repository-declaration
dependencyResolutionManagement {
    // to fix `Read timed out` e.g.
    //    > Could not resolve com.facebook.react:react-native:0.70.+.
    //       > Failed to list versions for com.facebook.react:react-native.
    //          > Unable to load Maven meta-data from https://maven.google.com/com/facebook/react/react-native/maven-metadata.xml.
    //             > Could not GET 'https://maven.google.com/com/facebook/react/react-native/maven-metadata.xml'.
    //                > Read timed out
    //    > Could not download bcprov-jdk15on-1.48.jar (org.bouncycastle:bcprov-jdk15on:1.48)
    //       > Could not get resource 'https://repo.maven.apache.org/maven2/org/bouncycastle/bcprov-jdk15on/1.48/bcprov-jdk15on-1.48.jar'.
    //          > Read timed out
    //    > Could not download guava-17.0.jar (com.google.guava:guava:17.0)
    //       > Could not get resource 'https://repo.maven.apache.org/maven2/com/google/guava/guava/17.0/guava-17.0.jar'.
    //          > Read timed out
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/central' }
        // maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }

        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }
        mavenCentral {
            // We don't want to fetch react-native from Maven Central as there are
            // older versions over there.
            content {
                excludeGroup "com.facebook.react"
            }
        }
        google()
        maven { url 'https://www.jitpack.io' }
    }
}

and now can also remove useless allprojects in build.gradle

Upvotes: 0

madhead
madhead

Reputation: 33412

Modern Gradle versions provide a recommended way to declare dependencies in a centralized way.

TLDR: Use the dependencyResolutionManagement DSL in your settings files to configure the repositories in all the subprojects. Both Groovy and Kotlin DSL look the same 👇

dependencyResolutionManagement {
    repositories {
        mavenCentral()
    }
}

Read more in the docs: "Centralizing repositories declaration".

Upvotes: 0

Lukas Körfer
Lukas Körfer

Reputation: 14493

A multi-project build in Gradle may have multiple build.gradle files, but only one settings.gradle file (usually in the root project directory). Your second settings.gradle files defines a second setup that only contains a single project. You can check this by running gradle projects. Just delete the second settings.gradle file to solve your problem.

Usually you can simply define the names of your sub-projects by naming the respective directories and then calling include. The name of the rootProject may be defined inside settings.gradle, because the name of the directory is often not stored in version control systems like Git. Developers may check out the repository to different directories causing Gradle to use different names for the root project. If you want a subproject to have a different name than its containing directory, use include with the desired name and then change the project directory via project(':foo').projectDir = file('path/to/foo').

Upvotes: 2

Related Questions