Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app crashes on addAccountExplicitly(account, password, null);

Tags:

android

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

like image 919
Ove Stoerholt Avatar asked Oct 21 '15 22:10

Ove Stoerholt


3 Answers

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

like image 158
efor18 Avatar answered Oct 18 '22 19:10

efor18


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

like image 31
Ove Stoerholt Avatar answered Oct 18 '22 18:10

Ove Stoerholt


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

like image 21
ketankk Avatar answered Oct 18 '22 18:10

ketankk