React Native | "react-native-google-signin" 引起应用程序崩溃

5

React Native | react-native-google-signin

在安装了react-native-google-sigin后,

npm i react-native-google-signin

当应用启动时突然崩溃(强制关闭),我已经按照 Github 的文档所述执行了所有步骤,但当我运行我的应用时,它立即崩溃,日志中没有任何错误信息(不是 Android-Log 也不是 adb logcat *:S ReactNative:V ReactNativeJS:V)。
以下是我的代码:
build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
        googlePlayServicesAuthVersion = "16.0.1" // <--- use this version or newer
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.4.1")
        classpath 'com.android.tools.build:gradle:3.1.2' // <--- use this version or newer
        classpath 'com.google.gms:google-services:4.1.0' // <--- use this version or newer
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
    }
}

settings.gradle

rootProject.name = 'App'
include ':react-native-reanimated'
project(':react-native-reanimated').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-reanimated/android')
include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':react-native-google-signin', ':app'
project(':react-native-google-signin').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-google-signin/android')
include ':app'

app/build.gradle

dependencies {
    implementation project(':react-native-reanimated')
    implementation project(':react-native-gesture-handler')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation(project(":react-native-google-signin"))
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation(project(':react-native-maps')){
            exclude group: 'com.google.android.gms', module: 'play-services-base'
            exclude group: 'com.google.android.gms', module: 'play-services-maps'
        }
        implementation 'com.google.android.gms:play-services-base:10.0.1'
        implementation 'com.google.android.gms:play-services-maps:10.0.1'

    if (enableHermes) {
      def hermesPath = "../../node_modules/hermesvm/android/";
      debugImplementation files(hermesPath + "hermes-debug.aar")
      releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
      implementation jscFlavor
    }
}

apply plugin: 'com.google.gms.google-services' // <--- this should be the last line

有人能帮我解决这个错误吗?因为当我卸载react-native-google-signin时出现了问题。

npm uninstall react-native-google-signin

它可以正常运行


你好,你尝试使用react-native-link将依赖项链接到你的项目了吗? - OsamaD
你解决了吗? - Vincent Dave Te
请在此处添加您的 package.json 信息。为了调试,了解RN版本和google-sign-in版本将非常有帮助。 - Florin Dobre
对我来说问题在于 React 的依赖版本需要大于 0.59,我使用了 15.0.0 版本解决了这个问题。实现代码如下:'com.google.android.gms:play-services-base:15.0.0' 和 'com.google.android.gms:play-services-maps:15.0.0'。 - Vincent Dave Te
你有 google-services.json 文件吗? - Rafael Hovsepyan
3个回答

3

在app/build.gradle中设置以下内容:implementation(project(":@react-native-community_google-signin")),而不是之前的写法。

 implementation(project(":react-native-google-signin"))

2
有两个版本的React Native Google Signin:
- 适用于React Native <=0.59的是 "react-native-google-signin" - 适用于React Native >=0.60的是 "@react-native-community/google-signin"
由于您正在使用第一个版本,我假设您的React Native版本是<=0.59。如果不是,则必须切换到社区版本。
此外,您的android/build.gradle文件包含重复的 "classpath com.android.tools.build:gradle"。
classpath("com.android.tools.build:gradle:3.4.1")
classpath 'com.android.tools.build:gradle:3.1.2' // <--- use this version or newer

由于React Native Google Signin需要3.1.2或更高版本,因此您可以安全地删除第二个。

此外,根据文档中的说明,您的android/app/build.gradle缺少以下实现:

implementation "com.android.support:appcompat-v7:23.0.1"

希望能对您有所帮助。

1

如果你启用了Hermes,那么说明你使用的是RN 0.60+。

只需使用社区版:

"@react-native-community/google-signin": "^3.0.3"

RN 0.60+具有自动链接功能,所以您不需要这些行

// settings.gradle
include ':react-native-google-signin', ':app'
project(':react-native-google-signin').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/google-signin/android')


// MainApplication.java
new RNGoogleSigninPackage()

我认为你应该尝试将这种自动链接用于其他库(可能需要使用它们的最新版本)。
如果仍然无法正常工作,您可以在此处获取更多信息。

https://github.com/react-native-community/react-native-google-signin/blob/master/docs/android-guide.md

(注意,除非使用手动链接并仔细检查步骤1-3,否则不应执行步骤4)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接