Flutter Firebase 身份验证与 Google 应用程序崩溃

Posted

技术标签:

【中文标题】Flutter Firebase 身份验证与 Google 应用程序崩溃【英文标题】:Flutter Firebase Authentication With Google App Crash 【发布时间】:2019-12-20 12:24:51 【问题描述】:

我正在使用 Flutter 制作“使用 Google 登录”按钮,但是当我单击该按钮时,应用程序崩溃并退出,尽管我确信 Firebase 已与应用程序正确集成,并且 SHA1 正确且应用程序可以正常工作与 Firestore 相得益彰

这里是代码和错误。

错误:

I/art     (13904): Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.auth.api.signin.internal.SignInHubActivity>
I/art     (13904): Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.auth.api.signin.internal.SignInHubActivity>
E/flutter (13904): [ERROR:flutter/shell/platform/android/platform_view_android_jni.cc(40)] java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/auth/api/signin/internal/SignInHubActivity;
E/flutter (13904):  at com.google.android.gms.auth.api.signin.internal.zzh.zzc(Unknown Source)
E/flutter (13904):  at com.google.android.gms.auth.api.signin.GoogleSignInClient.getSignInIntent(Unknown Source)
E/flutter (13904):  at io.flutter.plugins.googlesignin.GoogleSignInPlugin$Delegate.signIn(GoogleSignInPlugin.java:291)
E/flutter (13904):  at io.flutter.plugins.googlesignin.GoogleSignInPlugin.onMethodCall(GoogleSignInPlugin.java:77)
E/flutter (13904):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/flutter (13904):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/flutter (13904):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
E/flutter (13904):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (13904):  at android.os.MessageQueue.next(MessageQueue.java:331)
E/flutter (13904):  at android.os.Looper.loop(Looper.java:218)
E/flutter (13904):  at android.app.ActivityThread.main(ActivityThread.java:5526)
E/flutter (13904):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (13904):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
E/flutter (13904):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/flutter (13904): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.auth.api.signin.internal.SignInHubActivity" on path: DexPathList[[zip file "/data/app/com.example.tester-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.tester-1/lib/arm, /data/app/com.example.tester-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
E/flutter (13904):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/flutter (13904):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/flutter (13904):  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/flutter (13904):  ... 14 more
E/flutter (13904):  Suppressed: java.lang.NoClassDefFoundError: com.google.android.gms.auth.api.signin.internal.SignInHubActivity
E/flutter (13904):      at dalvik.system.DexFile.defineClassNative(Native Method)
E/flutter (13904):      at dalvik.system.DexFile.defineClass(DexFile.java:226)
E/flutter (13904):      at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
E/flutter (13904):      at dalvik.system.DexPathList.findClass(DexPathList.java:338)
E/flutter (13904):      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
E/flutter (13904):      ... 16 more
E/flutter (13904):  Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.auth.api.signin.internal.SignInHubActivity
E/flutter (13904):      at java.lang.Class.classForName(Native Method)
E/flutter (13904):      at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/flutter (13904):      at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/flutter (13904):      at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/flutter (13904):      ... 15 more
E/flutter (13904):  Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
E/flutter (13904):
F/flutter (13904): [FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(77)] Check failed: CheckException(env).
F/libc    (13904): Fatal signal 6 (SIGABRT), code -6 in tid 13904 (.example.tester)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
UUID: 82d8c901-de47-4729-a4c6-a013707000ca
Build fingerprint: 'Sony/D6503/D6503:6.0.1/23.5.A.1.291/2769308465:user/release-keys'
Revision: '0'
ABI: 'arm'
pid: 13904, tid: 13904, name: .example.tester  >>> com.example.tester <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: '[FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(77)] Check failed: CheckException(env).
'
    r0 00000000  r1 00003650  r2 00000006  r3 b6fe9b7c
    r4 b6fe9b84  r5 b6fe9b34  r6 0000000b  r7 0000010c
    r8 bee23e9c  r9 00000000  sl 00000001  fp b6f7cc11
    ip 00000006  sp bee23d78  lr b6d4ecb1  pc b6d510a0  cpsr 400f0010
backtrace:
    #00 pc 000420a0  /system/lib/libc.so (tgkill+12)
    #01 pc 0003fcad  /system/lib/libc.so (pthread_kill+32)
    #02 pc 0001c463  /system/lib/libc.so (raise+10)
    #03 pc 00019615  /system/lib/libc.so (__libc_android_abort+34)
    #04 pc 00017568  /system/lib/libc.so (abort+4)
    #05 pc 00b0cc63  /data/app/com.example.tester-1/lib/arm/libflutter.so (offset 0xafe000)
    #06 pc 00b03411  /data/app/com.example.tester-1/lib/arm/libflutter.so (offset 0xafe000)
    #07 pc 00b02107  /data/app/com.example.tester-1/lib/arm/libflutter.so (offset 0xafe000)
    #08 pc 00b338ef  /data/app/com.example.tester-1/lib/arm/libflutter.so (offset 0xafe000)
    #09 pc 00b0d3c5  /data/app/com.example.tester-1/lib/arm/libflutter.so (offset 0xafe000)
    #10 pc 00b107ab  /data/app/com.example.tester-1/lib/arm/libflutter.so (offset 0xafe000)
    #11 pc 00012e03  /system/lib/libutils.so (_ZN7android6Looper9pollInnerEi+530)
    #12 pc 00012ed3  /system/lib/libutils.so (_ZN7android6Looper8pollOnceEiPiS1_PPv+130)
    #13 pc 00082a59  /system/lib/libandroid_runtime.so (_ZN7android18NativeMessageQueue8pollOnceEP7_JNIEnvP8_jobjecti+22)
    #14 pc 72d4655d  /data/dalvik-cache/arm/system@framework@boot.oat (offset 0x1f66000)
Tombstone written to: /data/tombstones/tombstone_08

代码都在 main.dart 中:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:provider/provider.dart';
import 'package:tester/locator.dart';
import 'package:tester/ui/views/HomeView.dart';

import 'core/viewmodels/crudModel.dart';

main(List<String> args) 
  setupLocator();
  runApp(TestApp());


class TestApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle(statusBarColor: Colors.green),
    );
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          builder: (_) => locator<CRUDModel>(),
        )
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          accentColor: Colors.green,
          primaryColor: Colors.green,
        ),
        home: GoogleLoginView(),
      ),
    );
  


class GoogleLoginView extends StatelessWidget 
  final GoogleSignIn _googleSignInObject = GoogleSignIn();

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Google Login'),
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.green,
          splashColor: Colors.greenAccent,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20),
          ),
          onPressed: _googleSignIn,
          child: Text('Google Sign In'),
        ),
      ),
    );
  

  Future<void> _googleSignIn() async 
    GoogleSignInAccount _googleSignInAccount =
        await _googleSignInObject.signIn().catchError((e) => print(e));
    GoogleSignInAuthentication _googleSignInAuthentication =
        await _googleSignInAccount.authentication.catchError((e) => print(e));
    await FirebaseAuth.instance.signInWithCredential(
      GoogleAuthProvider.getCredential(
        idToken: _googleSignInAuthentication.idToken,
        accessToken: _googleSignInAuthentication.accessToken,
      ),
    ).catchError((e) => print(e));
  

pubspec.yaml:

name: tester
description: A new Flutter project.

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In ios, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  provider: ^3.0.0+1
  get_it: ^1.0.3+2
  cloud_firestore: ^0.12.9
  firebase_core: ^0.4.0+8
  google_sign_in: ^4.0.6
  firebase_auth: ^0.14.0+1

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
   - res/images/bag.jpg
   - res/images/computer.jpg
   - res/images/dress.jpg
   - res/images/phone.jpg
   - res/images/shoes.jpg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

【问题讨论】:

您是否在 Google 控制台上填写了同意书?错误 12500。 【参考方案1】:

我遇到了同样的问题。升级到 AndroidX 解决了我的问题。 将您的 Flutter 项目升级到 androidx Android Studio 可以节省时间。

步骤:

    要将项目升级到 androidx,请在 Android Studio 的 Flutter 项目中打开 android 文件夹。

    重构 菜单中选择 迁移到 AndroidX

    并应用迁移更改

    运行flutter clean build

【讨论】:

【参考方案2】:

在应用层build.gradle

 defaultConfig       

    minSdkVersion 23


在项目级别gradle.properties添加

android.useAndroidX=true
android.enableJetifier=true

build.gradle添加

dependencies 
    classpath 'com.android.tools.build:gradle:3.2.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.2.0'

如果您不使用 Kotlin 支持,则可以跳过该 Kotlin 插件 不要忘记运行flutter clean 然后flutter pub upgrade

这些更改帮助我防止了我的应用崩溃,它也应该对您有所帮助。

【讨论】:

这肯定有帮助。【参考方案3】:

我正在使用 firebase_auth:^0.14.0+2 google_sign_in: ^4.0.6

应用级分级:

dependencies 
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.firebase:firebase-core:17.0.1'

收到此错误....

E/AndroidRuntime(5431): 原因: java.lang.NullPointerException: 尝试调用虚拟方法'android.view.View androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(android.view.View, java.lang. String, android.content.Context, android.util.AttributeSet)' 在空对象引用上

完成: 扑干净 清理缓存 颤振升级 颤振酒吧获取

【讨论】:

你是如何清理缓存的? C:flutter/pub-cache/hosted/../ & 我几乎尝试了 2 天来让两个版本一起工作,但失败了。所以我使用 google_sign_in: 4.0.2 & firebase_auth: ^0.11.0+7 我在 iOS 上遇到了同样的问题 .... 有没有人偶然为 iOS 解决了这个问题?

以上是关于Flutter Firebase 身份验证与 Google 应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

如何将 Flutter Bloc 与 Firebase 电话身份验证一起使用

Flutter Firebase 身份验证与 Google 应用程序崩溃

Flutter Firebase signInWithCustomToken 与现有身份验证系统的用户 ID?

成功身份验证后,Flutter Firebase 数据库权限被拒绝

Flutter Firebase 电话身份验证不起作用

Firebase 身份验证不会在 Flutter 中保留在 iOS 或 Android 上