AS 3.0 + Gradle 4.1 - understanding repository {} block in allprojects vs buildscript

Hi guys, let’s say I have the following root level build.gradle file:

buildscript {
    repositories {
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
    }
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        google()
    }
}

This would sync fine even though hugo is in the jitpack repo instead of the google() repo, which makes me believe that the buildscript somehow knows about jitpack via the allprojects {} block.

So I remove the repository {} block from buildscript {} altogether expecting that it will also know about the google() repository from the allprojects {} block, but now gradle complains that it cannot resolve the android gradle plugin (which is located in the google() repository). What gives? Thanks!

I suspect that you’ve got com.jakewharton.hugo:hugo-plugin:1.2.1 cached locally and Gradle is finding that. If I take the same build script, I get an error resolving that dependency:

> Could not resolve all files for configuration ':classpath'.
   > Could not find com.jakewharton.hugo:hugo-plugin:1.2.1.
     Searched in the following locations:
         https://dl.google.com/dl/android/maven2/com/jakewharton/hugo/hugo-plugin/1.2.1/hugo-plugin-1.2.1.pom
         https://dl.google.com/dl/android/maven2/com/jakewharton/hugo/hugo-plugin/1.2.1/hugo-plugin-1.2.1.jar
     Required by:
         project :

Thank you! Good to know I’m not crazy.

This leads to another question: I’ve been having the hardest time cleaning all my library cache, I’ve tried ./gradlew build --refresh-dependencies and that doesn’t even re-download all deps.

I’m not sure what you mean “doesn’t even re-download all deps” – --refresh-dependencies only forces Gradle to check for changes to the dependencies in the project. If it doesn’t find a change in the dependency, it doesn’t need to download anything. What kind of problem are you seeing/trying to solve?

In this case, even if you’ve removed the repository, Gradle will still have the dependency in its cache and will be able to satisfy the dependency request without looking externally.

Ah totally misunderstood the purpose of --referesh-dependencies, I thought it would be a brute force way to get gradle to ignore the cache and re-fetch all dependencies, which is what I’m trying to do

Why do you need to refetch all dependencies?

I was playing around with different repository {} configurations and trying to tease out how things work, was surprised to see the behavior mentioned in my original post. Now I’m just looking for a way to replicate the error you mentioned but don’t know how. I’ve tried deleting the .gradle directory, cleanBuildCache, uninstalling Android Studio and even deleting and re-cloning the project.

Are you removing your Gradle user home (~/.gradle) or the project’s .gradle?

You can just try using a temporary Gradle user home to see the same behavior:

gradle -g gradle-user-home buildEnvironment

This creates a directory called gradle-user-home in the current directory and uses that as your Gradle user home, which is where Gradle caches downloaded dependencies.

Worked like a charm! Thank you and sorry for all the confusing questions, just starting to dig into gradle.

Actually now that I’m building in a new gradle home, I’m still able to resolve dependencies like hugo, and they resolve from https://plugins.gradle.org/m2

Example input:

buildscript {
    repositories {
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
        classpath 'com.romandanylyk:pageindicatorview:0.2.0@aar'
    }
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        google()
    }
}

Example output:

Download https://plugins.gradle.org/m2/com/jakewharton/hugo/hugo-plugin/1.2.1/hugo-plugin-1.2.1.pom
Download https://plugins.gradle.org/m2/com/jakewharton/hugo/hugo-plugin/1.2.1/hugo-plugin-1.2.1.jar
Download https://plugins.gradle.org/m2/com/romandanylyk/pageindicatorview/0.2.0/pageindicatorview-0.2.0.pom
Download https://plugins.gradle.org/m2/com/romandanylyk/pageindicatorview/0.2.0/pageindicatorview-0.2.0.aar