Android 和 AWS Cognito - 无法使用 proguard 解组错误响应

Posted

技术标签:

【中文标题】Android 和 AWS Cognito - 无法使用 proguard 解组错误响应【英文标题】:Android and AWS Cognito - Unable to unmarshall error response with proguard 【发布时间】:2017-10-27 16:30:57 【问题描述】:

我正在使用适用于 android 的 AWS cognito SDK,版本 2.4.3。它在调试版本上工作得很好,但在发布版本上,如果出现错误,我无法得到关于异常的正确描述。相反,我得到了这个

com.amazonaws.AmazonClientException: Unable to unmarshall error response (<init> [class java.lang.String]). Response Code: 400, Response Text: Bad Request
    at com.amazonaws.http.AmazonHttpClient.a(Unknown Source)
    at com.amazonaws.http.AmazonHttpClient.b(Unknown Source)
    at com.amazonaws.http.AmazonHttpClient.a(Unknown Source)
    at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source)
    at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source)
    at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source)
    at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source)
    at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NoSuchMethodException: <init> [class java.lang.String]
    at java.lang.Class.getConstructor(Class.java:531)
    at java.lang.Class.getConstructor(Class.java:495)
    at com.amazonaws.transform.AbstractErrorUnmarshaller.a(Unknown Source)
    at com.amazonaws.transform.JsonErrorUnmarshaller.b(Unknown Source)
    at com.amazonaws.services.cognitoidentityprovider.model.transform.InvalidParameterExceptionUnmarshaller.b(Unknown Source)
    at com.amazonaws.http.JsonErrorResponseHandler.a(Unknown Source)
    at com.amazonaws.http.JsonErrorResponseHandler.b(Unknown Source)
    at com.amazonaws.http.JsonErrorResponseHandler.a(Unknown Source)
    at com.amazonaws.http.AmazonHttpClient.a(Unknown Source) 
    at com.amazonaws.http.AmazonHttpClient.b(Unknown Source) 
    at com.amazonaws.http.AmazonHttpClient.a(Unknown Source) 
    at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source) 
    at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.a(Unknown Source) 
    at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source) 
    at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool.a(Unknown Source) 
    at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserPool$1.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:818) 

在调试版本中,我收到一个异常,详细说明了错误。但是,如果所有字段都写得很好,那么在发布和调试用户注册都会成功。我在发布时使用 proguard,我正在应用来自 this file 的规则

任何有关此问题的帮助将不胜感激。

【问题讨论】:

【参考方案1】:

尝试不同的 Proguard 设置后,以下是 AWS SDK 存储库中建议的最低配置,以使其正常工作

-keep class com.amazonaws.services.cognitoidentityprovider.**  *; 

必须添加到app/proguard-rules.pro

【讨论】:

【参考方案2】:

AWS 核心库中的类也可能发生解组错误,而不仅仅是com.amazonaws.services.cognitoidentityprovider。此规则将防止所有 AWS lib 异常发生这种崩溃:

-keep class * extends com.amazonaws.AmazonClientException  *; 

【讨论】:

以上是关于Android 和 AWS Cognito - 无法使用 proguard 解组错误响应的主要内容,如果未能解决你的问题,请参考以下文章

AWS API Gateway Cognito 授权模拟

Android 和 AWS Cognito - 无法使用 proguard 解组错误响应

AWS + Serverless - 如何获取 cognito 用户池生成的密钥

aws cognito用户获取id令牌android

AWS cognito 新密码延续 - Android

AWS Lambda、API 网关和 Cognito:如何在 lambda 函数中获取身份对象?