skip to Main Content
AGP 4.2.2
gradle-6.7.1
arcticfox 2020.3.1    

Everytime I build my android project I see this maven-metadata.xml and it just seems to slow the build and it just seems to hang there. Which can take 10 minutes for each build.

Is there a way to avoid this?

Gradle: Download maven-metadata.xml...

When I run one the command line I see this:

enter image description here

This is my gradle.properties

org.gradle.jvmargs=-Xmx8g -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=256m -XX:+UseCompressedOops -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configureondemand=true

kotlin.coroutines=enable
kotlin.code.style=official
kapt.use.worker.api=true
kapt.incremental.apt=true

android.enableD8.desugaring=true
android.useAndroidX=true
android.enableJetifier=true

I am using some maven repositories as specified here in build.gradle(app):

maven { url "https://www.jitpack.io" }
maven { url 'http://......./repo/maven2' }
maven { url "https://....../repositories/snapshots" }
maven { url 'https://....../maven/release' }
maven { url "http://......./bintray.com/maven" }
maven { url "https://....../objectbox/objectbox" }

5

Answers


  1. The cause of slow downloads for maven-metadata.xml files is that one of your proxy repositories has a very slow remote.
    This problem will be compounded if either of the "metadata max age" and "not found cache ttl" settings is set too low. Thus it will check the circle too fast and download it everytime from scratch. Turn it up to 12 hours. Default should be 30min (1800sec) what is a too short period of time.

    For setting the max age you can simple follow along this site.

    Another good article to manage Http Cache Headers.

    Login or Signup to reply.
  2. The question is kind of insufficient to tell, because no build.gradle had been provided. But I’d assume, that the reason is any -SNAPSHOT dependency, which is not being cached. Building against a local copy of that dependency should result in not downloading the maven-metadata.xml over and over again; or use mavenLocal(). Building against a stable version should also result in caching.

    Login or Signup to reply.
  3. Gradle usually needs to download a maven-metadata.xml file, if

    1. you use a dynamic/changing version for one of your dependencies, and
    2. the time to live (TTL) in the dependency cache is over.

    For (1) you should look for versions like 1.2.3-SNAPSHOT or 1.2.+ on your declared dependencies (or at ./gradlew dependencies for simplicity). If you have any such versions, do you really need them? Using fixed versions like 1.2.3 should be faster.

    For (2), have you maybe changed the default TTL threshold or do you maybe always build with --refresh-dependencies? If not, then the slowness should at least not occur more often than once a day.

    If the above doesn’t help, then maybe try running your build with --info or --info --refresh-dependencies and carefully watch the log output. That should show which dependency and/or which repository is the culprit (i.e., the one on which the logging is stuck for the longest time). If you find such a culprit, then you could look into things like

    • replacing the dependency (or the repository),
    • improving the speed of the repository server or the network, or
    • pruning old snapshot versions from the problematic maven-metadata.xml file (if you control the repository).

    If all this still doesn’t turn up actionable items, then maybe it could be worth looking for bigger maven-metadata.xml files in your Gradle dependency cache. Maybe the repeated download of such big files is the issue and one of the approaches from the previous list could help. Here’s an idea for how to find the ten biggest maven-metadata.xml files (with a Unix shell command):

    find ~/.gradle/caches/modules-2/resources-* 
            -name maven-metadata.xml 
            -printf '%st%pn' 
        | sort -k1 
        | tail
    
    Login or Signup to reply.
  4. Chriki answer directed me in the right direction to find a simple solution. After you have downloaded all dependencies (in my case, I am using -SNAPSHOT library, which wants to be updated on every run), you can set Gradle in offline mode. This will prevent Gradle to download new metadata, etc.

    In Android Studio you have a dedicated button to toggle online mode (see image
    Android studio settings).

    Login or Signup to reply.
  5. Other answers give very good context on why this happens. Especially when using -SNAPSHOT dependencies, if you have no other way, I have a trick for you that might help a lot.

    It is to use offline mode 🎉

    When running via CLI, do

    ./gradlew someTask --ofline
    

    When running via IDE, enable offline mode.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search