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

Rap*_*itz 5 account android kotlin

我收到错误消息

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>
Run Code Online (Sandbox Code Playgroud)

验证器.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>
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

SwmsAuthenticatorService.kt

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

最后,在 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>
Run Code Online (Sandbox Code Playgroud)

尝试使用它

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

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)
Run Code Online (Sandbox Code Playgroud)

然后对于最后一行 ( 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)
...
Run Code Online (Sandbox Code Playgroud)

我完全不知道这里仍然存在什么问题。据我所知,帐户类型(net.roughdesign.swms)是通过<service>在清单中添加该部分来“注册”到我的应用程序的,但我认为android不接受“它是我的”。

这里还有什么问题吗?

Rap*_*itz 3

通用解决方案 - 如何调试

在android studio的logcat视图中,默认的过滤器设置是“仅显示选定的应用程序”。我确实收到了有关我的帐户服务无法注册的日志,包括原因。但是,使用默认过滤器设置时,它们不会显示!

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

但是,它会显示所有日志消息,因此您会看到大量日志消息。我必须滚动相当长一段时间才能看到该错误消息。我的建议是在获得所需内容后将过滤器重新设置为“仅显示选定的应用程序”。

我的具体问题

问题出在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" />
Run Code Online (Sandbox Code Playgroud)