44

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 accountManager = AccountManager.get(getBaseContext());
final Account basicAccount = new Account(mEmail, "com.example");
accountManager.addAccountExplicitly(basicAccount, mPassword, null);

When addAccountExplicitly(...) is called the app crashes with the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.example, PID: 19094
E/AndroidRuntime: java.lang.SecurityException: uid 10107 cannot explicitly add accounts of type: com.example
E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1552)
E/AndroidRuntime:     at android.accounts.IAccountManager$Stub$Proxy.addAccountExplicitly(IAccountManager.java:890)
E/AndroidRuntime:     at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:712)
E/AndroidRuntime:     at com.example.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:244)
E/AndroidRuntime:     at com.example.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:209)
E/AndroidRuntime:     at android.os.AsyncTask.finish(AsyncTask.java:651)
E/AndroidRuntime:     at android.os.AsyncTask.-wrap1(AsyncTask.java)
E/AndroidRuntime:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/ActivityManager:   Force finishing activity com.example/.LoginActivity

Question:

  1. I am certain that my AccountType are the same as specified in my authenticator.xml. Why does my code crash?
  2. Is it even possible to use AccountManager and Account with basic authentication? I have not been able to find a good example for this (they all use tokens...)
  3. My idea is to use this account for several applications. Is using a service for authentication (with intents) considered a best practice? Any good tutorials on this?

Thanks, Ove

Pawan
  • 533
  • 1
  • 7
  • 31
Ove Stoerholt
  • 3,843
  • 4
  • 25
  • 33

6 Answers6

39

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

mattfred
  • 2,689
  • 1
  • 22
  • 38
efor18
  • 1,256
  • 10
  • 5
  • How can we know which app is clashing with ours easily or programmatically? This will be helpful in the event this crash only happens on user side where we can't replicate it on our end. – Elye Jul 17 '20 at 04:38
34

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...

Ove Stoerholt
  • 3,843
  • 4
  • 25
  • 33
  • See an updated piece of this code at Android Developer's: https://developer.android.com/training/id-auth/custom_auth.html#TaskFour – Zon Feb 12 '18 at 08:42
21

You are using "com.example" as unique identifier for your app,please check if it's same in "authenticator.xml"

ketankk
  • 2,578
  • 1
  • 29
  • 27
  • 2
    Adding to sultan answer please check if android:accountType="pkgname" and Account account = new Account(context.getString(R.string.app_name), context.getPackageName()); accountManager.addAccountExplicitly(account, "123456", bundle); Logger.debug(TAG, "Account created successfully"); are same – silentsudo Nov 29 '16 at 12:46
  • can you help me : https://stackoverflow.com/questions/60791842/cannot-explicitly-add-accounts-of-type-com-example-android – sajjad Yosefi Mar 21 '20 at 20:30
12

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"/>
Farshid roohi
  • 722
  • 9
  • 23
1

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.

Paweł Szczur
  • 5,484
  • 3
  • 29
  • 32
0

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"
sDev
  • 1,463
  • 9
  • 17
  • Hi, was the issue because a) your sync service was in the package with lower case names, and the Manifest incorrectly used capitals, or b) the sync service was in a package with capital letters, but this was for some reason invalid, and you had to change it and the manifest to use all lower case? – Nicholas Jun 10 '22 at 11:24