'Error "Gradle sync failed: Connection refused: connect"

So I just installed Android Studio again after a long time and it asked me to configure the proxy, so I did. I tested the connection and it worked.

Then it asked me to configure the proxy settings for Gradle and I used the same configurations.

Now, when loading the latest Vuforia Sample project, I get an error:

Gradle sync failed: Connection refused: connect

I've looked everywhere on the web, but I can't find a solution. It can't be the proxy settings; I've configured Gradle just like the IDE.



Solution 1:[1]

You should configure Proxy settings in Android Studio’s menu (File ? Settings). Another strategy is to configure the file gradle.properties.

So, add these lines at the end of gradle.properties, located on the app folder base.


systemProp.http.proxyPassword=[PASSWORD]
systemProp.http.proxyHost=[IP ADDRESS]
systemProp.https.proxyPort=[PORT, TYPICALLY 3128]
systemProp.https.proxyUser=[USERNAME]
systemProp.https.proxyHost=[IP ADDRESS]
systemProp.https.proxyPassword=[PASSWORD]
systemProp.http.proxyPort=[PORT, TYPICALLY 3128]
systemProp.http.proxyUser=[USERNAME]

Pay attention. The passwords and other values that have special characters must have slash character such as (\#) and so on.

Solution 2:[2]

I was caught with the same issue and was trying bypass proxies on Mac.

I removed the proxy from the system level (.bashrc) and Android Studio (gradle.properties). I had no luck!

I found Android Studio unable to bypass the proxy and finally found the proxy was cached in Gradle in the below file.

~/.gradle/gradle.properties

Essentially, to make sure proxy references were muted in all gradle.prorties, including the Gradle cache.

Gradle cache could be find as below (in Android view)

Solution 3:[3]

This happened to me when I updated Android Studio to the latest version.

The fix was to go to menu File ? Settings ? Build Execution Deployment ? Gradle and check Use local Gradle distribution.

After that, in Gradle Home put the location to the Gradle directory - in my case it was C:\Program Files\Android\Android Studio\gradle\gradle-3.2.

Enter image description here

Solution 4:[4]

I have the same problem:

can't connect to socks proxy connection refused

Try

  1. Turn off VPN

  2. Menu File ? Invalidate Caches / Restart

This operation can fix this problem.

Solution 5:[5]

Use gradle.properties file in this path and check proxy info within:

C:\Users\[Windows-User]\.gradle\gradle.properties

I solve the problem by comment proxy definitions in this file.

Commented lines:

#systemProp.http.proxyHost=127.0.0.1
#systemProp.https.proxyPort=9050
#systemProp.https.proxyHost=127.0.0.1
#systemProp.http.proxyPort=9050

# used for comment in gradle files

Solution 6:[6]

I had the same problem recently. I had my proxy setup OK, but Gradle could not update. After a few attempts, I noticed that the Use HTTPS proxy check button was not selected.

I noticed that on the HTTPS proxy dialog which pops up while Android Studio starts.

Keep in mind that you might not be able to see the HTTPS setup from File ? Settings ? HTTP Proxy, so it is important to do it using the pop-up proxy dialog on Android Studio start.

You also may have the HTTPS form filled up, but you need to select that checkbox as well (at least that worked for me).

So here are the steps I follow:

  1. Invalidate caches and restart
  2. Close any project opens by default on Android Studio startup
  3. Select "Use https proxy on proxy dialog

Solution 7:[7]

This should work:

  1. Go to 'Gradle Scripts'

  2. Then go to 'gradle.properties'.

  3. There you will see proxy settings something like:

    systemProp.http.proxyHost=[YOUR PROXY]

    systemProp.http.proxyPort=[PORT]

    As you may see, this is for 'http' only.

  4. Just add the same for 'https':

    systemProp.https.proxyHost=[YOUR PROXY]

    systemProp.https.proxyPort=[PORT]

It worked for me.

Solution 8:[8]

When I got this error I used the Charles Proxy app (even though Charles Proxy was completely closed). To solve it, I just disabled SSL Proxying inside Charles Proxy.

After that I got

Cause: unable to find valid certification path to requested target

and

Connection refused

To solve it, just close Charles Proxy (with disabled (stopped) SSL Proxying) and restart Android Studio or analyse traffic and exclude unnecessary hosts (Proxy* -> *SSL Proxy Settings... -> Exclude block). In my case it was

<some_digits>.cloudfront.net

Solution 9:[9]

This worked for me

  1. commented these lines in Gradle properties

    systemProp.https.proxyPort=41258 systemProp.http.proxyHost=109.199.77.83 systemProp.https.proxyHost=109.199.77.83 systemProp.http.proxyPort=41258

  2. Use VPN

  3. Rebuild the project

Solution 10:[10]

What works for me:

  • Go to file
  • Invalidate cache / Restart
  • Select Invalidate & Restart

It should work now.

Solution 11:[11]

My case might be unique as in I got Android project code and was facing this error when trying to build it. I just removed the proxy properties in gradle.propertiesand it worked fine.

Solution 12:[12]

Invalidate caches and restart worked for me.

Solution 13:[13]

Try the latest version of the Gradle build tools in build.gradle(Module:app)

Here is a screenshot:

Screenshot

Solution 14:[14]

I solved my problem by putting the proxy options with http and https. Before it was just http and was giving an error. After I added https, it worked.

Solution 15:[15]

In my case, I verified that in File ? Settings ? HTTP proxy, I had checked the "no proxy" option.

Then I created an exception in my antivirus software for Android Studio, and deleted the lines of proxy configuration of my gradle.properties file. Finally, I clicked on Sync Project with Gradle files and it worked.

Solution 16:[16]

Check and review all of these files for having proxy settings:

  1. YOUR_PROJECT_ROOT/App/gradle.properties
  2. ~/.gradle/gradle.properties
  3. ~/.AndroidStudioX.Y/config/options/proxy.settings.xml

Note: replace X.Y with your current Android Studio version.

Solution 17:[17]

I did the following and my problem was easily solved:

  1. Go to menu File ? Settings ? Appearance & Behavior ? System Settings ? HTTP Proxy

  2. On this page, select option No Proxy.

  3. Download a VPN client.

  4. Connect to one of the servers and sync the Gradle.

I hope your problem is solved!

Solution 18:[18]

If there is VPN running on your computer you might have to turn it off.

Solution 19:[19]

I got the issue when I downloaded the Glide project and solved the issue by installing JDK 1.8.0_151 and reset the JAVA_HOME. My old JDK was 1.8.0_131, and I removed the old version.

Solution 20:[20]

On a Mac, open a terminal window and hit the below command:

echo $proxy

It should return your IP address and port number.

Now check your gradle.properties of your project and set the below properties as per your login details:

 systemProp.http.proxyPort=80
 systemProp.http.proxyUser=<userName>
 org.gradle.parallel=true
 systemProp.http.proxyPassword=<PassWord>
 org.gradle.jvmargs=-Xmx1536m
 systemProp.https.proxyHost=<IP Address>
 systemProp.http.proxyHost=<Proxy Host URL>
 systemProp.https.proxyPort=<Port number form above>

Solution 21:[21]

Go to Preferences ? Appearance & Behavior ? System Settings ? HTTP Proxy.

If your network needs a proxy, setup Manual proxy configuration.

Else, select Auto-detect proxy settings.

Solution 22:[22]

I find when I change the option of Preferences ? Appearance & Behavior ? System Settings ? HTTP Proxy to Manual proxy configuration, the Gradle properties of the project won't change, so I add:

systemProp.http.proxyHost= myProxyHost
systemProp.http.proxyPort= myProxyPort

Then it runs well.

Solution 23:[23]

In your gradle.properties file inside your app project, add the following properties (both HTTP and HTTPS):

systemProp.http.proxyHost=<proxyHost>
systemProp.http.proxyPort=<proxyPort>
systemProp.https.proxyHost=<proxyHost>
systemProp.https.proxyPort=<proxyPort>

If your network has a proxy (not defined in your system configuration), go to:

Menu File ? Settings ? Appearance & Behavior ? System Settings ? HTTP Proxy.

And set up Manual proxy configuration. Otherwise, check Auto-detect proxy settings.

Solution 24:[24]

A lot of people suggested changing proxy settings, but that was not my problem.

At the time of writing, com.android.tools.build:gradle:3.3.2 was recently released and I was not able to update to that from my network.

However, com.android.tools.build:gradle:3.3.1 was available and it no longer threw that error.

Solution 25:[25]

I finally found the solution is to disable Avast Antivirus in my case, so if you too have an antivirus, try it or make an exception for Android Studio.

Solution 26:[26]

If you ever used Shadowsocks X-NG on Mac, (mine is version 1.7.1 (1)) and once use the Global Mode, it will automatically add the following command:

~/.gradle/gradle.propertries:

#Mon Nov 18 03:59:09 CST 2019
systemProp.http.proxyHost=127.0.0.1
systemProp.https.proxyPort=1086
systemProp.https.proxyHost=127.0.0.1
systemProp.http.proxyPort=1086

Enter image description here

When you close Shadowsocks X-NG, Android studio still use the proxy, and that is why it shows

Gradle sync failed: Connection refused

Please comment those lines out, and rebuild the project again.

## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Mon Nov 18 03:59:09 CST 2019
#systemProp.http.proxyHost=127.0.0.1
#systemProp.https.proxyPort=1086
#systemProp.https.proxyHost=127.0.0.1
#systemProp.http.proxyPort=1086

Solution 27:[27]

Amending the gradle.properties file did not work for me. What worked was manually setting the proxy in settings. Additionally, I also had to enter my user name and password for the network.

Solution 28:[28]

In Android Studio 3.6.1 you can set the Proxy and after that, you can change that to no proxy. If still having the same issue then follow these steps:

  1. Check the Gradle location from in Settings ? Build, Execution ? Deployment* ? Gradle. Find the location of Gradle in your PC.

  2. Now go to that location and open the Gradle.properties file. In this file, you have to remove the proxy settings lines.

  3. That's it. Now your project is working.

Solution 29:[29]

For me it worked like this:

  1. turned off firewall and antivirus software
  2. deleted the .gradle file. Location: C:\Users\user\.gradle
  3. restarted Android Studio
  4. checked the Internet connection

P.S. There is no proxy in my system (windows 8.1)

And it worked!

Solution 30:[30]

I am writing on this old post as I have also faced the same problem after updating the Android Studio to 3.5. I was not using any proxy setting in my current project, still, it was showing me connection refused error. To resolve this problem I deleted the .android and .gradle file from User folder and restarted the android studio. It helped me to rebuild the project successfully.

Hope it will help you out.