构建 NativeScript 项目时出现 java.util.zip.zipException

标签 java android nativescript gradlew

我收到了 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-barcodescannernativescript-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 缓存 ( ~/.npm/_cacache ) 并重新下载所有依赖项
  • 创建一个新的 NativeScript 项目(当我 tns add 插件时问题又回来了)
  • 删除插件并安装旧版本
  • 重新安装 NativeScript ( npm i -g nativescript )
  • 删除 Gradle 缓存文件夹 ( ~/.gradle/caches ) 并重试构建
  • 使用较新版本的 java(OpenJDK 11 而不是 8)

  • 我的(当前)猜测是某些文件已被错误下载。这很可能是因为我的 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/

    相关文章:

    android - 如何在 Flutter 插件中添加 OpenCV

    Angular 2 - 错误 : Supplied parameters do not match any signature of call target

    nativescript - 删除了 hooks 文件夹

    android - 为什么 Kotlin for Android Developers(该书)需要再次添加扩展 parseList?

    java - 在 Linux 上运行 jar 文件时出现此错误 - 线程 "main"java.lang.NoClassDefFoundError : org/apache/log4j/Logger 中的异常

    java - 可检查的GridView

    java - Java围绕另一个点旋转一个点

    Android Twitter sdk使用fabric获取特定用户的公共(public)推文

    ios - 发布 .ipa-File 作为 iTunes Connect 的更新

    java - 使用不同的源代码创建多个 APK