others-how to solve `Received status code 502 from server: Bad Gateway` problem when trying to build an app using gradle?

1. Purpose

In this post,I will demonstrate how to solve the following exception when trying to build an app using gradle:


Executing tasks: [:app:assembleProCnDebug] in project /Users/bswen/app3/myunit


> Configure project :app
FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:processProCnDebugResources'.
> Could not resolve all task dependencies for configuration ':app:proCnDebugRuntimeClasspath'.
   > Could not resolve com.alipay.sdk:alipaysdk-android:+.
     Required by:
         project :app
      > Failed to list versions for com.alipay.sdk:alipaysdk-android.
         > Unable to load Maven meta-data from https://jcenter.bintray.com/com/alipay/sdk/alipaysdk-android/maven-metadata.xml.
            > Could not HEAD 'https://jcenter.bintray.com/com/alipay/sdk/alipaysdk-android/maven-metadata.xml'. Received status code 502 from server: Bad Gateway

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.internal.tasks.TaskDependencyResolveException: Could not determine the dependencies of task ':app:processProCnDebugResources'.
    at org.gradle.api.internal.tasks.CachingTaskDependencyResolveContext.getDependencies(CachingTaskDependencyResolveContext.java:68)
    at org.gradle.execution.plan.TaskDependencyResolver.resolveDependenciesFor(TaskDependencyResolver.java:46)
    at org.gradle.internal.graph.CachingDirectedGraphWalker.findValues(CachingDirectedGraphWalker.java:73)
    at org.gradle.api.internal.tasks.CachingTaskDependencyResolveContext.getDependencies(CachingTaskDependencyResolveContext.java:66)
    ... 106 more
Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve com.alipay.sdk:alipaysdk-android:+.
Required by:
    project :app
Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Failed to list versions for com.alipay.sdk:alipaysdk-android.
Caused by: org.gradle.api.resources.ResourceException: Unable to load Maven meta-data from https://jcenter.bintray.com/com/alipay/sdk/alipaysdk-android/maven-metadata.xml.
    at org.gradle.api.internal.artifacts.repositories.maven.MavenMetadataLoader.load(MavenMetadataLoader.java:54)
    at org.gradle.api.internal.artifacts.repositories.maven.MavenVersionLister.listVersions(MavenVersionLister.java:48)
    at org.gradle.api.internal.artifacts.repositories.metadata.DefaultMavenPomMetadataSource.listModuleVersions(
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: org.gradle.internal.resource.transport.http.HttpErrorStatusCodeException: Could not HEAD 'https://jcenter.bintray.com/com/alipay/sdk/alipaysdk-android/maven-metadata.xml'. Received status code 502 from server: Bad Gateway
    at org.gradle.internal.resource.transport.http.HttpClientHelper.processResponse(HttpClientHelper.java:160)
    at org.gradle.internal.resource.transport.http.HttpClientHelper.performHead(HttpClientHelper.java:72)
    at org.gradle.internal.resource.transport.http.HttpResourceAccessor.getMetaData(HttpResourceAccessor.java:64)
    at org.gradle.internal.resource.transfer.DefaultExternalResourceConnector.getMetaData(
    at org.gradle.cache.internal.ProducerGuard$AdaptiveProducerGuard.guardByKey(ProducerGuard.java:97)
    at org.gradle.internal.resource.transfer.DefaultCacheAwareExternalResourceAccessor.getResource(DefaultCacheAwareExternalResourceAccessor.java:81)
    at org.gradle.api.internal.artifacts.repositories.maven.MavenMetadataLoader.parseMavenMetadataInfo(MavenMetadataLoader.java:60)
    at org.gradle.api.internal.artifacts.repositories.maven.MavenMetadataLoader.load(MavenMetadataLoader.java:50)
    ... 200 more


* Get more help at https://help.gradle.org

BUILD FAILED in 4s

The environment in {project.dir}/gradle/gradle_wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip



2. Solution

Before solving the problem, we should know what is HTTP 502 exception:

The HyperText Transfer Protocol (HTTP) 502 Bad Gateway server error response code indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.

And what is a HTTP HEAD request?

The HTTP HEAD method requests the headers that would be returned if the HEAD request’s URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file

So the problem is happened when gradle what to send a HTTP HEAD request to the URL: https://jcenter.bintray.com/com/alipay/sdk/alipaysdk-android/maven-metadata.xml, but it got a HTTP 502 response.

We should use the latest gradle to query for the URL, so:

The solution to this problem is to change your gralde/gradle-wrappers.properties file as follows:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

Now build again:

Download https://services.gradle.org/distributions/gradle-6.7-all.zip finished, took 28 s 265 ms (146.23 MB)
Starting Gradle Daemon...
Gradle Daemon started in 1 s 180 ms
> Task :prepareKotlinBuildScriptModel UP-TO-DATE
Calling mockable JAR artifact transform to create file: /Users/bswen/.gradle/caches/transforms-2/files-2.1/13b9ae895826e75e88d8930fcd91dda3/android.jar with input /Users/bswen/Library/Android/sdk-new/platforms/android-30/android.jar

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2m 26s



3. Why does it work?

The reason of this problem is that:

The problem is that a plugin (location in this case) didn’t specify a fixed version of an Android library. So in order to find which versions are available, Gradle must go to the repository to check. In this case, that repository is Bintray, which has been down for days and returning HTTP 502 Bad Request.

So , you can change your gradle distribution version to solve this problem, if still not found, you can add the following repositories to your project’s build.gradle:(remove jcenter() and google() first)

maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }

Hope it helps for you.



4. Summary

In this post, I demonstrated how to solve the Could not HEAD 'https://jcenter.bintray.com/com/alipay/sdk/alipaysdk-android/maven-metadata.xml'. Received status code 502 from server: Bad Gateway, the key is to upgrade your gradle distribution version or find a better alternative gradle repositories. That’s it, thanks for your reading.