ProGuard IllegalArgumentException:不支持getAuthToken

时间:2018-10-12 19:27:17

标签: android kotlin proguard android-proguard android-authenticator

仅当启用ProGuard模糊处理时,才会出现以下错误。

2018-10-12 15:15:49.954 19177-19177/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 19177
    java.lang.IllegalArgumentException: getAuthToken not supported
        at android.accounts.AccountManager.convertErrorToException(AccountManager.java:2511)
        at android.accounts.AccountManager.-wrap0(Unknown Source:0)
        at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:2350)
        at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
        at android.os.Binder.execTransact(Binder.java:697)

我尝试添加ProGuard规则来保留AccountAuthenticator类。

-keep class com.example.app.AccountAuthenticator { *; }

我还通过在适当的函数中添加@Throws(NetworkErrorException::class)批注来尝试在this similar question中找到的解决方案。

class AccountAuthenticator(
        private val context: Context
) : AbstractAccountAuthenticator(context) {

    private val accountRepository = Injector.provideAccountRepository()

    @Throws(NetworkErrorException::class)
    override fun addAccount(
            response: AccountAuthenticatorResponse,
            accountType: String,
            authTokenType: String?,
            requiredFeatures: Array<out String>?,
            options: Bundle?
    ): Bundle {
        val intent = AccountSetupActivity.createIntent(context, response, accountType,
                authTokenType, requiredFeatures, options)
        return bundleIntent(intent)
    }

    @Throws(NetworkErrorException::class)
    override fun confirmCredentials(
            response: AccountAuthenticatorResponse, account: Account, options: Bundle?
    ): Bundle? = null

    @Throws(NetworkErrorException::class)
    override fun getAuthToken(
            response: AccountAuthenticatorResponse, account: Account,
            authTokenType: String, options: Bundle?
    ): Bundle? {
        if (authTokenType != AUTH_TOKEN_TYPE) {
            val result = Bundle()
            result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType")
            return result
        }

        var authToken = accountRepository.getAuthToken(account, authTokenType)

        if (authToken.isNullOrBlank()) {
            val status = accountRepository.refreshAuthentication(account).blockingGet()
            if (status == AccountRepository.STATUS_SUCCESS) {
                authToken = accountRepository.getAuthToken(account, authTokenType)
            }
        }

        if (!authToken.isNullOrBlank()) {
            Timber.i("auth token acquired; $authToken")
            val result = Bundle()
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name)
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type)
            result.putString(AccountManager.KEY_AUTHTOKEN, authToken)
            return result
        } else {
            Timber.i("auth token invalid, requesting credentials")
            val intent = AccountCredentialsActivity.createIntent(context, response, account)
            return bundleIntent(intent)
        }
    }

    override fun getAuthTokenLabel(authTokenType: String): String? = null

    override fun editProperties(
            response: AccountAuthenticatorResponse, accountType: String
    ): Bundle? = throw UnsupportedOperationException()

    @Throws(NetworkErrorException::class)
    override fun updateCredentials(
            response: AccountAuthenticatorResponse, account: Account,
            authTokenType: String?, options: Bundle?
    ): Bundle? = null

    @Throws(NetworkErrorException::class)
    override fun hasFeatures(
            response: AccountAuthenticatorResponse, account: Account,
            features: Array<out String>
    ): Bundle? {
        // This call is not expected to be used, so false (no) is always returned.
        val result = Bundle()
        result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)
        return result
    }

    private fun bundleIntent(intent: Intent): Bundle {
        val bundle = Bundle()
        bundle.putParcelable(AccountManager.KEY_INTENT, intent)
        return bundle
    }

}

什么可能导致此错误?

0 个答案:

没有答案