java.util.zip.zipException while building NativeScript project - java

I am getting a java.util.zip.ZipException while preparing my NativeScript project for Android (tns prepare android):
Failed to build plugin nativescript-barcodescanner :
Error: Command ./gradlew failed with exit code 1 Error output:
Exception in thread "main" java.util.zip.ZipException: zip END header not found
This error seems to be occurring only when certain plugins (nativescript-barcodescanner and nativescript-socket.io) are installed. If I try to build without those plugins, the build works properly. So it's probably not a problem with Gradle itself (as in e.g.this answer).
First occurrence
I first encountered the problem with nativescript-barcodescanner after upgrading NativeScript from version 3.2.0 to 4.2.4 and subsequently updating my project packages as well. Everything was working fine before the upgrade.
However, the problem is not specific to my project. If I create a new project and add nativescript-barcodescanner
$ tns create testproject && cd testproject
$ tns plugin add nativescript-barcodescanner
$ tns prepare android
...then the error shows up again. When I tns remove nativescript-barcodescanner, the build runs properly.
Steps taken
At first, I thought it was a problem with the nativescript-barcodescanner plugin (here is my GitHub issue). However, I now ran into the same error with a NativeScript-Vue template, where the build failed at nativescript-socket.io.
I have tried the following steps, without success:
Deleting node_modules and re-running npm install
Clearing the npm cache (~/.npm/_cacache) and re-downloading all dependencies
Creating a fresh NativeScript project (problem comes back when I tns add the plugin)
Removing the plugin and installing an older version
Re-installing NativeScript (npm i -g nativescript)
Deleting the Gradle cache folder (~/.gradle/caches) and retrying the build
Using a newer version of java (OpenJDK 11 instead of 8)
My (current) guess is that some file that has been incorrectly downloaded. This is quite likely because my Internet connection is not very great and might have disconnected in the middle. However, I can't make out which file is causing trouble (I'd rather not re-download the whole setup).
Full output
Here is my NativeScript setup info:
$ tns info
✔ Getting NativeScript components versions information...
✔ Component nativescript has 4.2.4 version and is up to date.
✔ Component tns-core-modules has 4.2.1 version and is up to date.
✔ Component tns-android has 4.2.0 version and is up to date.
✔ Component tns-ios has 4.2.0 version and is up to date.
And this is a sample run (this was with OpenJDK 8 so it says error opening zip file instead of the more detailed zip END header not found):
$ tns create bartest
npm notice created a lockfile as package-lock.json. You should commit this file.
added 3 packages in 295.509s
Project bartest was successfully created.
$ cd bartest
$ tns plugin add nativescript-barcodescanner
+ nativescript-barcodescanner#2.7.8
added 1 package in 97.781s
Successfully installed plugin nativescript-barcodescanner.
$ tns prepare android
Copying template files...
Platform android successfully added. v4.2.0
Preparing project...
Successfully prepared plugin nativescript-barcodescanner for android.
Successfully prepared plugin nativescript-theme-core for android.
Successfully prepared plugin tns-core-modules for android.
Successfully prepared plugin tns-core-modules-widgets for android.
Failed to build plugin nativescript-barcodescanner :
Error: Command ./gradlew failed with exit code 1 Error output:
Exception in thread "main" java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:225)
at java.util.zip.ZipFile.<init>(ZipFile.java:155)
at java.util.zip.ZipFile.<init>(ZipFile.java:169)
at org.gradle.wrapper.Install.unzip(Install.java:215)
at org.gradle.wrapper.Install.access$600(Install.java:27)
at org.gradle.wrapper.Install$1.call(Install.java:75)
at org.gradle.wrapper.Install$1.call(Install.java:48)
at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:69)
at org.gradle.wrapper.Install.createDist(Install.java:48)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:107)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Is there any way to figure out which zip file is causing the error, or other way I can try to debug this problem?

TL;DR: One of the zip files in my ~/.gradle was faulty. Re-downloading the file solved the problem.
My error in reasoning was this:
This error seems to be occurring only when certain plugins
(nativescript-barcodescanner and nativescript-socket.io) are
installed. If I try to build without those plugins, the build works
properly. So it's probably not a problem with Gradle itself (as in
e.g.this answer).
The fact that some builds work does not necessarily imply that Gradle is set up properly. NativeScript seems to use different versions for different tasks (in my case, gradle-4.4-bin for plugin builds but gradle-4.4-all for full-project builds). If only the project-build version is faulty, builds will work for some setups but fail when a plugin build is also required.
Solutions
Quick solution: Delete the ~/.gradle folder and run the build command again (as described in this GitHub issue).
Slower solution: To avoid re-downloading the whole setup, look through the gradle-*.zip files, located in ~/.gradle/wrapper/dists, and find out which one is faulty (which probably means it wasn't downloaded properly). The files will be named something like (as in my case) this:
~/.gradle/wrapper/dists/gradle-4.4-bin/bgaq7vklkazwgxox0hdadxbvi/gradle-4.4-bin.zip
To check for faulty zips, compare the filesize and/or sha-sum to this Gradle Distribution List. Simply opening the .zip is not sufficient testing, because many file managers open partially-downloaded/incomplete zipfiles without complaining.
Once you find the faulty file, delete it and re-run the tns build command. It will automatically download the missing file and continue with the build. Alternatively, you can manually download the file from the above link and place it in the correct directory.

go to the .gradle folder
find the latest Gradle version if have
and
after open the VS code and in file explore project-> Android->gradle/wrapper->gradle-wrapper.properties
paste distributionUrl=https://services.gradle.org/distributions/gradle-7.0.2-all.zip
7.0.2 <- this change with the version (enter the latest gradle version)
and Run the project !!

I encountered the problem and noticed that the downloaded gradle-x.x.x.x.zip size differs from the real size (gradle-7.3.3-bin size was 110 MB but the downloaded one was 101 MB). Therefore, I decided to download it manually and change distributionUrl to the local file
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=gradle-7.3.3-bin.zip
I downloaded gradle-7.3.3-bin.zip and inserted it to:
%PROJECT_DIR%\platforms\android\gradle\wrapper
And the error gone !

Related

Gradle version not supported after Android Studio update

I am running on Windows 10.
I just updated the Android studio to 3.6, and my app is no longer syncing. I'm getting 3 separate responses for the failure
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id 'com.android.internal.version-check']
Caused by: java.lang.RuntimeException: Minimum supported Gradle version is 5.6.4. Current version is 5.4.1. If using the gradle wrapper, try editing the distributionUrl in C:\Users\redacted\redacted\Documents\Android Studio\app\gradle\wrapper\gradle-wrapper.properties to gradle-5.6.4-all.zip
ERROR: Minimum supported Gradle version is 5.6.4. Current version is 5.4.1.
Please fix the project's Gradle settings.
Fix Gradle wrapper and re-import project (Appears as an un-clickable link for me)
Open Gradle wrapper properties (Appears as an un-clickable link for me)
Gradle settings (Appears as an un-clickable link for me)*
Being smart as I am, I checked my gradle-wrapper properties:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
In the project structure, the Gradle version is also correct, 5.6.4.
I have uninstalled and reinstalled Android Studio.
I have also tried running the program on a different machine.
I created a brand new (working!) project to compare the gradle-wrapper properties file and everything matches.
Please help me out if you can.
Update as to how I resolved the situation. I started a brand new project, and copied over all the Kotlin and XML code. I'm still not sure what went wrong.
I renamed the project directory and re-imported the project, and it worked. Then I changed it back to the old name and it continued working. Magic!
P.S.
BTW I also deleted all the .iml files .idea .gradle files gradle gradlew gradlew.bat files also.
Yes, it just ignores what is in gradle-wrapper.properties. After a week of frustration and trying dozens of different solutions that didn't solve it...
Create a new 3.6.1 dummy project and Gradle Sync to force download of the new Gradle distribution.
Then in File -> Settings -> Gradle, set Use Gradle from to Specified location and set the full path of where your gradle is (e.g. C:\Users\Duncan\.gradle\wrapper\dists\gradle-5.6.4-all\ankdp27end7byghfw1q2sw75f\gradle-5.6.4). And finally it works again!
Then you can delete the new dummy project.
Why is Android Studio / Gradle so ridiculously overcomplicated and unreliable ? Every upgrade is a nightmare.
I followed the advice of these awesome Flutter error messages and simply replaced in my project's file <project>/android/gradle/wrapper/gradle-wrapper.properties the line distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip by distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip (thereby only changing the version of the employed gradle module).
That solved the issue.
Just upgrade (Increase) the build.gradle version

Kotlin Koans with EduTools plugin: "Failed to launch checking"

I'm trying to follow the Kotlin Koans tutorial in Android Studio by installing the EduTools plugin and choosing Kotlin Koans course.
Everything works fine, but when I try "Check Task" in the Task Description panel, I get this error:
Failed to launch checking
I also tried the plugin with Intellij IDEA and got the same error.
Using:
Android Studio 3.2 with EduTools 2.0-2018.1-443
Intellij IDEA 2018.2.3 with EduTools 2.0-2018.2-906
Maybe could try IntelliJ Idea Edu which comes already bundled with EduTools.
I found the problem in IDE logs as #NullReference suggested.
INFO - ecker.gradle.GradleCommandLine - Failed to launch checking
Cannot run program "./gradlew" (...) No such file or directory
I had configured the project (Settings / Build, Execution, Deployment / Build Tools / Gradle) to use a local Gradle distribution and it seems that EduTools plugin always looks for the Gradle Wrapper, so I just had to generate a Wrapper for project and configure it to use my local Gradle distribution:
gradle wrapper --gradle-distribution-url file:///path/to/local/gradle-4.6-all.zip
In my case I had a problem related to JDK 11 and I had JDK 8 installed.
This is how I fixed the problem:
Go to Settings / Build, Execution, Deployment / Build Tools / Gradle.
At Gradle JVM select JDK 11 path.
It worked for me.
I also got this issue. Finally, I 've resolved it by configuring my gradle bin to PATH.
Here are series of commands to configure Gradle into you Path.
# cd ~
# touch .bash_profile
# open -e .bash_profile
//to edit your bash_profile ,you need to input your local path to bash_profile,like this:
export GRADLE_HOME=/Users/PCNAME/.gradle/wrapper/dists/gradle-4.5-bin/bgd6nrwgxy3inh8yed6gmsvbx/gradle-4.5/ --- use your own grade path to replace them.
export PATH=${PATH}:${GRADLE_HOME}/bin
# source .bash_profile
# gradle -v // you will see your gradle version if your configure successfully.
Restart Android Studio, try to run your EDUTOOLS.
I received the same error. I closed Android Studio and created a new Koans project thinking I must have done something wrong. This time I received a new build time error when trying to "Check". None of this made sense so I just shut down Android Studio, reopened the Koans project I just created and clicked "Check". Everything seemed to work now.
Had the same problem. Just Sharing how I fixed this problem.
I opened build.gradle of my Kotlin Koans Project and scrolled down to where wrapper was defined.
Pressed play icon right next to it.
Was greeted with an error
Error:Failed to open zip file.
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
Re-download dependencies and sync project (requires network)
Followed Answer by Hamid
To resolve this issue close Android Studio and delete the following
directory's content, necessary files will be downloaded on IDE's next
launch.
macOS: ~/.gradle/wrapper/dists
Linux: ~/.gradle/wrapper/dists
Windows: C:\Users\your-username.gradle\wrapper\dists
Restarted Android Studio and it worked like a charm.

Failed to open .zip Gradle Cache May be corrupt error

I am facing Gradle cache error and I don't know how to tackle it. I have attached the error snap with my question below. I tried different solutions posted at stack-overflow but none of them worked. Anyone suggest me a solution Any additional suggestions would be helpful.
e.g Error: Failed to open zip file.e.g
Error: Failed to open zip file.
Download the latest gradle package from this directory
Extract it and put it somewhere (for example f:\gradle-1.10)
Go to your Android Studio and load your project then open File->Settings->gradle, in this page click on Use local gradle distribution.
Type your gradle folder address there.
Its because your gradle directory is not the directory of your gradle, to check go to android studio open your project then open File->Settings->Build,Execution, Deployment->Gradle
https://i.stack.imgur.com/wVFyh.png
Hey not necessary to set gradle manually. i have also faced this issue but i deleted all files in gradle folder path "C:/Users/username/.gradle/"
simple steps follow
first goto path "C:/Users/username/.gradle/"
delete all files in above path.
open android studio and sync gradle or build again or create new project.
it will be auto downloaded new gradle version files and auto build.
*Make sure you your gradle settings like this
- checked "Use default gradle wrapper(recommended)
- unchecked offline work
You can download manually but this is another solution:
Change "https" to "http" on 'gradle-wrapper.properties' in root project.
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
to,
distributionUrl=http\://services.gradle.org/distributions/gradle-3.3-all.zip
first :i tried to delete the .gradle folder from project it doesn't
work;
second: i tried use the old version gradle ,but studio force me to
use gradle-3.3,so it confused me long time;
third:I noticed the tips unzip exception"java.util.zip.ZipException:
error in opening zip file", I tried to unzip the file by handset's
really can't unzip, so the problem must happen in the download
process.
solution: I copy the file "gradle-3.3-all.zip " from my colleague who build success,and replace mine in such path ".gradle/wrapper/dists/gradle-3.3-all/55gk2rcmfc6p2dg9u9ohc3hw9/gradle-3.3-all.zip "
If you want to use gradle wrapper - delete anything under:
.gradle/wrapper/dists/
and refresh gradle.
The gradle wrapper will download from scratch and everything should work fine.
I solved this problem with the installation of Gradle using HomeBrew on Mac. If you type gradle -v in terminal and shows command not found: gradle, then I think you can use this way to solve this problem on Mac:
First install the Gradle with Homebrew: brew install gradle
Then go to Android Studio, open Android Studio->Preferences->Build,Execution,Deployment->Gradle, under Project-level settings check: Use local gradle distribution and set Gradle home with: '/usr/local/Cellar/gradle/3.5/libexec' (3.5 is my gradle version)
Try again, and if the version is not correct, you can manually download the specified version from gradle website (the version specified in the file <your project dir>/gradle/wrapper/gradle-wrapper.properties: distributionUrl=<url>), extract the downloaded zip file to the location and set the Gradle home to it in Android Studio.
Hope that helps.
From my MAC i just go to the gradle folder (/Users/USER/.gradle) > wrapper > dist and delete the gradle folder with the problem. Then go to studio and re-download the gradle file.
In my case, I changed distributionUrl in gradle-wrapper.properties from
https\://services.gradle.org/distributions/gradle-6.5-bin.zip
to
https\://services.gradle.org/distributions/gradle-6.5-all.zip
cd /root/.gradle/wrapper/dists
ls
gradle-5.6.4-all gradle-6.5-all gradle-6.6.1-bin
gradle-6.1.1-bin gradle-6.5-bin
for example (gradle-6.1.1-bin) :
rm -R gradle-6.1.1-bin

How to Setup gradle-2.2.1-all.zip Manually

I want to run my unit test with this command
gradlew.bat connectedInstrumentTest
But it tries to download https://services.gradle.org/distributions/gradle-2.2.1-all.zip
And my corporal network blocks it. Hence, I want to setup the zip manually, I already downloaded it from browser.
The ways I failed are listed below
Pasting it in Program Files\Android\Android Studio\gradle\
gradle-2.2.1 didn't work
File -> Settings -> Build execution, Deployment -> Build Tools and
defining the gradle home directory didn't work
Any suggestions except these will be much appreciated. Thanks.
Solution :
1). Delete the C:\Users\username\.gradle folder
2). Download http://downloads.gradle.org/distributions/gradle-2.2.1-all.zip
3). Create a new project in android studio.
4). When this time it stucks at gradle building, Force close the android studio.
5) Now go to C:\Users\username\.gradle\wrapper\dists\gradle-2.2.1-all\c64ydeuardnfqctvr1gm30w53 (the end directory name may be different)
6) Delete gradle-2.2.1-all.zip.lck and other files from this directory.
7) Paste the new downloaded gradle-2.2.1-all.zip here.
8) Run the android studio. :)
In your command you're using the Gradle Wrapper. The Gradle wrapper is a thin wrapper around the real Gradle bin. It's intended to be checked in to your source control and it then downloads the version of Gradle you've defined in gradle/wrapper/gradle-wrapper.properties. The advantage here is the version of Gradle used in the build is directly tied to the source control.
You have 2 options:
use the gradle command instead of gradlew - this will use the gradle that's installed on the local machine (assumes that 'gradle' is on the PATH). The drawback here is you have to have installed Gradle somewhere before the build will work.
change distributionUrl in gradle/wrapper/gradle-wrapper.properties to point to a different URL and host that file yourself somewhere. You get all the advantages of the wrapper but fix the problems you're having downloading it.
Mostly, this error occurs due to brake in internet or slow connection network.
A gradle file downloads partially, this zip file isn't extractable and neither does re-download using Android Studio work.
So, to solve this problem, you have to:
Delete the gradle download folder or download the gradle zip file manually and configure using the following method
Download requires gradle and past the zip into C:\Users\your_pc_name\.gradle\wrapper\dists
Finally, open Android Studio and run the project.
The gradle version care fully before downloading

android ant error: invalid resource directory name: ./bin/res\crunch

I'm using ant command line to build an android project with an external library. (For some reason, I can't make it into a jar to put in libs/)
After completing all the steps of android update and reference link for the project and the lib project, the error of "invalid resource directory name: ./bin/res\crunch" pops out when running "ant release". I know it is a common error but failed to solve it after doing intensive research and trying out all the solutions, including
1) project->clean
2) delete ../bin/res/crunch. (it will re-appear and create the trouble)
3) change android version or SDK version to higher one
Any suggestion? Thanks!
A cleaner solution is to clean before building:
ant clean
ant release
You don't really have to copy things from the 'crunch' directory to the 'res' directory, since these are generated as part of the build anyway.
It appears that the root cause of this problem is incompatibility between ADT v.22.2.1 and ANT.
Related threads:
https://plus.google.com/+AndroidDevelopers/posts/hNZNjqKLpny
https://code.google.com/p/android/issues/detail?id=60496
Finally I figured it out: copied everything from bin/res/crunch folder and pasted it in res and then deleted the crunch folder. If the library project is modified, the error will reappear. Redoing the procedure above will fix it again.

Categories