Aws-sdk-android: [S3] Issue with Proguard obfuscating TransferNetworkConnectionType enum that are serialized using Gson

Created on 15 Feb 2019  ·  7Comments  ·  Source: aws-amplify/aws-sdk-android

Describe the bug
Fails to upload file using transferUtility if minifyEnabled = true
you get the following error
Caused by: java.lang.AssertionError: java.lang.NoSuchFieldException: ANY at com.google.gson.a.a.n$a.<init>(SourceFile:791) at com.google.gson.a.a.n$24.a(SourceFile:817) at com.google.gson.f.a(SourceFile:423) at com.google.gson.a.a.i.a(SourceFile:115) at com.google.gson.a.a.i.a(SourceFile:164) at com.google.gson.a.a.i.a(SourceFile:100) at com.google.gson.f.a(SourceFile:423) at com.google.gson.f.a(SourceFile:661) at com.google.gson.f.a(SourceFile:648) at com.google.gson.f.a(SourceFile:603) at com.google.gson.f.a(SourceFile:583) at com.amazonaws.mobileconnectors.s3.transferutility.TransferDBUtil.b(SourceFile:806) at com.amazonaws.mobileconnectors.s3.transferutility.TransferDBUtil.a(SourceFile:139) at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.a(SourceFile:549) at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.a(SourceFile:508) at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.a(SourceFile:477) at com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility.b(SourceFile:421)
This is a known issue with Gson. The way I found to fix it is adding the next line.

-keepclassmembers enum com.amazonaws.** { *; }

I reproduced the issue in sdk version 2.12.0

Which AWS service(s) are affected?
s3

Environment Information (please complete the following information):

  • AWS Android SDK Version: [e.g. 2.6.25]
Needs Info from Requester S3 Usage Question closing-soon-if-no-response

Most helpful comment

@GerardoRobledo

-keepclassmembers enum com.amazonaws.** { *; }

This helps me. 👍
Thank you !

All 7 comments

@GerardoRobledo Sorry for the inconvenience caused. This is my understanding of the issue.

TransferUtility added a new feature in 2.11.0 to specify the type of network connection. TransferNetworkConnectionType is an enum which is serialized to store in the SQLite Database.
We use Gson - "com.google.code.gson:gson:2.2.4" which has a bug while serializing enum when with proguard enabled. This is because Proguard would modifies the enums’ constant names and Gson would throw NoSuchFieldException when deserializing enum constants from JSON data.

            TransferUtilityOptions transferUtilityOptions = new TransferUtilityOptions(
                    2,
                    TransferNetworkConnectionType.WIFI);
            TransferUtility sTransferUtility = TransferUtility.builder()
                    .context(context)
                    .s3Client(getS3Client(context))
                    .awsConfiguration(new AWSConfiguration(context))
                    .transferUtilityOptions(transferUtilityOptions)
                    .build();

Can you explain the steps you took to reproduce the issue? I can spot an issue in the code with the way Enums are being serialized with Gson.

Possible fix:

Add @SerializedName for all the enum constants in TransferNetworkConnectionType and Gson would use serialized name for JSON form.

@GerardoRobledo

-keepclassmembers enum com.amazonaws.** { *; }

This helps me. 👍
Thank you !

@GerardoRobledo - can confirm that @interkenny's suggestion fixes your issue?

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

-keepclassmembers enum com.amazonaws.** { *; }

This line working for me.

I added this to my proguard-rules.pro to handle all proguard + aws client related issues.
checkout https://github.com/aws-amplify/aws-sdk-android/blob/master/Proguard.md for more information.

# Class names are needed in reflection
-keepnames class com.amazonaws.**
-keepnames class com.amazon.**
-keepclassmembers enum com.amazonaws.** { *; }
# Request handlers defined in request.handlers
-keep class com.amazonaws.services.**.*Handler
# The following are referenced but aren't required to run
-dontwarn com.fasterxml.jackson.**
-dontwarn org.apache.commons.logging.**
# Android 6.0 release removes support for the Apache HTTP client
-dontwarn org.apache.http.**
# The SDK has several references of Apache HTTP client
-dontwarn com.amazonaws.http.**
-dontwarn com.amazonaws.metrics.**

@wycliff Amazon changed their branch from master -> main. New link:

https://github.com/aws-amplify/aws-sdk-android/blob/main/Proguard.md for more information.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slomin picture slomin  ·  4Comments

zgao67 picture zgao67  ·  4Comments

logo17 picture logo17  ·  4Comments

shabana0508 picture shabana0508  ·  3Comments

nsadiq-radpits picture nsadiq-radpits  ·  3Comments