aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 549 forks source link

SDK crashes on Android Gradle Plugin 8.0 #3302

Closed atlantis0 closed 1 year ago

atlantis0 commented 1 year ago

Describe the bug App crashes when using AWS core SDK with AGP 8.0. AGP 8.0 was released in April 2023 with breaking changes as described here, https://developer.android.com/build/releases/gradle-plugin#default-changes

See crash stack trace

Caused by: java.lang.IllegalStateException: Cannot create an instance of com.amazonaws.auth.AWS4Signer
    at com.amazonaws.auth.SignerFactory.a(Unknown Source:77)
    at com.amazonaws.auth.SignerFactory.d(Unknown Source:12)
    at com.amazonaws.auth.SignerFactory.b(Unknown Source:0)
    at com.amazonaws.AmazonWebServiceClient.l(Unknown Source:8)
    at com.amazonaws.AmazonWebServiceClient.m(Unknown Source:14)
    at com.amazonaws.AmazonWebServiceClient.B(Unknown Source:7)
    at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.F(Unknown Source:127)
    at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.<init>(SourceFile:3)
    at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.<init>(SourceFile:2)
    at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.<init>(SourceFile:1)
    at com.amazonaws.auth.CognitoCredentialsProvider.d(Unknown Source:7)
    at com.amazonaws.auth.CognitoCredentialsProvider.<init>(SourceFile:2)
    at com.amazonaws.auth.CognitoCredentialsProvider.<init>(SourceFile:1)
    at com.amazonaws.auth.CognitoCachingCredentialsProvider.<init>(Unknown Source:6)
    at h1.a.<init>(Unknown Source:20)
    at com.yes.upload.HomeActivity.onCreate(Unknown Source:22)
    at android.app.Activity.performCreate(Activity.java:7815)
    at android.app.Activity.performCreate(Activity.java:7804)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1318)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3349)
    ... 11 more
Caused by: java.lang.InstantiationException: java.lang.Class<com.amazonaws.auth.AWS4Signer> cannot be instantiated
    at java.lang.Class.newInstance(Native Method)
    at com.amazonaws.auth.SignerFactory.a(Unknown Source:12)
    ... 30 more

To Reproduce

  1. Use AGP 8.0.0
  2. Build your app in release mode
  3. See sample code snippet below that caused the crash
    CognitoCachingCredentialsProvider mCredentialsProvider = new CognitoCachingCredentialsProvider(context, accountId, identityPool, unAuthRole, authRole, regions1);
    AmazonS3Client s3Client = new AmazonS3Client(mCredentialsProvider, Region.getRegion(regions1.getName()));
    TransferUtility transferUtility = TransferUtility.builder().s3Client(this.s3Client).context(context).build();

Here is link to the full project, https://drive.google.com/file/d/1snapMz2QLcmCnhZbBrcVnx5zHrBaih3k/view?usp=sharing

Which AWS service(s) are affected? AWS Auth with Cognito

Expected behavior App should not crash in release mode when using AWS auth Cognito and AGP 8+

Environment Information (please complete the following information):

Additional context AGP 8.0 sets android.enableR8.fullMode=true as its default value. fullMode aggressively optimizes classes that use reflection. The documentation explicitly states, "Classes that are only instantiated using reflection should be kept explicitly with a -keep rule.". See here, https://r8.googlesource.com/r8/+/refs/heads/master/compatibility-faq.md#r8-full-mode

Since, com.amazonaws.auth.AWS4Signer is used reflectively, the appropriate proguard rule should be created for it. E.g like this

-keep class com.amazonaws.auth.AWS4Signer { *; }

Work Around Two options

  1. Add the above rule to the app's proguard file or
  2. Disable r8 rull mode. Add the following line to your gradle.properties file android.enableR8.fullMode=false

Edit - Formatting

mattcreaser commented 1 year ago

Hey @atlantis0 thank you for the excellent bug report. We'll work on getting out an updated consumer proguard file out, but in the meantime anyone encountering this should use the first listed workaround, add this line to your proguard file:

-keep class com.amazonaws.auth.AWS4Signer { *; }
eeatonaws commented 1 year ago

A fix has been released in version 2.71.0. Please let us know if you have any additional questions.