Saturday, February 1, 2014

Solving errors in Android Studio's new project creation

The other day I upgraded my Android Studio bundle to latest stable version: android-studio-bundle-132.893413-windows.

Shortly afterwards, I noticed that each time I tried to create a new project, it ended with the following error message:
Cause: error in opening zip file. Consult IDE log for more details (Help | Show Log)










Hmmmm.. which error file exactly and what's wrong with it?

Let's open the log file - Help > Show Log in Explorer > idea.log or %USERPROFILE%\.AndroidStudioPreview\system\log\idea.log

Latest logs show the following messages:
INFO - ject.ProjectImportErrorHandler - Failed to import Gradle project at 'C:\Users\user\AndroidStudioProjects\MyApplicationProject2'
org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from 'http://services.gradle.org/distributions/gradle-1.8-bin.zip'.
...
WARN - nal.AbstractExternalSystemTask - Cause: error in opening zip file
com.intellij.openapi.externalSystem.model.ExternalSystemException: Cause: error in opening zip file
...
WARN - .project.GradleProjectImporter - com.intellij.openapi.externalSystem.model.ExternalSystemException: Cause: error in opening zip file
...
INFO - .project.GradleProjectImporter - Cause: error in opening zip file

Clearly something is wrong with Gradle plugin. Android Studio generally downloads Gradle archive in background and stores it under %USERPROFILE%\.gradle\wrapper\dists\gradle-1.x-bin\hash-code\gradle-1.x-bin.zip. While the archive usually weighs about 30 megabytes, my gradle-1.8-bin.zip archive was a 50 kb file.

I opened the file in text editor and found that it contained the html of my proxy server's error page. It seems that Android Studio issues a get command to download the Gradle archive, and without checking the received MIME type, just blindly writes the stream to the gradle-1.x-bin.zip file. Later on, the module that responsible for new project creation tries to extract the Gradle plugin and obviously fails.

To fix that issue you have two options:
  1. [Workaround] Manually download the appropriate zip file, replace the existing archive and restart Android Studio.
  2. Delete the existing archive and fix your proxy settings.

To fix your proxy settings, go to File > Settings. Click the HTTP Proxy option on the left pane.
Select the Use Proxy radio button, and enter your proxy server's Host name and Port number.

Initially I didn't checked the Proxy authentication and left Login and Password fields blank. I restarted the Android Studio and received another error message (at least this time much more informative):
Could not GET 'http://repo1.maven.org/maven2/com/android/tools/build/gradle/'. Received status code 401 from server: Unauthorized
Consult IDE log for more details (Help | Show Log)

My proxy server requires authorization, so I eventually had to enter my credentials in Login and Password fields. Restarted the Android Studio, it succeeded to download the required Gradle archive, and I finally was able to create new project.



2 comments:

  1. Sir.. we tried the above steps...Tried to delete the gradle archive file..but couldn't find one..,...G:\ConfecResource\gradle\wrapper\gradle wrapper, gradle wrapper properties..That is all we have..!
    Kindly help..!
    Thanks in advance..

    ReplyDelete
    Replies
    1. If you can't find the file I can't really help you with that. On my Windows machine %USERPROFILE%\.gradle\wrapper\ folder contains 'dists' folder. If you don't have it - maybe the problem is somewhere else. Have you checked the errors in Android Studio's log?

      Delete