Gowtham Gowtham - 5 months ago 92
Java Question

Error when building a Cordova Android project

Hi i'm trying to build a cordova project with android plugin in windows 7 with
the following environment

ANDROID_HOME=D:\DevTools\Android\Android_SDK_NDK\android-sdk
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45


i generated a cordova project

>cordova create CordovaProject io.xyz.hellocordova CordovaApp


and added the android plugin

>cordova platform add android


now after doing that i tried to build the android project

>cordova build android


this trying to download gradle-2.2.1-all.zip which leads to exception as

E:\Cordova\Workspace\HelloCordova>cordova build android --verbose
Executing "before_build" hook for all plugins.
Executing "before_prepare" hook for all plugins.
Searching PlatformJson files for differences between project vs. platform instal
led plugins
No differences found between project and android platform. Continuing...
Generating config.xml from defaults for platform "android"
Wrote out Android application name to "HelloCordova"
Wrote out Android package name to "com.gowtham.HelloCordova"
This app does not have launcher icons defined
updated project successfully
Executing "after_prepare" hook for all plugins.
Executing "before_compile" hook for all plugins.
ANDROID_HOME=D:\DevTools\Android\Android_SDK_NDK\android-sdk
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45
Downloading http://services.gradle.org/distributions/gradle-2.2.1-all.zip

Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException
: Connection timed out: connect
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.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketI
mpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.ja
va:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocket
Impl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java
:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLC
onnection.java:996)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConne
ction.java:932)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection
.java:850)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:1300)
at org.gradle.wrapper.Download.downloadInternal(Download.java:59)
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
Error: Error code 1 for command: cmd with args: /s,/c,"E:\Cordova\Workspace\Hell
oCordova\platforms\android\gradlew cdvBuildDebug -b E:\Cordova\Workspace\HelloCo
rdova\platforms\android\build.gradle -Dorg.gradle.daemon=true -Pandroid.useDepre
catedNdk=true"

E:\Cordova\Workspace\HelloCordova>gradle -version

------------------------------------------------------------
Gradle 2.2.1
------------------------------------------------------------

Build time: 2014-11-24 09:45:35 UTC
Build number: none
Revision: 6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a

Groovy: 2.3.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.7.0_45 (Oracle Corporation 24.45-b08)
OS: Windows 7 6.1 x86


may be this was due to my office firewall/proxy which is not allowing to download the zip file. but i have the zip file extracted and set the path variable as


GRADLE_HOME = C:\gradle-2.2.1


even that also i'm not able to resolve this issue. I understand that the env. path variable is not accepted in build script but i'm not sure where to change or by-pass this step.

Answer

As you mentioned, it looks more of a network issue. The gradle download will fail if you are using restricted proxy network. In case of network restrictions, you can work around this issue by following the steps below:

1) Download the required gradle version zip file from gradle distribution link and save it in local folder.

2) Navigate to PROJECT_ROOT_FOLDER/platforms/android/cordova/lib/builders folder and edit GradleBuilder.js file

3) Change the line from var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'http\\://services.gradle.org/distributions/gradle-2.2.1-all.zip';to 'var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'file:///local/path/to/folder/where/gradle/zip/is/saved/gradle-2.2.1-all.zip'; and save the file

4) Try rebuilding android build.

You can also try the following as quick fix,

1) In command prompt execute the following command: export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=file:///local/path/to/folder/where/gradle/zip/is/saved/gradle-2.2.1-all.zip

2) Execute the following command: cordova run android

Comments