velis
velis

Reputation: 10025

Registering a Google account to Android device programmatically

I've been looking for a solution to this problem for a while (days, not minutes), but it eludes me quite effectively.

Please note that this is NOT a question about starting up the registration procedure. This must happen automatically without any user interaction.

I would like to add a Google account to my custom device (1000's of them). The account will mostly be used to activate Google Play store on the device so that the app can update when newer versions are available.

My existing code (the shortest snippet of those I tried):

AccountManager mgr = AccountManager.get(this);
Account acc = new Account("[email protected]", "com.google");
mgr.addAccountExplicitly(acc, "password", new Bundle()));

naturally yields a

java.lang.SecurityException: caller uid 10047 is different than the authenticator's uid

So how would I go about actually achieving this? My device is rooted so that's not an obstacle if it's the only way.

Upvotes: 5

Views: 2552

Answers (2)

velis
velis

Reputation: 10025

Warning: this solution doesn't work well. See comments for explanation.

Well, as it turns out, this is not something easily solved. I ended up registering one device, then pulled the users file from it. Location of users file : /data/system/users/0/accounts.db (if there are multiple user profiles on the device, the last directory may differ according to profile in question).

I stored this file into my app's assets (gzipped, make sure the extension is not something.gz because that gets lost during packaging - didn't bother checking out why).

First I check if my user already exists:

AccountManager mgr = AccountManager.get(this);
for (Account acc: mgr.getAccountsByType("com.google")) {
  if (acc.name.equalsIgnoreCase("[email protected]"))
    return;
}

If it does, I just skip the step. Otherwise I unpack the users file and overwrite existing one (using su). I then also do a reboot to make sure changes are registered.

Upvotes: 0

Rajesh N
Rajesh N

Reputation: 6683

It is not possible to add/create a Google account using addAccountExplicitly(). You can only add accounts for your own services. even your device is rooted because it will rejected by Google web server. For more detail check this link

Upvotes: 0

Related Questions