I'm trying to build an app with react-native and firebase, and have hit a fair few installation issues along the way. Currently, my problem is that the app crashes immediately after its starts, on the first start after it is installed (either debug or release modes). Once killed and re-started it works normally.
There seems to be no specific error recorded in the logs. Using log messages to narrow the point of crash, it appears to be either:
Firebase.auth().signInAnonymouslyAndRetrieveData(), ordoc_ref.get()wheredoc_refis the result ofFirebase.firestore().collection("profiles").doc(instance_id)
depending on the call sequence.
In the above, "start" means doing
- either
- react-native run-android
- or
- cd android && ./gradlew assembleRelease
- cd ..
- react-native run-android --variant=release
Doing either of the above if the app is NOT already installed results in it starting and immediately crashing, but then appearing in the phone's list of installed apps. Repeating the above once the app is installed results in it working as intended.
Any help would be really appreciated!
Environment:
- laptop running Ubuntu 17.10
- Real Sony Xperia Z5 phone connected via USB
- node v6.11.4
- yarn v1.6.0
- react-native 0.55.3
- react-native-cli 2.0.1
- react-native-firebase 4.0.4
Relevant application code:
// 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'