Solved - Android Studio and "Your Android SDK is out of date or is missing templates"

tl;dr
  • I had misconfigured the default Project settings in Android studio by adding the 2.1 SDK to it
  • Open source can be a nice thing, especially to figure out how your newbieness has contributed to a strange state of affairs
  • Frustrating error messages can provide a nice opportunity to dive into something new - at least in open source projects
The offending misconfiguration here: including the "Android 2.1 Platform"
will result in the "Your Android SDK is out of date" error



I had been playing with Android Studio, and continued to get an irritating error that prevented me from creating a new project:

A frustrating error message preventing me from creating new projects in Android Studio:
Your Android SDK is out of date or is missing templates.  
Please ensure you are using SDK version 22 or later.
You can configure your SDK via Configure | Project Defaults | Project Structure | SDKs
Note: This can be a confusing message because the "version 22" they are referring to is for the SDK Tools, which is a component for the SDK and not the SDK itself.  However, these tools seem to usually be included with the SDK itself as part of the Android Development Tools (as mentioned here), and I am guessing (at the moment) that the version of these tools is 22 starting at

I had followed all the various "this fixed it for me" tips out there, and it was still happening for me.  I definitely had the latest SDK specified in the list of SDKs for new Projects.  I had a hunch that somehow I had buggered something up somewhere that made me a bit unique.  So, I decided to take a look at the Android Studio IDE source, which you can grab via git like this:

git clone https://android.googlesource.com/platform/tools/adt/idea

There, at about line 75 in the file NewProjectWizard.java, was the relevant code that spits that message out:

protected void init() {
    if (!TemplateManager.templatesAreValid()) {
      String title = "SDK problem";
      String msg = "<html>Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.<br> />"</html><br />         + "You can configure your SDK via Configure | Project Defaults | Project Structure | SDKs
";      Messages.showErrorDialog(msg, title);
      throw new IllegalStateException(msg);
    }
    myWizardState = new NewProjectWizardState();
    ... more stuff 

Btw, this was originally added to Android Studio on May 9, 2013 in this commit.

Anyway, obviously, I needed to check what the method TemplateManager.templatesAreValid() did.  This is in the file TemplateManager.java, where we have the following:

/**
   * Do a sanity check to see if we have templates that look compatible, otherwise we get really strange problems. The existence
   * of a gradle wrapper in the templates directory is a good sign.
   * @return whether the templates pass the check or not
   */
  public static boolean templatesAreValid() {
    try { return  new File(getTemplateRootFolder(), "gradle/wrapper/gradlew").exists(); } catch (Exception e) {}
    return false;
  }

So, it's looking for a particular file to detect whether it's at least the right SDK version.  Seems a bit brittle but whatever. I checked everything I could find in /Applications/Android Studio.app, and found what seems to be the file it should be looking for and finding:

/Applications/Android Studio.app/sdk/tools/templates/gradle/wrapper/gradlew


So, where is it actually looking? And what is it using for getTemplateRootFolder()? Adding an extra debug statement or two and then rebuilding the Android Studio IDE itself seemed a fairly big task I wasn't ready to tackle.

Since I'm on a mac, which is unixy, I figured I had strace available that would allow me to watch all the system calls being made.  But on the mac, it turns out that there's also something called dtruss available, and I can attach to a running process and see all the system calls fly by.  So, from a commandline I ran the following

    sudo dtruss -a -p 12674

(where "12674" was the pid of the "studio" process when I run Android Studio).

A stream of system calls flew by, and then boolyah there it was:

stat64("/Users/bradflyon/Downloads/android-sdk-macosx-2/tools/templates\0"... = 0 0
stat64("/Users/bradflyon/Downloads/android-sdk-macosx-2/tools/templates/gradle/wrapper/gradlew\0", ...= -1 Err#21

This was a strange directory to be looking in.  An older SDK I had installed a while back.  It found the templates directory, but then could not find the gradlew file in a subdirectory.  Of course, it would have been nice in my case for the error message to tell me where it was looking.

Looking further into this, I noticed that even though the default was to use the 4.3 SDK for new projects, it was checking in the SDK directories I have set up for 2.1 and 3.0 (shown in the first screenshot above), because the GUI let me (unwisely) add them for default projects in addition to the newer ones.  Having things set up this way probably makes no sense at all.  Removing these from the list in the Project structure configuration window and restarting Android Studio resulted in New Projects now working.

whew.

And in hindsight, I definitely had the default Project settings misconfigured in Project Studio, resulting in fairly odd behavior of my own doing (which it smelled like all along).  However, maybe some of the details here will help other folks dealing with these kinds of things.  Yes, I was a bit impatient with the error message, but the nice thing about open source is that I was able to meander through the source to get more insight into the core issue, and learn a few more things along the way.  Android Studio source is in git and not svn - yay!  dtruss lets you attach to a running process on a mac and see all the system calls a process is making - yay!  All learned as part of solving a little interesting puzzle.








No comments:

Post a Comment

Popular Posts