androidaccountaccountmanagerandroid-accountandroid-authenticator

Cannot add new custom account from different app with same authenticator


I have two apps that both use a same custom account type. Those two apps are completely independent and just share the account. When one of them starts, it checks for existing custom accounts and if no account was found, shows the sign-in page.

So I have created my AccountAuthenticator as a library project and reference it in both apps. According to this tutorial:

Let’s say you copied your authenticator’s code to 2 of your apps, thus sharing its logic, and altering the sign-in pages design on each app to fit the app it belongs to. In that case, the first installed app’s authenticator will be called for both apps when an auth-token will be requested. If you uninstall the first app, the second app’s authenticator will be called from now on (since it’s the only one now).

When I run one of the apps (no matter which app) and call addAccount it shows the sign-in page well. Then, when I run the second app and call addAccount nothing happens and sign-in page is not shown. After uninstalling the first app, the second app works correctly and shows sign-in page. So what is the problem and how can I fix that?

The implementation of addAccount:

mAccountManager.addAccount(accountType, authTokenType, null, null, this, new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> future) {
                try {
                    Bundle bnd = future.getResult();
                    showMessage("Account was created");
                } catch (Exception e) {
                    e.printStackTrace();
                    showMessage(e.getMessage());
                }
            }
        }, null);

Thanks in advance


Solution

  • I'm not sure this the correct answer, but I found that the problem is that the apps have different UIDs. So I use sharedUserId in both apps and the problem was solved:

     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.test"
        android:sharedUserId="test.account"
        android:versionCode="100"
        android:versionName="1.0" >