My first Android project was a music player, which was built on Android Studio and as a beginner then, one knows that it gets very very difficult to solve some of the Android errors especially in case of a sync error or a Gradle error.
Here in this article we will go through the top 5 most common Android Gradle errors which you might face while working on an Android project and then we will also analyze the problems and try to solve them. So let’s first dive in to find what exactly is Gradle. 

What is Gradle?

Gradle is a build automation tool often used for JVM languages such as Java, Groovy or Scala.

One cool thing about gradle is that it is a plugin-based system. Gradle can be configured to run Tasks which do things like compile jars, run tests, create documentation and much more.

Similarly for Android, the build system automated and compiles the source code and then is used to package to deploy and distribute or to make its APK. Android studio uses Gradle to automated the build process while at the same time also allowing custom build configs. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app. The Android plugin for Gradle works with the build toolkit to provide processes and configurable settings that are specific to building and testing Android applications.

Fig – The default project structure for an Android app module

For more information about the build configuration, check this page on the official Android Studio site. 

1) How to update gradle in android studio?

After updating Android Studio, it is possible that the Gradle is not up to date with the latest version of Android Studio and this gives an error that looks like this-

Project is using an old version of the Android Gradle plug-in. The minimum supported version is 0.5.0. Please update the version of the dependency ‘com.android.tools.build:gradle’

After the latest release version of Android Studio, it will automatically show a popup to upgrade the version of your Gradle if needed, but you can do that manually as well.

For manually changing the Gradle version, just go to the top-level(project-level) build.gradle file and in the dependencies change the class. 

// Top-level build file where you can add configuration
// options common to all sub-projects/modules.
 
buildscript
{
    repositories
    {
        google()
            jcenter()
    }
    dependencies
    {
        classpath ‘com.android.tools.build:gradle:3.0.1’
        // change the gradle version as required
    }
}
allprojects
{
    repositories
    {
        google()
            jcenter()
    }
}
 
task clean(type
          : Delete)
{
    delete rootProject.buildDir
}

2) Error running android: Gradle project sync failed

In Android Studio sometimes due to any issue or unsaved changes, some of the Gradle files may get deleted unexpectedly. So when you will start building your apps you will get to see an error in Android studio as ‘Error running android: Gradle project sync failed. The error looks like this-

Error running android: Gradle project sync failed. Please fix your project and try again.

We can solve the error just by restarting your project using the Invalidate Caches and Restart option. You can find that option here:

Android Studio makes a lot of files as build and Gradle cache files and does not delete them. This can cause trouble when there is a need to make new files or some files are changed, so clearing caches will clear the old cache and make Android Studio faster.

3) Gradle DSL method not found

Below is an example of a build.gradle file code in a project.

apply plugin: ‘android’

android {
    compileSdkVersion 21
    buildToolsVersion ‘20.0.0’

    packagingOptions {
        exclude ‘META-INF/DEPENDENCIES’
        exclude ‘META-INF/LICENSE’
        exclude ‘META-INF/LICENSE.txt’
        exclude ‘META-INF/license.txt’
        exclude ‘META-INF/NOTICE’
        exclude ‘META-INF/NOTICE.txt’
        exclude ‘META-INF/notice.txt’
        exclude ‘META-INF/ASL2.0’
    }

    defaultConfig {
        applicationId ‘com.xxx.axxx’
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 6
        versionName ‘1.0’
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: ‘libs’, include: [‘*.jar’])
    compile ‘com.android.support:appcompat-v7:19.+’
    compile files(‘libs/commons-codec-1.8.jar’)
    compile files(‘libs/asmack-android-8-4.0.4.jar’)
    compile ‘com.android.support:support-v4:21.0.0’
    compile ‘com.google.code.gson:gson:2.2.4’
    compile ‘com.jakewharton:butterknife:5.1.1’
}

This build.gradle file gives an error as stated below-

Error: Gradle DSL method not found: ‘runProguard()’

To fix this problem you just have to replace “runProguard” with “minifyEnabled”.

If you are using version 0.14.0 or higher of the gradle plugin, you should replace “runProguard” with “minifyEnabled” in your build.gradle files.

runProguard was renamed to minifyEnabled in version 0.14.0. For more info, See Android Build System.

4) Failed to resolve: com.android.support:cardview- android

Mostly such errors appear when you are working with Android Studio 3 or below. This happens due to using incorrect dependencies.  

Error message – 

Failed to resolve: com.android.support:cardview- android

To resolve all such issues : 


If you are using Android Studio 3.0 or above make sure your project build.gradle should have content similar to-

buildscript {                
    repositories {
        google()  // add google() before jcenter()
        jcenter()
    }
    dependencies {           
        classpath ‘com.android.tools.build:gradle:3.0.1’

    }
}

allprojects {
    repositories {
        google()  // add google() before jcenter()
        jcenter()
    }
}

And for below Android Studio 3.0 and starting from support libraries 26.+ your project build.gradle must look like this-

allprojects {
    repositories {
        jcenter()
        maven {
            url “https://maven.google.com”
        }
    }
}

5) Cannot resolve symbol HttpClient

package com.mysite.myapp;

import org.apache.http.client.HttpClient;

public class Whatever {
    public void headBangingAgainstTheWallExample () {
        HttpClient client = new DefaultHttpClient();
    }
}

If you are using android SDK 23 or higher, and want to use HTTP client you will face an error which looks like this

Error: Cannot resolve symbol HttpClient

HttpClient is not supported anymore in sdk 23. You have to use URLConnection or downgrade to SDK 22 (compile ‘com.android.support:appcompat-v7:22.2.0’)

But even if you need sdk 23, add this to your gradle:

android {
    useLibrary ‘org.apache.http.legacy’
}

Conclusion


There are other tons of specific Gradle errors which you can face while working on a project, but in this article I have stated the most common android gradle build errors
We went through the common errors and also solved them while discussing what may have caused the error to occur.
The world of android app development is full of possibilities and enough errors as well, but it’s always better to not just solve the error but know the cause as well.
If you have any doubts or feedback, please leave a comment below and I will be more than happy to help.

Author

Tech-savvy Tejas girds up his loins to present to you the familiar technologies in a not so familiar fashion. His writings blow the whistles for the exploration enthusiast in you.