Chris Risner . Com

Changing the SDK Path with Android Studio

Posted on: 5/15/2013 9:16:00 PM by

If you’ve been watching the news out of Google I/O today, you’ll have heard about Android Studio.  Android Studio is a new IDE based off of NetBean’s IntelliJ IDE for Android development.  This is super exciting to many Android developers because many people are using Eclipse and Eclipse is generally pretty terrible.  Plus, an IDE made by the people that are making the Android OS / SDK is pretty exciting.  As soon as they made Android Studio available to download, I went and grabbed it and installed it.  One of the first things I noticed once I installed it was that when I went to create a project, not all of the SDK versions that I already had installed were showing up as SDK choices.  This made me realize that at no point in the install process was I asked where my install of the Android SDK was at.  The reason for that is that Android Studio installs it’s own version of the SDK in it’s own app folder.  On OS X, if you navigate to /Applications/Android Studio.app/sdk, you’ll see the Android SDK installed by Android Studio.  This might be fine if I was ready to commit entirely to Android Studio, but with it being at v0.1, I’m not quite ready to make that jump.  What I really wanted to do was point Android Studio at the same version of the SDK I was using elsewhere.

Changing the SDK

The first place I looked for changing this was in the Preferences section.  Unfortunately, I couldn’t find anything here.  I looked around in some of the other menus and didn’t find anything obvious (Tools has an Android submenu, but it’s just for launching the AVD Manager, SDK Manager, Monitor, or enabling / disabling ADB Integration).  Fortunately I kept looking and stumbled upon the File –> Project Structure menu.  After opening this, I found something that referenced the SDK:

Project Structure

Clicking the Edit button Takes you to the SDKs area on the left which includes the ability to change the Android SDK Home Path:

Android SDK Home Path

Once we change this, we’re set to use our previously installed SDK.  But wait, there’s more!

SDK Updates

Before you do this, you probably want to make sure you have the latest updates to the SDK.  Open the SDK manager (the original SDK install) which you should be able to open by running SDK_PATH/tools/android.  Once you’ve run this, make sure you don’t have any updates available for Android SDK Tools, Android SDK Platform-Tools, and Android SDK Build-tools.  Android Studio uses a new build engine so if you try to change your SDK home path and create a new project without getting this update, it won’t work!

Good luck and enjoy playing with Android Studio.  The team has said they’re hoping to do weekly updates and Android Studio is set up to check for updates on it’s own so hopefully any issues we run into should get fixed fast.

Categories: Android
Bookmark and Share
First Article

CountDownLatchs and Android

Posted on: 5/13/2013 8:07:00 PM by

A few weeks ago I was working on an Android sample connecting to Windows Azure Mobile Services to do different kinds of authentication.  One of the features of that sample was the ability to ask the user to login if they made a request that required they be authenticated but the token they were using had expired.  After they logged in again, the previous request would then be retried.  Unfortunately, at the time it seemed like I would need to add something custom to the Mobile Services SDK for Android in order to track the previous request and then retry it.  This resulted in the addition of quite a bit of code.  After posting that sample and walking through the changes I made with the SDK team, we found a much easier way to accomplish the task without making any changes to the SDK:  using a CountDownLatch

What is a CountDownLatch?

A CountDownLatch is a synchronization aid that enables you to block a thread until the countdown reaches zero.  In other words, you can pause execution of one thread until a second thread says “ok you can finish now”.  How does this help us with the scenario above?  Well, due to the fact that network requests can’t (and shouldn’t) be made on the main (UI) thread, you have to perform these requests on a separate thread.  When we make the first request (with the expired token) the response is handled on a background thread.  From this thread, if we want to make the user log in again, we need to open the login dialog, but we need to jump back to the UI thread to do that.  When we get the response from the login dialog that they’ve successfully authenticated, we then need to update our request with the new token and rerun that request but on a background thread!  That’s a lot of jumping back and forth so I’ve made this diagram in an attempt to simplify things:

Retry request diagram

So as you can see, this is a bit complicated.  My previous solution was to keep a copy of the request that was made and, if we had to re-login the user, to alter that copied request and then reissue it.  At the time I thought this was necessary because Mobile Services requests are wrapped up in the AsyncTask class and you can’t have the same task run twice.  However, I was missing the fact that I could use the same service filter to reissue the request.  That might not make sense right now but I’ll show you some code and it will clear things up for you quite a bit. 

The Code

I’ve taken the code from the previous sample and removed the code not necessary for explaining how this works:

This ServiceFilter’s handleRequest method will be called prior to any request being made against my Mobile Service.  It uses the nextServiceFilterCallback to call onNext which will process that request and call the onResponse method when it gets a result back.  Inside that onResponse, we check for a 401 and if so, we create our CountDownLatch with an initial value of 1 (since we’re only waiting on one operation to complete).  We then use the current activity to call runOnUiThread and ask the user to login again.  When the onCompleted method of the login is called, if there wasn’t an exception, we can update our request object (left out for brevity) and call latch.coundDown().  This call basically says “if anyone called await() on this CountDownLatch, you can proceed now!”  Right beneath the block we have to run code on the UI thread, the onResponse method continues with a call to latch.await().  So basically, we’re running some code on the UI thread and in the background thread we’re saying “let’s wait for that to be done”.  Once the UI thread is done, we use the same nextServiceFilterCallback onNext method to process the request.  Now we’ve got the same process we had before working but without needing to track the previous request, request type, table, etc.  Much easier (though a bit to understand).

Conclusion

Using the CountDownLatch gives you an easy way to block a thread and wait for some action to be performed.  This is very useful if you’re doing background processing of any sort.  In this specific situation, it saves us from needing to keep track of quite a few variables so we can reprocess a request.  I’ve updated the sample code for the authentication demo so it now includes this code instead of the old version that required SDK changes and you can read more about it here.

Bookmark and Share
First Article