Android Studio sync fails with connection refused or connection timed out
Gradle sync failed: Connection refused: connectShort 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.
Fix it fast
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.properties2Match what you see, then jump to its fix
- 1
The network only reaches the internet through a proxy, and neither file has any proxyHost lineNo proxy is set anywhereFix - 2
gradle.properties has http.proxyHost but no https.proxyHostOnly the http keys are set, but repositories use httpsFix - 3
The terminal build works, but sync inside Android Studio failsThe IDE proxy page overrides a correct gradle.propertiesFix - 4
Connection works for other tools in the browser but Gradle still fails behind an authenticating proxyThe proxy needs a loginFix
- 1
What does the error look like?
Gradle sync failed: Connection refused: connectConnection timed outjava.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.properties2Fix
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=80803Verify
Sync the project again, or resolve dependencies from the terminal.
./gradlew --refresh-dependencies helpConfirmed by developer.android.com
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.properties2Fix
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|localhost3Verify
Run the same task again and watch the first download succeed.
./gradlew helpConfirmed by docs.gradle.org
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.properties2Fix
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.com3Verify
Click Sync Project with Gradle Files again.
./gradlew helpConfirmed by developer.android.com
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.properties2Fix
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=DOMAIN3Verify
Run a dependency download from the terminal.
./gradlew --refresh-dependencies helpConfirmed by docs.gradle.org
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 fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix