Search code examples
javaandroidnativescriptgradlew

java.util.zip.zipException while building NativeScript project


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?


Solution

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