【发布时间】:2018-04-19 08:52:39
【问题描述】:
我正在尝试使用 react-native 和 firebase 构建一个应用程序,并且在此过程中遇到了一些安装问题。目前,我的问题是应用程序在启动后立即崩溃,在安装后第一次启动时(调试或发布模式)。一旦杀死并重新启动它就可以正常工作。
日志中似乎没有记录特定错误。使用日志消息来缩小崩溃点,它似乎是:
-
Firebase.auth().signInAnonymouslyAndRetrieveData(),或 -
doc_ref.get()其中doc_ref是Firebase.firestore().collection("profiles").doc(instance_id)的结果
取决于调用顺序。
在上面,“开始”意味着做
- 要么
- react-native run-android
- 或
- cd android && ./gradlew assembleRelease
- cd ..
- react-native run-android --variant=release
如果应用程序尚未安装,则执行上述任一操作会导致应用程序启动并立即崩溃,但随后会出现在手机的已安装应用程序列表中。安装应用后重复上述操作会使其按预期工作。
任何帮助将不胜感激!
环境:
- 运行 Ubuntu 17.10 的笔记本电脑
- 通过 USB 连接的真正的 Sony Xperia Z5 手机
- 节点 v6.11.4
- yarn v1.6.0
- react-native 0.55.3
- react-native-cli 2.0.1
- react-native-firebase 4.0.4
相关应用代码:
// Top level app component
export default class App extends React.Component<Props, State> {
...
componentDidMount() {
try {
IconFiles.load();
StatusBar.setHidden(true);
BackHandler.addEventListener("hardwareBackPress",
this.hardwareBackPress.bind(this));
const that = this;
this.setupProfile()
...
.catch(function (err) {
Log.error(`error in new App() end of promise chain: ${err}`);
Crashlytics.recordError(1, err);
});
...
export default class Profile {
...
public static getProfile(): Promise<Profile> {
const that = this;
Log.debug(`getProfile() starting: ${RNFirebase.SDK_VERSION}`);
return RNFirebase.auth().signInAnonymouslyAndRetrieveData()
.then(function (blah) {
Log.debug(`getProfile() signInAnonymouslyAndRetrieveData() return okay: ${blah}`);
return RNFirebase.iid().get()
})
.then(function (instance_id: string): Promise<Profile> {
Log.debug(`getProfile() got instance id: ${instance_id}`);
const firestoreCollection: any
= RNFirebase.firestore().collection("profiles");
const doc_ref = firestoreCollection.doc(instance_id);
Log.debug(`getProfile() got profile doc_ref: ${doc_ref.id}`);
return doc_ref.get();
})
.then(function (doc_snapshot: any /*Firebase.firestore.DocumentSnapshot*/) {
Log.debug(`getProfile() got profile doc_snap: ${doc_snapshot.exists}`);
let data: Constrob;
if (doc_snapshot.exists) {
data = <Constrob>doc_snapshot.data();
} else {
data = {
created_date: new Date(),
feedback: null,
starts: null,
token: null,
};
}
return new Profile(doc_snapshot.ref, data, doc_snapshot.exists);
});
}
...
package.json ...
"dependencies": {
"firebase": "^4.12.0",
"loglevel": "^1.6.0",
"moment": "^2.21.0",
"react": "^16.3.1",
"react-native": "0.55.2",
"react-native-firebase": "^4.0",
"react-native-htmlview": "^0.12.1",
"react-native-star-rating": "^1.0.9",
"react-native-svg": "^6.3.1",
"react-native-vector-icons": "^4.6.0"
},
android/build.gradle
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:1.+"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenLocal()
jcenter()
google()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
}
}
android/gradle.properties
android.useDeprecatedNdk=true
android.enableAapt2=false
android/gradle/gradle-wrapper.properties
...
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
android/app/build.gradle
apply plugin: "com.android.application"
apply plugin: "io.fabric"
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 {
compileSdkVersion 23
defaultConfig {
applicationId "com.doorkey"
minSdkVersion 16
targetSdkVersion 23
versionCode 4
versionName "1.0.3"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
signingConfigs {
release {
if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
}
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"
signingConfig signingConfigs.release
}
}
// 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 "com.google.android.gms:play-services-base:12.0.1"
implementation "com.google.firebase:firebase-core:12.0.1"
implementation "com.google.firebase:firebase-auth:12.0.1"
implementation("com.crashlytics.sdk.android:crashlytics:2.9.1@aar") {
transitive = true
}
implementation "com.google.firebase:firebase-firestore:12.0.1"
implementation "com.google.firebase:firebase-messaging:12.0.1"
implementation project(':react-native-vector-icons')
implementation project(':react-native-svg')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:23.0.1"
implementation "com.facebook.react:react-native:+" // From node_modules
}
// 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'
【问题讨论】:
-
您解决了这个问题吗?安装 firebase 后我遇到了类似的问题。
-
不,我没有。由于这样的一系列问题,最终放弃了 react-native / firebase 轨道,并恢复为普通的 react / webapp 方法。
标签: android firebase react-native