简体   繁体   English

下载 Gradle 时 Ionic 构建 Android 错误

[英]Ionic build Android error when downloading Gradle

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.我下载 Gradle 并将其安装在 PC 上,但它想再次下载。 Can I add Gradle to a project offline?我可以将 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_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)

I am using Ionic version 1.7.14 and the distributionUrl found in the following file.我正在使用 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.zip and in build.js from myApp\\platforms\\android\\cordova\\lib\\build.js find 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';

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

I ran into this issue as our network proxy is blocking the download of the zip file.我遇到了这个问题,因为我们的网络代理阻止了 zip 文件的下载。 Here's what I found:这是我发现的:

NOTE: this differs from the "accepted" answer in that we aren't modifying Cordova's library directly.注意:这与“已接受”的答案不同,因为我们没有直接修改 Cordova 的库。 This is far less prone to "error" or being overwritten when you run ionic state restore当您运行ionic state restore时,这不太容易出现“错误”或被覆盖

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

  2. Put this file in platforms/android/gradle .将此文件放在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将环境变量CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL设置为../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 linux/osx: 添加export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL="../gradle-2.2.1-all.zip"到你的 ~/.bashrc 文件
    • windows: setx CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL ../gradle-2.2.1-all.zip windows: setx CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL ../gradle-2.2.1-all.zip
  4. Run ionic build android运行ionic build android

从命令行的另一个选项是运行$ export JAVA_OPTS="-Dhttp.proxyHost=proxy-url.com -Dhttp.proxyPort=911 -Dhttps.proxyHost=secure-proxy-url.com -Dhttps.proxyPort=911"之前,科尔多瓦构建命令。

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.如果您需要在您的网络上使用代理,请在%USER_HOME%/.gradle添加一个gradle.properties文件,如下所示,但将这些值替换为您的代理详细信息。 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

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 distributionUrl as follows: GradleBuilder.js 设置distributionUrl如下:

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

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.它奏效了。

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 .如果您不想使用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:它会自动在distributionUrl之前添加myApp/platforms/android/gradle/wrapper ,比如你把gradle-2.2.1-all.zip放在myApp/platforms/android/gradle目录下,那么你需要设置:

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

Alternatively, if you have a web server on your localhost, download and put the gradle (exactly the tested version, ie 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:或者,如果您的本地主机上有 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 具有未知协议。

我遇到了同样的问题,通过卸载 build-tools 24 并从 SDK Manager 安装 build-tools 23 解决了这个问题。

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

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 SDK 应该可以修复它,您可以使用终端中的这一行来完成:

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).另请参阅repos 问题(本质上是相同的问题)。

But after you do that rebuild, and it should work.但是在你重建之后,它应该可以工作。

It sounds like Windows Defender or some other antivirus system is messing up with your install.听起来 Windows Defender 或其他一些防病毒系统正在干扰您的安装。 So temporarily deactivate your firewall and re-run the command:所以暂时停用你的防火墙并重新运行命令:

ionic build android (or ios)

At the end you have the message BUILD SUCCESSFUL...最后,您会收到消息 BUILD SUCCESSFUL ...

As far as ionic development concern,you will have to modified the distributionURL in GradleBuilder.js with below mentioned URL.就离子开发而言,您必须使用下面提到的 URL 修改 GradleBuilder.js 中的 distributionURL。

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

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

I used every thing above but nothing was working and then I did this and boom it worked..!我使用了上面的所有东西,但没有任何效果,然后我做了这个并且繁荣它起作用了..!

在此处输入图片说明

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.这是我所做的: 1. 从您的项目中打开 cmd 2. 转到https://services.gradle.org/distributions/单击您需要的版本,右键单击并复制链接。 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! 3. 一旦完成设置变量:set CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL= https://services.gradle.org/distributions/gradle-2.2.1-all.zip 4. 好!

在此处输入图片说明

Updating the Android platform did the trick for me:更新 Android 平台对我有用:

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

This is because of a Gradle error.这是因为 Gradle 错误。

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下载文件http://services.gradle.org/distributions/gradle-2.14.1-all.zip ,解压到任意目录,打开cmd设置路径,输入: setx path (extracted directory)/bin

After that, reopen cmd and check whether Gradle is works or not by issuing gradle -v .之后,重新打开 cmd 并通过发出gradle -v检查 Gradle 是否正常工作。 It shows the Gradle version output.它显示了 Gradle 版本输出。 Now the error is fixed;现在错误已修复; run ionic build android .运行ionic build android

Download JDK version 8 and remove all existing versions.下载 JDK 版本 8 并删除所有现有版本。

And update Ionic.并更新离子。

I hope your problem will be solved.我希望你的问题会得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM