73

I'm very new to the Ionic framework :)

>npm install -g cordova
>npm install -g ionic
>ionic start test blank
>cd test
>ionic platform add android

When I use '>ionic build android' start download gradle, but I can't download and get an error.

What caused this error and how can I fix it?!

I download Gradle and install it on a PC, but it wants to download again. Can I add Gradle to a project offline?

C:\Users\LENOVO2014\test>ionic build android
running cordova build android
Running command: "C:\Program Files\nodejs\node.exe" C:\Users\LENOVO2014\myApp\ho
oks\after_prepare\010_add_platform_class.js C:\Users\LENOVO2014\myApp
add to body class: platform-android
Running command: C:\Users\LENOVO2014\myApp\platforms\android\cordova\build.bat
ANDROID_HOME=D:\program\Programing\Android\sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_31
Running: C:\Users\LENOVO2014\myApp\platforms\android\gradlew cdvBuildDebug -b C:
\Users\LENOVO2014\myApp\platforms\android\build.gradle -Dorg.gradle.daemon=true
Downloading http://services.gradle.org/distributions/gradle-2.2.1-all.zip
................................................................................
................................................................................
................................................................................
............................................
Exception in thread "main" java.lang.RuntimeException: java.net.SocketException:
 Connection reset
        at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAcc
essManager.java:78)
        at org.gradle.wrapper.Install.createDist(Install.java:47)
        at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
        at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:48)
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:189)
        at java.net.SocketInputStream.read(SocketInputStream.java:121)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
        at sun.net.www.MeteredStream.read(MeteredStream.java:134)
        at java.io.FilterInputStream.read(FilterInputStream.java:133)
        at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Http
URLConnection.java:3335)
        at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Http
URLConnection.java:3328)
        at org.gradle.wrapper.Download.downloadInternal(Download.java:63)
        at org.gradle.wrapper.Download.download(Download.java:45)
        at org.gradle.wrapper.Install$1.call(Install.java:60)
        at org.gradle.wrapper.Install$1.call(Install.java:47)
        at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAcc
essManager.java:65)
        ... 3 more

C:\Users\LENOVO2014\myApp\platforms\android\cordova\node_modules\q\q.js:126
                    throw e;
                          ^
Error code 1 for command: cmd with args: /s /c "C:\Users\LENOVO2014\myApp\platfo
rms\android\gradlew cdvBuildDebug -b C:\Users\LENOVO2014\myApp\platforms\android
\build.gradle -Dorg.gradle.daemon=true"
ERROR building one of the platforms: Error: C:\Users\LENOVO2014\myApp\platforms\
android\cordova\build.bat: Command failed with exit code 1
You may not have the required environment or OS to build this project
Error: C:\Users\LENOVO2014\myApp\platforms\android\cordova\build.bat: Command fa
iled with exit code 1
    at ChildProcess.whenDone (C:\Users\LENOVO2014\AppData\Roaming\npm\node_modul
es\cordova\node_modules\cordova-lib\src\cordova\superspawn.js:131:23)
    at ChildProcess.emit (events.js:110:17)
    at maybeClose (child_process.js:1015:16)
    at Process.ChildProcess._handle.onexit (child_process.js:1087:5)
Mohsen
  • 4,536
  • 2
  • 27
  • 49
sadegh
  • 1,720
  • 1
  • 16
  • 30

18 Answers18

109

I am using Ionic version 1.7.14 and the distributionUrl found in the following file. myApp/platforms/android/cordova/lib/builders/GradleBuilder.js.

I had to point the locally downloaded gradle as specified in this answer.

I had the same problem. I added gradle to my project offline.

After downloading gradle from issued link (here is http://services.gradle.org/distributions/gradle-2.2.1-all.zip) paste it in somewhere likes myApp\platforms\android\gradle\gradle-2.2.1-all.zip and in build.js from myApp\platforms\android\cordova\lib\build.js find this:

var distributionUrl = 'distributionUrl=http\\://services.gradle.org/distributions/gradle-2.2.1-all.zip';

And replace it with your own file's location:

var distributionUrl = 'distributionUrl=../gradle-2.2.1-all.zip';
Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • I did the same thing , but its still trying to download from the old url. – hybrid Nov 17 '15 at 10:35
  • This line doesn't exist in build file. Ubuntu. – swade Feb 01 '16 at 22:26
  • Its working for me>>> i download gradle zip from https://services.gradle.org/distributions and paste it in.. C:\Users\suresh.kumar.NSI\.gradle\wrapper\dists\gradle-2.2.1-all\2m8005s69iu8v0oiejfej094b after running ionic build android its running. – SuReSh Feb 19 '16 at 04:53
  • 9
    real path is: project/platforms/android/cordova/lib/builders/GradleBuilder.js – Ivan Ferrer Jun 01 '16 at 20:38
  • 1
    This solution is "mostly" correct, although we should probably recommend *against* modifying the cordova JS code. it appears there is an environment variable you can set to accomplish this, which is a) less error prone and b) less likely to be "overwritten" when you delete your platforms directory – Jeff Woodard Jun 09 '16 at 17:15
  • 2
    I found the distribution url at following path & file 'myApp\platforms\android\cordova\lib\builders\GradleBuilder.js' – RAVI VAGHELA Jul 06 '16 at 07:15
  • Thank you. Should we use this in each project? – Vahid Najafi Aug 15 '16 at 06:24
  • 1
    I had the same problem ,but went away after multiple tries. Agree with @Jeff – Jeril Kuruvila Sep 28 '16 at 04:49
  • 1
    hi @Spara, my gradle zip file gets deleted everytime i build the app. Any solution to this? – AishApp Oct 03 '16 at 08:31
  • 2
    @aishapp see my answer. it's the correct solution. this answer shouldn't be the accepted one, nor should it be upvoted so high. we should NOT be modifying cordova's code. this stuff gets deleted if you run "cordova platform remove android" – Jeff Woodard Oct 05 '17 at 23:00
52

Since the solution posted above needs to include .zip into folder that is about 55MB

You can also force CLI to use Ant instead of gradle during a build with the following command:

cordova build android -- --ant
A.B
  • 20,110
  • 3
  • 37
  • 71
25

I ran into this issue as our network proxy is blocking the download of the zip file. Here's what I found:

NOTE: this differs from the "accepted" answer in that we aren't modifying Cordova's library directly. This is far less prone to "error" or being overwritten when you run ionic state restore

  1. Download gradle - http://services.gradle.org/distributions/gradle-2.2.1-all.zip

  2. Put this file in platforms/android/gradle. However, if you're integrating this into your build process, you may want to stick it somewhere less volatile and add a step to copy the file into that location.

  3. Set the environment variable CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL to ../gradle-2.2.1-all.zip

    • linux/osx: add export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL="../gradle-2.2.1-all.zip" to your ~/.bashrc file
    • windows: setx CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL ../gradle-2.2.1-all.zip
  4. Run ionic build android

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeff Woodard
  • 647
  • 8
  • 15
  • i don't get the set environment variable point in window. how to set in user variable or in system variable.? – muneeb_ahmed Sep 28 '19 at 15:37
  • In point 2) how to add a step to copy the file into that location. – muneeb_ahmed Sep 28 '19 at 16:29
  • 1
    @muneeb_ahmed You can set an environment variable w/ Powershell or through the "Environment Variables" UI. https://stackoverflow.com/questions/32730544/powershell-add-system-variable – Jeff Woodard Sep 30 '19 at 18:25
  • 1
    Also, as far as copying the file there, you can copy the file manually or add it to an npm task or something. there are myriad options for automating that. It would somewhat depend on the rest of your process. Typically that file will only go away after an ionic states restore. – Jeff Woodard Sep 30 '19 at 18:26
14

Another option from command line is to run $ export JAVA_OPTS="-Dhttp.proxyHost=proxy-url.com -Dhttp.proxyPort=911 -Dhttps.proxyHost=secure-proxy-url.com -Dhttps.proxyPort=911"before the cordova build command.

Lucas Massuh
  • 221
  • 2
  • 9
10

Cordova 6.0.0 / Visual Studio Code 0.10.6

TL;DR

Manually download http://services.gradle.org/distributions/gradle-2.2.1-all.zip.

Place it in appName\platforms\android\gradle\

set CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=..\gradle-2.2.1-all.zip

Start code and compile.

Explanation:

GradleBuilder.js sets distributionUrl as follows:

var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'http\://services.gradle.org/distributions/gradle-2.2.1-all.zip';

Script runs in appName\platforms\android\gradle\wrapper

Output is:

Downloading file://appname/platforms/android/gradle/gradle-2.2.1-all.zip

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user5880697
  • 101
  • 1
  • 4
  • It seems it's not depending on Cordova 6 in fact, but on [Cordova Android 5.0](https://cordova.apache.org/announcements/2015/11/09/cordova-android-5.0.0.html) - see [this issue](https://issues.apache.org/jira/browse/CB-9402). So when you update Cordova Android to 5.0, you can use `CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL` env param without the need to edit any build files. – jakub.g May 13 '16 at 13:05
9

If you require to use a proxy on your network then add a gradle.properties file in %USER_HOME%/.gradle with the below but replacing the values with your proxy details. This worked for me.

gradlePropertiesProp=gradlePropertiesValue
sysProp=shouldBeOverWrittenBySysProp   
envProjectProp=shouldBeOverWrittenByEnvProp  
systemProp.system=systemValue    
systemProp.http.proxyHost=myproxy.com    
systemProp.http.proxyPort=8080    
systemProp.http.nonProxyHosts=\*.mydomain.com|localhost   
systemProp.https.proxyHost=myproxy.com    
systemProp.https.proxyPort=8080    
systemProp.https.nonProxyHosts=\*.mydomain.com|localhost
arghtype
  • 4,376
  • 11
  • 45
  • 60
Ben Morris
  • 282
  • 3
  • 13
  • This is the answer for me (macOS). If `gradle.properties` file not present, simply create one in `platforms/android` Only needed: `systemProp.http.proxyHost`, `systemProp.http.proxyPort`, `systemProp.https.proxyHost`, `systemProp.https.proxyPort` – Jonathan Lin Jun 20 '17 at 09:48
6

Hi all I had the same issue.

In the platforms\android\cordova\lib\build.js file, I changed

var distributionUrl = 'distributionUrl=http\\://services.gradle.org/distributions/gradle-2.2.1-all.zip';

to

var distributionUrl = 'distributionUrl=http://services.gradle.org/distributions/gradle-2.2.1-all.zip';

and it worked.

aero
  • 1,654
  • 1
  • 21
  • 31
sid1337
  • 61
  • 1
  • 2
4

There seems to be a problem for the previous solutions. At least none of them worked for me, and I just changed it to:

var distributionUrl = 'http://localhost/gradle-2.2.1-all.zip';

Then it works. I don't know why they all add "distributionUrl=" as follows (this doesn't work for me):

 var distributionUrl = 'distributionUrl=http\\://localhost/gradle-2.2.1-all.zip';

BTW, I am using ionic 2.

Pay attention to the path if you don't want to use localhost. It will automatically add myApp/platforms/android/gradle/wrapper before distributionUrl, for example if you put gradle-2.2.1-all.zip in the myApp/platforms/android/gradle directory, then you need to set:

var distributionUrl = '../gradle-2.2.1-all.zip';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
seaguest
  • 2,510
  • 5
  • 27
  • 45
3

Alternatively, if you have a web server on your localhost, download and put the gradle (exactly the tested version, i.e. http://downloads.gradle.org/distributions/gradle-2.2.1-all.zip at the time of this post) in the www root and change the platforms\android\cordova\lib\build.js file in your application directory to this one:

 var distributionUrl = 'distributionUrl=http\\://localhost/gradle-2.2.1-all.zip';

Changing the URL to a local file (not an http address) would not work for me under windows and it would complain that the URL has an unknown protocol.

wmac
  • 1,023
  • 1
  • 16
  • 34
3

I have faced the same problem recently, and I searched here and there.

Finally for the latest build I found the solution.

After "ionic platform add android" command, go to platforms/android/cordova/lib/builders/GradleBuilder.js

Search for

http\\://services.gradle.org/distributions/gradle-2.2.1-all.zip

and change it to

http\\://downloads.gradle.org/distributions/gradle-2.2.1-all.zip
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tapan
  • 39
  • 1
3

I had the same problem, and it was solved with uninstalling build-tools 24, and installing build-tools 23 from SDK Manager.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emad Dehnavi
  • 3,262
  • 4
  • 19
  • 44
  • In android studo: This works if you have not upgraded to build-tools 25 and are still targeting api 23. After I upgraded the sdk, tareget api and build tools to 25, it wasn't a a problem. – iOSAndroidWindowsMobileAppsDev May 24 '17 at 12:43
0

All answers here are all saying the same thing when this could be done easier than the top answer. Updating your Android SDK should fix it, and you can do it with this one line from the terminal:

android update sdk --no-ui --all --filter "extra-android-m2repository"

or

android update sdk --no-ui --filter extra

Also see this repos issue (it's the essentially same problem).

But after you do that rebuild, and it should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
garrettmac
  • 8,417
  • 3
  • 41
  • 60
0

It sounds like Windows Defender or some other antivirus system is messing up with your install. So temporarily deactivate your firewall and re-run the command:

ionic build android (or ios)

At the end you have the message BUILD SUCCESSFUL...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Theophane Fotso
  • 313
  • 2
  • 4
  • 13
0

As far as ionic development concern,you will have to modified the distributionURL in GradleBuilder.js with below mentioned URL.

/android/cordova/lib/builders/GradleBuilder.js

var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'https://services.gradle.org/distributions/gradle-2.14.1-all.zip';

Sachin Mishra
  • 1,125
  • 1
  • 16
  • 17
0

I used every thing above but nothing was working and then I did this and boom it worked..!

enter image description here

Here is what I did: 1. open cmd from your project 2. goto https://services.gradle.org/distributions/ click on the version you need , right click and copy link. 3. Once done set the variable : set CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=https://services.gradle.org/distributions/gradle-2.2.1-all.zip 4. Good to go!

enter image description here

Gru
  • 67
  • 1
  • 7
-1

Updating the Android platform did the trick for me:

  • Cordova platform update Android
  • Cordova prepare,
  • Cordova compile and finally
  • Cordova build
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Priyanka
  • 15
  • 2
-1

This is because of a Gradle error.

Download the file http://services.gradle.org/distributions/gradle-2.14.1-all.zip and extract it in any of directory and set the path by open cmd and enter: setx path (extracted directory)/bin

After that, reopen cmd and check whether Gradle is works or not by issuing gradle -v. It shows the Gradle version output. Now the error is fixed; run ionic build android.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Navin a.s
  • 416
  • 2
  • 8
  • 19
-3

Download JDK version 8 and remove all existing versions.

And update Ionic.

I hope your problem will be solved.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Somnath
  • 368
  • 2
  • 9
  • 22