我收到了 java.util.zip.ZipException
在为 Android (tns prepare android
) 准备我的 NativeScript 项目时:
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
此错误似乎仅在安装某些插件(
nativescript-barcodescanner
和 nativescript-socket.io
)时发生。如果我尝试在没有这些插件的情况下进行构建,则构建工作正常。所以这可能不是 Gradle 本身的问题(例如 this answer )。第一次出现
我第一次遇到
nativescript-barcodescanner
的问题从版本 3.2.0
升级 NativeScript 后至4.2.4
并随后更新我的项目包。升级前一切正常。但是,问题并非特定于我的项目。如果我创建一个新项目并添加
nativescript-barcodescanner
$ tns create testproject && cd testproject
$ tns plugin add nativescript-barcodescanner
$ tns prepare android
...然后错误再次出现。当我
tns remove nativescript-barcodescanner
,构建运行正常。采取的步骤
一开始我以为是
nativescript-barcodescanner
的问题插件(here is my GitHub issue)。但是,我现在在使用 NativeScript-Vue 模板时遇到了同样的错误,其中构建在 nativescript-socket.io
处失败。 .我尝试了以下步骤,但没有成功:
node_modules
并重新运行 npm install
~/.npm/_cacache
) 并重新下载所有依赖项 tns add
插件时问题又回来了)npm i -g nativescript
) ~/.gradle/caches
) 并重试构建 我的(当前)猜测是某些文件已被错误下载。这很可能是因为我的 Internet 连接不是很好,并且可能在中间断开了连接。但是,我无法确定哪个文件导致了问题(我宁愿不重新下载整个设置)。
全输出
这是我的 NativeScript 设置信息:
$ 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.
这是一个示例运行(这是使用 OpenJDK 8,所以它说
error opening zip file
而不是更详细的 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)
有什么方法可以找出导致错误的 zip 文件,或者我可以尝试调试此问题的其他方法吗?
最佳答案
TL;DR: 我的 ~/.gradle
中的一个 zip 文件是错误的。重新下载文件解决了这个问题。
我的推理错误是这样的:
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).
一些构建工作的事实不一定暗示 Gradle 设置正确。 NativeScript 似乎为不同的任务使用不同的版本(在我的例子中,
gradle-4.4-bin
用于插件构建,而 gradle-4.4-all
用于完整项目构建)。如果只有项目构建版本有问题,构建将适用于某些设置,但在还需要插件构建时会失败。解决方案
快速解决方案:删除
~/.gradle
文件夹并再次运行构建命令(如 this GitHub issue 中所述)。较慢的解决方案:为避免重新下载整个设置,请查看
gradle-*.zip
文件,位于 ~/.gradle/wrapper/dists
,并找出哪个有问题(这可能意味着它没有正确下载)。这些文件将被命名为(在我的例子中)这样的:~/.gradle/wrapper/dists/gradle-4.4-bin/bgaq7vklkazwgxox0hdadxbvi/gradle-4.4-bin.zip
要检查有问题的 zip,请将文件大小和/或 sha-sum 与 this Gradle Distribution List 进行比较. 只需打开
.zip
测试不足 ,因为许多文件管理器打开部分下载/不完整的 zip 文件而不提示。找到错误文件后,将其删除并重新运行
tns build
命令。它将自动下载丢失的文件并继续构建。或者,您可以从上面的链接手动下载文件并将其放置在正确的目录中。
关于构建 NativeScript 项目时出现 java.util.zip.zipException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52947693/