Thursday, January 16, 2014

How to Fix Gradle Build Errors with Android Studio

Hi Cross Linked Coders,

I was trying to build an old ADT project for Android with Android Studio 0.4.2 and I faced a whole bunch of error messages.

The errors were all related to an older gradle version.

Essentially deleting the gradle 1.8 folder and placing the gradle 1.9 folder was one of the many steps to fix this.

Build.gradle needed to be changed to:
classpath 'com.android.tools.build:gradle:0.7.+'
and
buildToolsVersion "19"
The final error message was the most cryptic essentially the build was error free but when attempting to run the application there was a gradle sync error message. What solved this was clicking on the "Sync Project with Gradle Files" button. Which looks like this on the toolbar:



After going through all this I found a somewhat interesting list to fix the gradle issues here:
http://tools.android.com/recent/androidstudio040released

However 0.4.2 is the latest Android Studio so their list is a little out of date but is still helpful.

One thing to note is adding external .jar libraries to projects that don't use gradle you can drag the jar files into the project and then right click on the file and add as a library to the project. If the project is using gradle simply create a "libs" folder in the project and add all the .jar libraries to that folder.

Update 1:
If you have multiple modules in your project to solve gradle errors it is best to use the same version for the following structures:
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
android {
buildToolsVersion "20.0.0"
}

Update 2:
If you get an error such as:
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define...

Any Dex errors in my case have always been because a library has been included twice in the module or project, either through copying the .jar file to the project file space or through module library linking.

Update 3 -- Slightly off-topic:
If you have any Android SDK Manager errors when either downloading from a repository or repository information is not showing up or is out of date -- then the best thing to do is to delete all termporary files which is done through the JAVA settings of your computer.