After a successful basic authentication I want to add an account for later use. When I tried to create this account using the following code:
AccountManager
You are using "com.example"
as unique identifier for your app,please check if it's same in "authenticator.xml"
It may cause by an ACCOUNT_TYPE mismatch. check ACCOUNT_TYPE in Class and ACCOUNT_TYPE in authenticator.xml must match
private static final String ACCOUNT_TYPE = "com.someonew.syncaccount";
authenticator.xml
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.someonew.syncaccount"
android:icon="@mipmap/ic_launcher_round"
android:smallIcon="@mipmap/ic_launcher_round"
android:accountPreferences="@xml/syncsettings"
android:label="@string/app_name"/>
In my case i was having Package name contains capital characters, so service was not getting registered in Android Manifest and hence issue was occurred.
android:name="com.SyncServices.SyncService"
changed to
android:name="com.syncservices.SyncService"
In my case it was happening that I had another app installed on the device with the same account name and type, but with different signing cert that the one I was trying to install.
So it was crashing the app.
Checking the android doc for the method addAccountExplicity, it says:
This method requires the caller to have a signature match with the authenticator that owns the specified account.
That was my problem
There's not a one golden response to your 3rd question.
If all apps use the same name, you may consider a service, just remember to restrict it to your own apps.
However, the user may get confused about being signed in just after the app is installed thus I would recommend to allow user to sign in in each separate app. You could generate multiple separate private keys for the the same app in Google Developer Console and thus get the same user LOCAL_ID in every app.
1) Reason for crash was because the following snippet was missing in AndroidManifest.xml.
<service android:name="com.example.accounts.GenericAccountService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator" />
</service>
2) It is absolutely possible, even though best practice example is still missing.
3) No idea. Yet...