Search code examples
androidflutterbuildapkaws-amplify

Amplify.Auth.fetchAuthSession() userPoolTokens is null when shrinking the apk


I am using the amplify libraries for flutter to connect to my aws backend. After a successful call to Amplify.Auth.signIn() I use Amplify.Auth.fetchAuthSession() to access the current credentials. Like that:

final result = await Amplify.Auth.fetchAuthSession(
  options: CognitoSessionOptions(getAWSCredentials: true),
);

if (!result.isSignedIn) return const None();

if (result is CognitoAuthSession) {
  return Some(result.userPoolTokens.idToken);
}

But when I build an apk with flutter using the following command my code breaks.

flutter build apk

Because result.userPoolTokens is suddenly null and I get a NoSuchMethodError. The getter 'idToken' was called on null. But if I disable shrinking and build the apk with the following command everything works again.

flutter build apk --no-shrink

So I think it has something to do with the shrinking and I would prefer to shrink my app because the shrinked apk is significantly smaller.

Would appreciate any help.


Solution

  • This is likely due to inadequate rules being applied for the R8 shrinker.

    The Amplify Android library did not vend any consumer-rules.pro until version 1.6.10 (see release notes), which was just released January 22nd, 2021.

    The latest version of Amplify Flutter is 0.0.2-dev.1, and it consumes version 1.6.8 of the Android library. I've raised an issue on their GitHub repository, to update to 1.6.10.

    In the meantime, you could try to apply these rules in your top-level application project.

    -keep class com.amazon.** { *; }
    -keep class com.amazonaws.** { *; }
    -keep class com.amplifyframework.** { *; }