The problem

The following error message may (in most cases) be reported when a Flutter project is created using VScode:

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:214) at org.gradle.wrapper.Install.access$600(Install.java:27) at org.gradle.wrapper.Install$1.call(Install.java:74) at org.gradle.wrapper.Install$1.call(Install.java:48) at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:65) at org.gradle.wrapper.Install.createDist(Install.java:48) at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61) Running Gradle task 'assembleDebug'... Running Gradle task 'assembleDebug'... The Done 1.8 s [!]  Gradle threw an error while downloading artifacts from the network. Retrying to download...Copy the code

Analysis of the

The last line of the error message reads:

Gradle threw an error while downloading artifacts from the network.
Copy the code

An error occurred when downloading Gradle configuration files from the network. Where in our project do we need to configure and download Gradle? Gradle and gradle-wrapper.properties. Now that you know what the problem is, let’s start with these two files and try to solve it.

To solve

Gradle file. We know that the configuration information in this file is downloaded from Google and JCenter. Both of these download sources are very slow to access in China. We need to configure buildScript and AllProjects nodes at the same time, and replace the content of these two nodes with ali Cloud image source:

repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
         maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
         maven { url 'http://maven.aliyun.com/nexus/content/repositories/google'}
         maven { url 'http://maven.aliyun.com/nexus/content/repositories/gradle-plugin'}
        jcenter()
    }
Copy the code

After flutter Run is executed, errors are still reported, which indicates that the error has nothing to do with the mirror source itself. However, for convenience in future use, we still retain the mirror source configuration of Aliyun.

Since the way we configured the mirror source didn’t solve our problem, let’s try modifying gradle-wrapper.properties. The gradle-wrapper.properties file of the flutter project created by vscode has a default gradle version 5.6.2. I have a newer version 6.7-all. We replaced the default version with a relatively new one

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
Copy the code

Then flutter Run was executed again and no errors were reported and the application was successfully installed on the phone. Gradle is running at a lower level than gradle.