Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Android Studio sync fails with connection refused or connection timed out

ErrorGradle sync failed: Connection refused: connect

Short answer

These errors mean Gradle could not open a socket to the repository. Behind a company proxy the cause is almost always proxy settings: missing, set only for http, overridden by the IDE, or missing credentials. Set one proxy in Android Studio, mirror it in gradle.properties for command-line builds, and keep the https keys too.

  • Applies to Android Studio current release, Gradle 8.x and 9.x, Android Gradle Plugin 8.x and 9.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which proxy values Gradle really has. Print the user-level and project-level properties files, since user-level values win over project ones.

    cat ~/.gradle/gradle.properties
    cat gradle.properties
  2. 2Match what you see, then jump to its fix

    1. 1The network only reaches the internet through a proxy, and neither file has any proxyHost lineNo proxy is set anywhereFix
    2. 2gradle.properties has http.proxyHost but no https.proxyHostOnly the http keys are set, but repositories use httpsFix
    3. 3The terminal build works, but sync inside Android Studio failsThe IDE proxy page overrides a correct gradle.propertiesFix
    4. 4Connection works for other tools in the browser but Gradle still fails behind an authenticating proxyThe proxy needs a loginFix

What does the error look like?

  • Gradle sync failed: Connection refused: connect
  • Connection timed out
  • java.net.ConnectException

Cause 1 of 4

No proxy is set anywhere

1Check

A refused connection means nothing accepted the connection at the address Java tried. On a proxy-only network, Gradle needs the proxy values to reach any repository.

grep -i proxy ~/.gradle/gradle.properties gradle.properties

2Fix

In Android Studio open Settings > Appearance & Behavior > System Settings > HTTP Proxy and choose Manual proxy configuration. For command-line or CI builds also set the system properties in gradle.properties.

systemProp.http.proxyHost=proxy.example.com
systemProp.http.proxyPort=8080
systemProp.https.proxyHost=proxy.example.com
systemProp.https.proxyPort=8080

3Verify

Sync the project again, or resolve dependencies from the terminal.

./gradlew --refresh-dependencies help

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 4

Only the http keys are set, but repositories use https

1Check

Gradle reads separate keys for each protocol. Maven Central and Google Maven are fetched over https.

grep -n "proxy" gradle.properties

2Fix

Add the https.proxyHost and https.proxyPort pair. Only the non-proxy host list is shared: it is http.nonProxyHosts for both protocols.

systemProp.https.proxyHost=proxy.example.com
systemProp.https.proxyPort=8080
systemProp.http.nonProxyHosts=*.internal.example.com|localhost

3Verify

Run the same task again and watch the first download succeed.

./gradlew help

Confirmed by docs.gradle.org

Did this fix it?

Cause 3 of 4

The IDE proxy page overrides a correct gradle.properties

1Check

Android's documentation states the IDE HTTP Proxy page overrides the proxy values in gradle.properties when you build from Studio. A stale entry there, such as an old office proxy, wins.

grep -i proxy ~/.gradle/gradle.properties gradle.properties

2Fix

Make both places carry the same proxy, or choose No proxy in the IDE if you are off the corporate network, and remove old systemProp lines.

# gradle.properties: delete or correct stale lines such as
systemProp.http.proxyHost=old-proxy.example.com

3Verify

Click Sync Project with Gradle Files again.

./gradlew help

Confirmed by developer.android.com

Did this fix it?

Cause 4 of 4

The proxy needs a login

1Check

Gradle takes proxy credentials from its own properties. NTLM proxies need the Windows domain as well.

grep -i "proxyUser\|ntlm" ~/.gradle/gradle.properties

2Fix

Put the credentials in the user-level file, not in the repository, and give the domain either inside the user name or in its own key.

# ~/.gradle/gradle.properties
systemProp.https.proxyUser=DOMAIN/username
systemProp.https.proxyPassword=secret
# or, with the domain in its own property:
systemProp.http.auth.ntlm.domain=DOMAIN

3Verify

Run a dependency download from the terminal.

./gradlew --refresh-dependencies help

Confirmed by docs.gradle.org

Did this fix it?

How do I stop it coming back?

  • Keep proxy values in ~/.gradle/gradle.properties, which applies to every project and keeps passwords out of version control.
  • Set the same proxy in the IDE and in gradle.properties so Studio and the terminal behave the same.
  • Remove proxy lines when you leave the network that needed them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android app crashes because the Apache HTTP classes are missing

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;

From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.