Android SecurityException: uid xxxxx 无法显式添加帐户

Android SecurityException: uid xxxxx cannot explicitly add accounts

我收到错误消息

java.lang.SecurityException: uid 10178 cannot explicitly add accounts of type: net.roughdesign.swms

即使是我能创建的最基本的示例。它包括:

账户配置

帐户类型 strings.xml

<string name="accounts__account_type">net.roughdesign.swms</string>

authenticator.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
            android:accountType="@string/accounts__account_type"
            android:icon="@drawable/ic_add_black_24dp"
            android:smallIcon="@drawable/ic_add_black_24dp"
            android:label="@string/global__authenticator_account_label"
            />

</resources>

SwmsAccountAuthenticator.kt

class SwmsAccountAuthenticator(val context: Context) : AbstractAccountAuthenticator(context) {

    override fun addAccount(
        response: AccountAuthenticatorResponse, accountType: String, authTokenType: String?,
        requiredFeatures: Array<out String>?, options: Bundle?
    ): Bundle? {
        return null
    }


    override fun confirmCredentials(response: AccountAuthenticatorResponse, account: Account, options: Bundle?)
            : Bundle? {
        return null
    }


    override fun editProperties(response: AccountAuthenticatorResponse?, accountType: String?): Bundle? {
        return null
    }


    override fun getAuthToken(
        response: AccountAuthenticatorResponse, accountInput: Account, authTokenType: String, options: Bundle?
    ): Bundle? {

        return null
    }


    override fun getAuthTokenLabel(authTokenType: String): String? {
        return null
    }


    override fun hasFeatures(response: AccountAuthenticatorResponse, account: Account, features: Array<out String>)
            : Bundle? {
        return null
    }


    override fun updateCredentials(
        response: AccountAuthenticatorResponse, account: Account, authTokenType: String?, options: Bundle?
    ): Bundle? {
        return null
    }
}

SwmsAuthenticatorService.kt

class SwmsAuthenticatorService : Service() {
    override fun onBind(intent: Intent?): IBinder? {
        val authenticator = SwmsAccountAuthenticator(this)
        return authenticator.iBinder
    }
}

最后,在AndroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application ... >

    <service
            android:name=".users.SwmsAuthenticatorService"
            android:exported="false">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>
        <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
    </service>

   ...
</application>

正在尝试使用它

我尝试用这个创建一个帐户:

val username = "myUserName"
val password = "myPassword"

val accountManager = AccountManager.get(this)
val accountType = getString(R.string.accounts__account_type)
val account = Account(username, accountType)

val accountAdded = accountManager.addAccountExplicitly(account, password, null)

然后对于最后一行 (accountManager.addAccountExplicitly),我收到上述错误:

   java.lang.SecurityException: uid 10178 cannot explicitly add accounts of type: net.roughdesign.swms
        at android.os.Parcel.readException(Parcel.java:2013)
        at android.os.Parcel.readException(Parcel.java:1959)
        at android.accounts.IAccountManager$Stub$Proxy.addAccountExplicitly(IAccountManager.java:1205)
        at android.accounts.AccountManager.addAccountExplicitly(AccountManager.java:878)
        at net.roughdesign.swms.swmsandroid.users.AuthenticateActivity.submit(AuthenticateActivity.kt:68)
...

我完全不知道这里还有什么问题。据我所知,通过在清单中添加 <service> 部分,我的应用程序的帐户类型 (net.roughdesign.swms) 是 "registered",但我认为 android 不是不接受 "it's mine".

这里还有什么问题?

一般解决方案 - 如何调试这个

在android工作室的logcat视图中,默认过滤器设置为"Show only selected application"。我确实 收到有关我的帐户服务无法注册的日志,包括原因。但是,使用默认过滤器设置,它们不会显示!

因此,为了了解此处实际发生的情况,请将过滤器设置切换为 "No Filters"。这将使有问题的错误消息出现。

但是,它会显示所有 条日志消息,因此您会得到一吨 条日志消息。我必须滚动很多才能看到该错误消息。我的建议是在获得所需内容后将过滤器调回 "Show only selected application"。

我的具体问题

问题出在 authenticator.xml。此文件不应包含 <resources> 元素。 <account-authenticator> 需要是顶级元素;完整的文件需要如下所示:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
        android:accountType="@string/accounts__account_type"
        android:icon="@drawable/ic_add_black_24dp"
        android:label="@string/global__authenticator_account_label"
        android:smallIcon="@drawable/ic_add_black_24dp" />