我无法使用 Flutter 为 Android 实现 Google (Firebase) 登录

问题描述 投票:0回答:1
Exception has occurred.
PlatformException (PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null))

使用 google 登录在 iOS 上按预期工作 该问题仅适用于 Android

使用

./gradlew signedReport
我获得了 SHA1 和 SHA256 密钥并将其输入到 Firebase 菜单中

然后我继续下载 google-services.json 文件并将其插入项目中

我通过添加以下内容更新了我的 AndroidManifest.xml

 <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
            <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-..........."/>

我的根级别 build.gradle 看起来像

plugins {
    id "com.google.gms.google-services" version "4.3.15" apply false
    id "org.jetbrains.kotlin.android" version "2.0.0" apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}


rootProject.buildDir = "../build"
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

我的settings.gradle看起来像

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    // START: FlutterFire Configuration
    id "com.google.gms.google-services" version "4.3.15" apply false
    // END: FlutterFire Configuration
id "org.jetbrains.kotlin.android" version "2.0.0" apply false

}

include ":app"

我的APP级别build.gradle看起来像

plugins {
    id "com.android.application"
    // START: FlutterFire Configuration
    id 'com.google.gms.google-services'
    // END: FlutterFire Configuration
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}
dependencies {
  implementation 'com.google.android.gms:play-services-ads:23.2.0'
  implementation platform('com.google.firebase:firebase-bom:33.1.1')
  implementation 'com.google.firebase:firebase-analytics'
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader("UTF-8") { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
    flutterVersionCode = "1"
}

def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
    flutterVersionName = "1.0"
}

android {
    namespace = "com.example.attendly_check"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.attendly_check"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdk = 31
        targetSdk = 31
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig = signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}

我的 Google Sign In 实现如下所示

Future<UserCredential> signInWithGoogle() async {
    print("Initiating");
    final GoogleSignIn _googleSignIn = GoogleSignIn(
      clientId:
          '<client_id>',
    );
    print("Went through");
    final googleUser = await _googleSignIn.signIn(); // THE ERROR IS TRIGGERED HERE

    // Obtain the auth details from the request
    final GoogleSignInAuthentication? googleAuth =
        await googleUser?.authentication;

    // Create a new credential
    final credential = GoogleAuthProvider.credential(
      accessToken: googleAuth?.accessToken,
      idToken: googleAuth?.idToken,
    );

    // Once signed in, return the UserCredential
    return await FirebaseAuth.instance.signInWithCredential(credential);
  }
flutter firebase firebase-authentication flutter-dependencies google-signin
1个回答
0
投票

当您在 Flutter 中遇到 PlatformException 时,特别是错误 PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null),它通常与 Google 的问题有关登录配置或插件设置。

以下一些步骤可能会解决您的问题:

清理构建并刷新依赖项:

每当您为 Flutter 应用程序安装或更新插件时,最好清理应用程序构建并刷新依赖项。您可以使用以下命令来执行此操作:

flutter clean
flutter pub get

运行 flutter clean 将删除构建目录并确保删除所有旧文件或缓存文件。然后,flutter pub get将刷新您的依赖项,确保您使用的是最新版本的插件。

如果问题仍然存在,请采取其他步骤

如果执行第一步后错误仍然存在,您可能需要检查您的配置:

检查Google登录配置:

  • 确保您的应用在 Firebase 控制台中配置正确。
  • 验证 Firebase 项目设置中是否正确设置了 SHA-1SHA-256 键。这些键对于 Google 登录正常工作至关重要。
© www.soinside.com 2019 - 2024. All rights reserved.