Java 下载 Gradle 时 Ionic 构建 Android 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29874564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:44:43  来源:igfitidea点击:

Ionic build Android error when downloading Gradle

javaandroidionic-frameworkgradlebuild

提问by sadegh

I'm very new to the Ionic framework :)

我对 Ionic 框架很陌生:)

>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.

当我使用“ >ionic build android”时,开始下载 gradle,但无法下载并出现错误。

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?

我下载 Gradle 并将其安装在 PC 上,但它想再次下载。我可以将 Gradle 离线添加到项目中吗?

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_prepare0_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.call(Install.java:60)
        at org.gradle.wrapper.Install.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)

采纳答案by Spara

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

我正在使用 Ionic 版本 1.7.14 和以下文件中的 distributionUrl。 myApp/platforms/android/cordova/lib/builders/GradleBuilder.js.

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

我必须指向本答案中指定的本地下载的 gradle。

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

我有同样的问题。我离线将 gradle 添加到我的项目中。

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.zipand in build.js from myApp\platforms\android\cordova\lib\build.jsfind this:

从发布的链接(这里是http://services.gradle.org/distributions/gradle-2.2.1-all.zip)下载 gradle 后,将其粘贴到某个地方,myApp\platforms\android\gradle\gradle-2.2.1-all.zip然后在 build.js 中myApp\platforms\android\cordova\lib\build.js找到:

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';

回答by A.B

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

由于上面发布的解决方案需要将 .zip 包含到大约 55MB 的文件夹中

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

您还可以使用以下命令强制 CLI 在构建期间使用 Ant 而不是 gradle:

cordova build android -- --ant

回答by wmac

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.zipat 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:

或者,如果您的本地主机上有 Web 服务器,请下载并放置 gradle(正是经过测试的版本,即http://downloads.gradle.org/distributions/gradle-2.2.1-all.zip在此期间post) 在 www 根目录中,并将应用程序目录中的platforms\android\cordova\lib\build.js 文件更改为以下文件:

 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.

将 URL 更改为本地文件(不是 http 地址)在 Windows 下对我不起作用,它会抱怨 URL 具有未知协议。

回答by sid1337

Hi all I had the same issue.

大家好我有同样的问题。

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

在platforms\android\cordova\lib\build.js 文件中,我改了

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.

它奏效了。

回答by Lucas Massuh

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.

命令行中的另一个选项是$ export JAVA_OPTS="-Dhttp.proxyHost=proxy-url.com -Dhttp.proxyPort=911 -Dhttps.proxyHost=secure-proxy-url.com -Dhttps.proxyPort=911"在cordova build 命令之前运行。

回答by Ben Morris

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

如果您需要在您的网络上使用代理,请在下面添加一个gradle.properties文件,%USER_HOME%/.gradle但用您的代理详细信息替换这些值。这对我有用。

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

回答by Priyanka

Updating the Android platform did the trick for me:

更新 Android 平台对我有用:

  • Cordova platform update Android
  • Cordova prepare,
  • Cordova compile and finally
  • Cordova build
  • Cordova 平台更新 Android
  • 科尔多瓦准备,
  • 科尔多瓦编译,最后
  • 科尔多瓦构建

回答by user5880697

Cordova 6.0.0 / Visual Studio Code 0.10.6

Cordova 6.0.0 / Visual Studio 代码 0.10.6

TL;DR

TL; 博士

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

手动下载http://services.gradle.org/distributions/gradle-2.2.1-all.zip

Place it in appName\platforms\android\gradle\

把它放在 appName\platforms\android\gradle\

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

Start code and compile.

启动代码并编译。

Explanation:

解释:

GradleBuilder.js sets distributionUrlas follows:

GradleBuilder.js 设置distributionUrl如下:

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

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

脚本运行 appName\platforms\android\gradle\wrapper

Output is:

输出是:

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

下载文件://appname/platforms/android/gradle/gradle-2.2.1-all.zip

回答by Tapan

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

在“ionic platform add android”命令之后,转到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

回答by seaguest

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):

然后它起作用了。我不知道为什么他们都添加“distributionUrl=”如下(这对我不起作用):

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

BTW, I am using ionic 2.

顺便说一句,我正在使用离子 2。

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

如果不想使用,请注意路径localhostmyApp/platforms/android/gradle/wrapper在distributionUrl之前会自动添加,比如你把gradle-2.2.1-all.zip放在myApp/platforms/android/gradle目录下,那么你需要设置:

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