Android AccountManager and SyncAdapter … WTF?

Ever try to write an application that uses Android’s AccountManager and SyncAdapter to store user credentials?  Wow, what a pain in the arse.

I’ll save you all the ranting and just get down to it.

First, a bit of consolation: writing the code to get your application working with AccountManager is probably one of the more overly complicated things you can do on Android, and one of the most poorly documented features on any cell phone ever.

Google really dropped the ball on this one.  So, I’ll do my best to fill in the gaps.  But be warned, my writing here will be incomplete until I get all my questions answered.  Cause, well, the information I can get is incomplete.

Examples

Google’s SyncAdapter example exists, but there’s no Google documentation that shows you WHY the example was written the way it was.  As with all examples, you must be able to change things to get them to work in your application, so you can’t use them verbatim.  And that’s where the problems come in.

Final concept has a nice example, but again it only solve one type of problem.  And it doesn’t go over the things to avoid when working directly with the AccountManager.

Show AccountManager Output in LogCat

The first thing you’re going to need to do is get logcat to dump AccountManager messages.  You can do this by entering this on your command line:

$ adb shell setprop log.tag.AccountManagerService VERBOSE
$ adb shell setprop log.tag.Accounts VERBOSE
$ adb shell setprop log.tag.Account VERBOSE
$ adb shell setprop log.tag.PackageManager VERBOSE

(source: http://stackoverflow.com/questions/3774282/securityexception-caller-uid-xxxx-is-different-than-the-authenticators-uid )

You can set the log level of any tag with this pattern,  See http://developer.android.com/reference/android/util/Log.html#isLoggable(java.lang.String,%20int)

Notes From The Android Source & Distribution

The best way to get your head around AccountManager, or any part of Android for that matter, is to look at the source, and the distribution packages.  This section covers what I’ve found.

<android-sdk>/platforms/android-X/framework.aidl

This file contains a list of Android Interface Definition Language (AIDL) interfaces that are used within the Android Framework (the operating system components above the hardware).  Within this we find a few key definitions:

interface android.accounts.IAccountManager;
interface android.accounts.IAccountManagerResponse;
interface android.accounts.IAccountAuthenticator;
interface android.accounts.IAccountAuthenticatorResponse;

This is a big clue that IDL bindings are used when talking to the account manager. We should be able to look at the code that implements these interfaces and see what they do.

Attach Android Source to Your Project

This is actually fairly straightforward once you know what you’re looking for.  You simply create a directory, initialize it with the repo command and synch up with the source.  Of course, we’re going to want to synch up with the version of the Android platform that your project is using. Take a look at these links:

Downloading The Android Source Tree – shows you how to get source code onto your machine.  The source lives at kernel.org here.

A List of Android Version Numbers and Names – you will need these to check out the code properly

Here is the command I used to fetch the source code for Android 2.1 (eclaire):


$ cd Android-2.1-eclair/
$ ls -als
$ pwd
$ repo init -u git://android.git.kernel.org/platform/manifest.git -b eclair
$ repo sync

…. lots of output follows ….

Checking out files: 100% (9063/9063), done.out files:   1% (129/9063)
Checking out files: 100% (8503/8503), done.out files:   0% (12/8503)
Checking out files: 100% (429/429), done.g out files:  29% (126/429)
Checking out files: 100% (599/599), done.ng out files:  42% (257/599)
Checking out files: 100% (1959/1959), done. out files:   2% (53/1959)
Checking out files: 100% (1233/1233), done.
Checking out files: 100% (620/620), done.ng out files:  29% (181/620)
Checking out files: 100% (920/920), done.ng out files:  12% (114/920)
Syncing work tree: 100% (162/162), done.

$

Once you’ve checked out the source, take a look at your project in Eclipse.  Associate the Android .jar file with

More to Come …

Advertisements

And now it's your turn ... comment here or on Twitter (@Androider)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s