Android Studio – Failed to Sync Gradle project – Fix

By | March 25, 2015

Matching SDK versions identified in build.gradle (Module: app) with the SDK versions you have installed via SDK Manager, then “Sync Now”, will fix most Android Studio build errors.

Common Android Studio build errors:

  • Failed to Sync Gradle project
  • Failed to find target android-21
  • Install missing platform(s) and sync project

If you’re building / running someone else’s project that you’ve cloned from Github likely you’ll run into build errors in Android Studio.

Most Android Studio project build errors can be fixed in one file: /project/app/build.gradle

There are multiple Gradle build files, frustratingly named build.gradle.  You want the build.gradle (Module: app) Gradle file.

android-studio-build-gradle

Here’s a typical Gradle build.gradle file

android {
 compileSdkVersion 21
 buildToolsVersion "21.1.2"

 defaultConfig {
 applicationId "com.domain.appname"
 minSdkVersion 18
 targetSdkVersion 21
 versionCode 1
 versionName "1.0"
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 }
}

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:appcompat-v7:21.0.3'
}

Starting from the top of build.gradle file and moving down,

  • compileSdkVersion
    • This number should not be higher than the highest SDK version you have installed via SDK Manager.  You can scroll through SDK Manager looking for SDK Platform entries, checking the API # (18 in the screenshot) and the Status – Installed:
    • android-sdk-version-installed
  • buildToolsVersion
    • This line in your build.gradle should be set to the latest Android SDK build-tools version you have installed, not a revision/version that matches your target SDK.  Again, the build tools version # is not related to your SDK version #.  In general you want to install and use the latest build tools version.
    • Visible in SDK Manager here:
    • android-sdk-build-tools-version
  • targetSdkVersion
    • This should generally be the same as your compileSdkVersion
  • dependencies
    • Here is where support libraries are identified / referenced.
    • Note: If you don’t specifically need libraries from the support library, such as Support FragmentActivity, remove the reference! Comment out (double forward slash //) these libraries or delete the line entirely.
    • Example:
      compile 'com.android.support:appcompat-v7:21.0.3'
    • A version # suffix is used when identifying the support library, which comes after the colon (21.0.3 in above example).  This specifies the version of the support library, not your SDK version.  Revisions of the support library are listed here.  Generally you’ll want to use the latest unless you specifically need to use previous versions of methods/functions in older support library revisions.
    • In SDK Manager, you want to ensure you have Support Repository installed.
    • Android Studio uses the Support Repository, not the Support Library (which I assume is for Eclipse projects).  Make sure you have the Support Repository installed.  Generally you’ll want this library updated to the latest version, so update it within SDK Manager when you see an update is available.
  • After correcting version numbers in build.gradle, Android Studio will request you Sync with the following message:
    • Gradle files have changed since last project sync.  A project sync may be necessary for the IDE to work properly.  Sync Now.
    • Click the blue Sync Now link to sync your project.
    • gradle-sync-now
  • After sync try building your project using Cmd + F9 (Control + F9 for Windows).  Hopefully the Android Studio project builds without errors.
  • If all goes well, you see a tiny completion message, bottom left corner saying: Gradle build finished in 2 sec.
  • If things go wrong, the Messages window will pop-up showing the error
  • gradle-sync-error
  • Language Level Changed – Answer Yes
  • language-levels-change


Related Articles: