Firestore的onSnapshot()和get()导致安卓应用崩溃

4

问题

Firestore的onSnapshot()get()导致Android应用程序崩溃。我确定在编程和设置方面没有做错任何事情。能否有人告诉我我哪里做错了?

try {
    const ref = firebase.firestore().collection(`test/${someId}/child`)

    this.unsubscribe = ref.onSnapshot(querySnapshot => {
        let list = []
        querySnapshot.forEach(doc => {

        }, error => console.error('error') // Error is not being caught)
} catch (e) {
    console.error(e) // error is not being caught
}

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath 'com.google.gms:google-services:3.1.2'
        classpath 'io.fabric.tools:gradle:+'
        classpath 'com.google.firebase:firebase-plugins:1.1.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

        maven {
            url 'https://maven.google.com'
        }

        maven {
            url 'https://maven.fabric.io/public'
        }
    }
}

app/build.gradle

apply plugin: "com.android.application"
apply plugin: "io.fabric"
apply plugin: "com.google.firebase.firebase-perf"

import com.android.build.OutputFile

project.ext.react = [
        entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    signingConfigs {
        debug {
            keyAlias 'debug'
            keyPassword 'android'
            storeFile file('debug.keystore')
            storePassword 'android'
        }
    }
    compileSdkVersion 23
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "..."
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a": 1, "x86": 2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation(project(':react-native-firebase')) {
        transitive = false
    }
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:23.0.1'
    implementation 'com.facebook.react:react-native:+'
    // From node_modules
    implementation(project(':react-native-google-signin')) {
        exclude group: "com.google.android.gms" // very important
    }
    implementation 'com.google.android.gms:play-services-base:11.8.0'
    implementation 'com.google.android.gms:play-services-auth:11.8.0'

    implementation 'com.google.firebase:firebase-core:11.8.0'
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    implementation "com.google.firebase:firebase-firestore:11.8.0"
    implementation 'com.google.firebase:firebase-storage:11.8.0'
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true
    }
    implementation 'com.google.firebase:firebase-crash:11.8.0'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    implementation 'com.google.firebase:firebase-invites:11.8.0'
    implementation 'com.google.firebase:firebase-perf:11.8.0'
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

package.json

{
  "name": "TokoDemo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "base-64": "^0.1.0",
    "native-base": "^2.4.1",
    "numeral": "^2.0.6",
    "prop-types": "^15.6.1",
    "react": "16.3.1",
    "react-native": "0.55.1",
    "react-native-easy-grid": "^0.1.17",
    "react-native-firebase": "^4.0.0",
    "react-native-google-signin": "^0.12.0",
    "react-redux": "^5.0.7",
    "react-router": "^4.2.0",
    "react-router-native": "^4.2.0",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0",
    "shortid": "^2.2.8"
  },
  "devDependencies": {
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.3",
    "react-test-renderer": "16.3.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

崩溃分析错误

Fatal Exception: java.lang.NoSuchMethodError
No virtual method getDocument()Lcom/google/firebase/firestore/QueryDocumentSnapshot; in class Lcom/google/firebase/firestore/DocumentChange; or its super classes (declaration of 'com.google.firebase.firestore.DocumentChange' appears in /data/app/io.zeven.toko.demo-2/base.apk)

io.invertase.firebase.firestore.FirestoreSerialize.documentChangeToWritableMap (FirestoreSerialize.java:129)
io.invertase.firebase.firestore.FirestoreSerialize.documentChangesToWritableArray (FirestoreSerialize.java:103)
io.invertase.firebase.firestore.FirestoreSerialize.snapshotToWritableMap (FirestoreSerialize.java:73)
io.invertase.firebase.firestore.RNFirebaseFirestoreCollectionReference.handleQuerySnapshotEvent (RNFirebaseFirestoreCollectionReference.java:246)
io.invertase.firebase.firestore.RNFirebaseFirestoreCollectionReference.access$000 (RNFirebaseFirestoreCollectionReference.java:32)
io.invertase.firebase.firestore.RNFirebaseFirestoreCollectionReference$2.onEvent (RNFirebaseFirestoreCollectionReference.java:85)
io.invertase.firebase.firestore.RNFirebaseFirestoreCollectionReference$2.onEvent (RNFirebaseFirestoreCollectionReference.java:81)
com.google.firebase.firestore.zzi.onEvent (Unknown Source)
com.google.android.gms.internal.zzevc.zza (Unknown Source)
com.google.android.gms.internal.zzevd.run (Unknown Source)
android.os.Handler.handleCallback (Handler.java:751)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6119)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:886)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:776)

屏幕截图错误崩溃分析

环境

Android react-native 0.55.1 react-native-firebase 4.0.0

  1. 应用目标平台: macOS High Sierra 10.13.3

  2. 开发操作系统: intelliJ Ultimate Android (Pixel 2 API 25)

  3. 构建工具:

  4. React Native 版本: 0.55.1

  5. RNFirebase 版本: "react-native-firebase": "^4.0.0",


你的 buildToolsVersion 必须与 compileSdkVersion 相似,将 compileSdkVersion 替换为 27 并重新构建。 - Pritish Vaidya
@PritishVaidya 我把compileSdkVersion改成了27,删除了watchman并重置了缓存。但它仍然崩溃。 - Fendy Jong
1个回答

0

我没有找到这个问题的答案。

然而,我只是将 RNFirebase 升级到 12.0.1 版本,升级后它就可以正常工作了。

这是我的新的 app/build.gradle 文件。

apply plugin: "com.android.application"
apply plugin: "io.fabric"
apply plugin: "com.google.firebase.firebase-perf"

import com.android.build.OutputFile

project.ext.react = [
        entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    signingConfigs {
        debug {
            keyAlias 'debug'
            keyPassword 'android'
            storeFile file('debug.keystore')
            storePassword 'android'
        }
    }
    compileSdkVersion 27

    defaultConfig {
        applicationId "..."
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }

        // TODO because of firestore: recheck for the next update
        multiDexEnabled true
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a": 1, "x86": 2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation(project(':react-native-firebase')) {
        transitive = false
    }
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.facebook.react:react-native:+'

    // From node_modules
    implementation(project(':react-native-google-signin')) {
        exclude group: "com.google.android.gms" // very important
    }

    // RNFirebase required dependencies
    implementation "com.google.firebase:firebase-core:12.0.1"
    implementation "com.google.android.gms:play-services-base:12.0.1"
    implementation "com.google.android.gms:play-services-auth:12.0.1"

    // RNFirebase optional dependencies
    implementation "com.google.firebase:firebase-ads:12.0.1"
    implementation "com.google.firebase:firebase-auth:12.0.1"
    implementation "com.google.firebase:firebase-config:12.0.1"
    implementation "com.google.firebase:firebase-database:12.0.1"
    implementation "com.google.firebase:firebase-invites:12.0.1"
    implementation "com.google.firebase:firebase-firestore:12.0.1"
    implementation "com.google.firebase:firebase-messaging:12.0.1"
    implementation "com.google.firebase:firebase-perf:12.0.1"
    implementation "com.google.firebase:firebase-storage:12.0.1"

    implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true
    }
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

针对 Android 模拟器:Google Play 服务(Auth)12.0.1 - 需要 Android 8.1(Oreo API 27)。
如果您还有疑问,请查看 react-native-firebase-starter 的 app/build.gradleapp/build.gradle
希望这可以帮到您。

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