Monthly Archives: March 2015

Android Studio – Failed to Sync Gradle project – Fix

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.

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'), ''

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile ''

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:
  • 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:
  • 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 ''
    • 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.
  • 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
  • Language Level Changed – Answer Yes

Android Studio Git Clone Push Pull Repository

For personal development in Android Studio, using BitBucket as remote repository here’s my VCS workflow using Git.

Android Studio VCS setup for untracked project:

  • VCS -> Import into Version Control
  • Create Git Repository
  • Add files & directories to track in Git, using File Explorer (left window)
    • Since these are personal private projects, git add all Android Studio files except:
      • /build
      • /app/build
      • External Libraries (these will be pulled in via Gradle upon sync/build)
    • Create a .gitignore file in the project root.  Add lines for /build and /app/build
    • VCS (menu) -> Commit Changes
      • this performs an initial commit, now we’re ready to add a remote repo & push
  • Create a repository at BitBucket, GitHub or wherever
  • get address of repository from remote git repository (top right corner HTTPS URL of repository overview page)
  • in Android Studio Terminal (bottom left in tools window), run the following command (replace username & repo-name with values from your repository)
    • git remote add origin
    • git push -u origin –all
  • Repository at remote location is now synced with local project.
  • For future commits, do Commit and Push to keep remote repository up-to-date

Clone Android Studio project from remote repository

  • From the remote repository web interface, Fork the repository with a new name
  • From Android Studio startup dialog choose:
    • Check out project from Version Control
  • Choose Git for BitBucket, Github for Github
  • In the Clone Repository dialog, Grab the forked repository URL from the web interface of Github or BitBucket (Overview page), paste into Vcs Repository URL field
  • Change Parent Directory if needed (usually not required)
  • Enter a directory name of your choosing
  • Click Clone button
  • Android Studio will pull files and clone the repository, ready for work
  • You’re done, add / edit / Commit + Push as usual
  • Likely you’ll get a dialog asking to add project-name.iml file.  I add it.