Android SecurityException: uid xxxxx 无法显式添加帐户 [英] Android SecurityException: uid xxxxx cannot explicitly add accounts

查看:32
本文介绍了Android SecurityException: uid xxxxx 无法显式添加帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误信息

java.lang.SecurityException: uid 10178 无法显式添加以下类型的帐户:net.roughdesign.swms

即使是我可以创建的最基本的示例.它包括:

even with the most basic example I could create. It consists of:

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>

尝试使用它

我尝试创建一个帐户:

Trying to use it

I try to create an account with this:

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),我收到上述错误:

then for the last line (accountManager.addAccountExplicitly), I receive the aforementioned error:

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

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

I have absolutely no idea what could still be the issue here. As far as I'm aware, the account type (net.roughdesign.swms) is "registered" with my app by adding the <service> part in the manifest, but I think android doesn't accept that "it's mine".

这里还有什么问题?

推荐答案

一般解决方案 - 如何调试

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

General solution - how to debug this

In android studio's logcat view, the default filter setting is "Show only selected application". I did receive logs about my account service being unable to register, including the reason why. However, with the default filter setting, they were not shown!

因此,为了了解此处实际发生的情况,请将过滤器设置切换为无过滤器".这将显示有问题的错误消息.

So, in order to get an idea what is actually happening here, switch the filter setting to "No Filters". That will make the error message in question appear.

但是,它会显示所有日志消息,因此您将获得大量日志消息.我不得不滚动很多才能看到该错误消息.我的建议是在您获得所需内容后将过滤器转回仅显示选定的应用程序".

However, it shows all log messages, so you'll get you a ton of them. I had to scroll quite a bit to see that error message. My advice would be to turn the filter back to "Show only selected application" after you got what you need.

问题在于authenticator.xml.此文件不应包含 元素. 需要是顶级元素;完整的文件需要如下所示:

The problem was with authenticator.xml. This file should not have a <resources> element. <account-authenticator> needs to be a top level element; the complete file needs to look like this:

<?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" />

这篇关于Android SecurityException: uid xxxxx 无法显式添加帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆