AWS Amplify Flutter's auth signup function is not working when deployed via Android APK

1.9k Views Asked by At

I'm using the signup function of AWS' Amplify Auth in my Flutter app. The function is working fine when I run the code in Android Studio and test it on an emulator/phone. I'm able to add a new user to Cognito and verify the user.

But when I build the APK and try to run that APK on the same emulator/phone, the signup function is throwing some error in logcat and signup is not working as it was during debugging. I am not understanding why this error does not occur when I'm testing my code through Android Studio.

Here's the error:

2020-10-28 07:05:27.554 14035-14035/? E/MethodChannel#com.amazonaws.amplify/auth_cognito: Failed to handle method call
    e.d: null cannot be cast to non-null type java.lang.reflect.Method
        at com.amazonaws.amplify.amplify_auth_cognito.types.FlutterSignUpRequest.a(Unknown Source:203)
        at com.amazonaws.amplify.amplify_auth_cognito.types.FlutterSignUpRequest.<init>(Unknown Source:96)
        at com.amazonaws.amplify.amplify_auth_cognito.AuthCognito.i(Unknown Source:10)
        at com.amazonaws.amplify.amplify_auth_cognito.AuthCognito.a(Unknown Source:150)
        at d.a.b.a.j$a.a(Unknown Source:17)
        at io.flutter.embedding.engine.e.b.a(Unknown Source:57)
        at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(Unknown Source:4)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:336)
        at android.os.Looper.loop(Looper.java:174)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
2020-10-28 07:05:27.556 14035-14063/? E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The method 'forEach' was called on null.
    Receiver: null
    Tried calling: forEach(Closure: (dynamic, dynamic) => Set<Set<dynamic>>)
    #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51)
    #1      AmplifyAuthCognitoMethodChannel._throwError (package:amplify_auth_cognito/method_channel_auth_cognito.dart:330)
    #2      AmplifyAuthCognitoMethodChannel.signUp (package:amplify_auth_cognito/method_channel_auth_cognito.dart:40)
    <asynchronous suspension>
    #3      AmplifyAuthCognito.signUp (package:amplify_auth_cognito/amplify_auth_cognito.dart:45)
    <asynchronous suspension>
    #4      _State._registerUser (package:confab_app/SignUp_Screen.dart:36)
    <asynchronous suspension>

Here's my code:

import 'package:flutter/material.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_core/amplify_core.dart';

class SignUpPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _State();
}

class _State extends State<SignUpPage> {
  bool isSignUpComplete = false;
  TextEditingController nameController = TextEditingController();
  TextEditingController passwordController = TextEditingController();

  @override
  void dispose() {
    // Clean up the controller when the widget is removed from the
    // widget tree.
    nameController.dispose();
    passwordController.dispose();

    super.dispose();
  }

  void _registerUser() async
  {
    try {
      Map<String, dynamic> userAttributes = {
        "email": nameController.text,
        // additional attributes as needed
      };
      SignUpResult res = await Amplify.Auth.signUp(
          username: nameController.text.trim(),
          password: passwordController.text.trim(),
          options: CognitoSignUpOptions(
              userAttributes: userAttributes
          )
      );
      setState(() {
        isSignUpComplete = res.isSignUpComplete;
      });
      navigateToConfirmPage(context);

    } on AuthError catch (e) {
      print(e);
    }
  }
1

There are 1 best solutions below

0
On

The problem is with R8 removing some amplify's file. Include following two checks in proguard_rules.pro file

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

Please note : this error appears only on release apk build without no-shrink flag.