How do I use Crashlytics for my React Native Android App? - react-native

I am trying to figure out how to use Crashlytics from Fabric for my React Native Android APP. I followed the steps on the Fabric homepage and added some lines in my build.gradle files. But the builds always crash.
Is there a difference using Crashlytics for React Native Android and Crashlytics for Native Android development using Android Studio and Java?

I got it working in some way, but it may not be the perfect solution...
1: Add fabric/crashlytics into your app/build.gradle - File
(I didn´t have the buildscript in my app/build.gradle so i just included it. But i am not sure if this is good....)
buildscript {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// The Fabric Gradle plugin uses an open ended version to react
// quickly to Android tooling updates
classpath 'io.fabric.tools:gradle:1.+'
}
}
// Add this directly under: apply plugin: "com.android.application"
apply plugin: 'io.fabric'
// and this directly under: apply from: "react.gradle"
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
// Last but not least add Crashlytics Kit into dependencies
compile('com.crashlytics.sdk.android:crashlytics:2.5.5#aar') {
transitive = true
}
2: The most important, because it is nowhere mentioned (or i didn´t find it anywhere), import Crashlytics and Fabric into MainActivity:
import com.crashlytics.android.Crashlytics;
import io.fabric.sdk.android.Fabric;
3: In your onCreate - method add:
// Fabrics
Fabric.with(this, new Crashlytics());
When you´ve done this, you will at least get Crashreports which are caused by native Code (Java Code). Crashes which are caused through JS - Syntax or similar wont be notified. There you will get the known RedBox :P
Good luck!

For the newer versions of React Native you have to import Bundle and place your own onCreate Method like this:
// Added Bundle to use onCreate which is needed for our Fabrics workaround
import android.os.Bundle;
..........
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fabrics
Fabric.with(this, new Crashlytics());
}
Not sure if this is good or not since they have removed the onCreate but it works for me

Try this : https://fabric.io/kits/android/crashlytics/install
Summarizes all the files you need to edit in your Android installation well.
For the AndroidManifest.xml file, replace the android:value key (e.g. below) with your actual API key. Remember to get your API key from your organization settings... 1. login to https://fabric.io/settings/organizations and 2. click on build secret.
<meta-data
android:name="io.fabric.ApiKey"
android:value="<api key here>"
/>

Related

A failure occurred while executing com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction

My react native project build fails somehow because of this error:
Execution failed for task ':app:mergeDebugNativeLibs'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction
> 2 files found with path 'lib/arm64-v8a/libfbjni.so' from inputs:
- C:\Users\Antonio\.gradle\caches\transforms-3\7cca348744e25f57fc2d9f871aa73c9a\transformed\jetified-react-native-0.71.0-rc.0-debug\jni\arm64-v8a\libfbjni.so
- C:\Users\Antonio\.gradle\caches\transforms-3\08b0f5c7017bf081f79b63ea5b053dc0\transformed\jetified-fbjni-0.3.0\jni\arm64-v8a\libfbjni.so
If you are using jniLibs and CMake IMPORTED targets, see
https://developer.android.com/r/tools/jniLibs-vs-imported-targets
Anybody got a clue what could cause the build to fail? I haven't edited any build file and/or removed/installed/upgraded new packages thanks
For me this worked (after reading Tony's link), my version of react was 0.66.0
Changed this file android\app\build.gradle
implementation "com.facebook.react:react-native:+" // From node_modules
to
implementation "com.facebook.react:react-native:0.66.0!!" // From node_modules
Short answer:
in your android/app/build.gradle
change
implementation 'com.facebook.react:react-native:+'
to ---> (replace 0.67.2 with your current react native version)
implementation 'com.facebook.react:react-native:0.67.2!!'
Long answer:
This is happening because all the templates reference the React Native dependency by range, like implementation 'com.facebook.react:react-native:+'. Usually this dependency gets resolved from the local Maven repo in ./node_modules/react-native/android but since it has been published to Maven Central it's now grabbing the very latest RC.
You can resolve this problem by forcing the React Native dependency to the version you expect with something like this implementation 'com.facebook.react:react-native:0.67.2!!' in your app's Gradle file. The !! is shorthand for restricting Gradle from upgrading if your project or its transitive dependencies depend on a newer version.
work for me, if your react native application version >= 0.63 you can update the patch version which should fix your problem.
link: https://github.com/facebook/react-native/issues/35210#:~:text=We%20have%20prepared%20releases%20for%20all%20the%20main%20versions%20of%20react%2Dnative%20with%20an%20hotfix%3A
if not just go to android/build.gradle and then in the allprojects object add the following code with the current version of react native in package.json
configurations.all {
resolutionStrategy {
force 'com.facebook.react:react-native:CURRENT_VERSION_OF_REACT_NATIVE'
}
}
Here's a workaround to fix this problem if you are not using latest version of react-native.
https://github.com/facebook/react-native/issues/35210
Go to android folder -> build.gradle file -> inside allprojects object and add following code. Add react native version from node_modules -> react-native -> package.json // "version": "0.68.2".
configurations.all {
resolutionStrategy {
force 'com.facebook.react:react-native:0.68.2'
}
}
See fb/rn#35204
This is the Official recommended fix!
Found through this issue: https://github.com/facebook/react-native/issues/35210.
Copied from this PR here
For my RN 0.66.0 project I only had to add theses lines:
allprojects {
repositories {
exclusiveContent {
// Official recommended fix for Android build problem with React Native versions below 0.71
// https://github.com/facebook/react-native/issues/35210
// TODO: remove this exclusiveContent section when we upgrade to React Native 0.71 (or above)
// copied from https://github.com/Scottish-Tech-Army/Volunteer-app/pull/101/commits/40a30310ee46194efbaf1c07aef8a0df70231eeb
filter {
includeGroup "com.facebook.react"
}
forRepository {
maven {
url "$rootDir/../node_modules/react-native/android"
}
}
}
}
}
I had the same issue. There is a new patch for react-native now so update it in your package.json.
Mine is
"react-native": "^0.70.3"
and I changed it to
"react-native": "^0.70.5"
which worked for me
The answer lies here depending on the version of your react native. Patches are available for RN version 0.63 and up
https://github.com/facebook/react-native/issues/35210
Tips:
Don't just update react-native package, do an npm install
Clean gradle before running the app
Only add the code below IF your react native version is < 0.63
def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())
allprojects {
configurations.all {
resolutionStrategy {
// Remove this override in 0.65+, as a proper fix is included in react-native itself.
force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
}
}
}

Dynamically link packages based on product flavor to support GMS & HMS with one code base

In order for my application to be approved and visible on both of Google PlayStore and Huawei App Gallery, I am currently maintaining two different branches:
Google branch which has react-native-maps installed but no HMS packages.
Huawei branch which has HMS maps installed but not react-native-maps.
I'm aware of the HMS+GMS approach, but from my experience having a module like react-native-maps that relies heavily on GMS made my app hidden from pure HMS devices, even though my app had ways to check and never navigate to screens that rely on GMS.
Both branches have the same code on the JavaScript side except one file which is used to display the map, this file imports from react-native-maps on GMS phones, and from react-native-hms-map for Huawei phones.
My question is: is there a way to dynamically exclude some files and packages in build time based on the product flavor so that I can use one codebase and just ignore some file when building the APK.
Solution:
Managed to come up with a solution that got my app approved and fully visible on app stores through disabling auto linking for react native maps and manually linking it based on the product flavor.
(Code might not be the cleanest but it's behaving as expected, so any cleanup suggestions would be appreciated)
Steps:
1. Disable manual linking for react-native-maps on android
By creating a file named react-native.config.js in the root directory of the project, and added the following
module.exports = {
dependencies: {
"react-native-maps": {
platforms: {
android: null,
}
}
}
}
2. Added product flavors for Google and Huawei
By adding the following to android/app/build.gradle
...
...
android{
...
...
flavorDimensions "provider"
productFlavors {
google {
dimension "provider"
}
huawei {
dimension "provider"
}
}
...
...
}
...
...
3. Added the following to the same android/app/build.gradle file
...
...
dependencies {
...
...
huaweiImplementation 'com.huawei.hms:location:4.0.2.300'
huaweiImplementation 'com.huawei.hms:hwid:4.0.1.300'
googleImplementation project(':react-native-maps')
...
...
}
...
...
4. Added the following into android/settings.gradle
include ':react-native-maps'
project(':react-native-maps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-maps/lib/android')
5. Created 2 folders inside android/app/src
folders named: huawei and google with the following structure
6. added a java file inside android/app/src/google/java/com/appname
MapPackageChecker.java
package com.appname;
import com.facebook.react.ReactPackage;
import com.airbnb.android.react.maps.MapsPackage;
public class MapPackageChecker {
public static ReactPackage getMapPackage() {
return new MapsPackage();
}
}
7. added a java file inside android/app/src/huawei/java/com/appname
MapPackageChecker.java
package com.appname;
import com.facebook.react.ReactPackage;
public class MapPackageChecker {
public static ReactPackage getMapPackage() {
return null;
}
}
8. added the following to android/app/src/main/java/com/appname/MainApplication.java
import static com.appname.MapPackageChecker.getMapPackage;
...
...
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
//start of new lines
if(BuildConfig.FLAVOR.equals("google")){
packages.add(getMapPackage());
}
//end of new lines
return packages;
}
...
...
9. Access flavor name from react native code
I decided to use react-native-build-config for this purpose
Example for navigation component:
import googleMapScreen from "./googleMapScreen.js"; //relies on gms maps
import huaweiMapScreen from "./huaweiMapScreen.js"; //relies on hms maps
import BuildConfig from 'react-native-build-config';
const flavor = BuildConfig.FLAVOR
...
...
<Stack.Screen
name="MapScreen"
component={flavor === "huawei" ? huaweiMapScreen : googleMapScreen}
/>
...
...
10. After adding product flavors, we need to make some changes to our commands
yarn react-native run-android
becomes: yarn react-native run-android --variant=huaweiDebug
or: yarn react-native run-android --variant=googleDebug
./gradlew assembleRelease
becomes: ./gradlew assembleHuaweiRelease
or: ./gradlew assembleGoogleRelease
11. For convenience we can add the following to package.json scripts
"scripts":{
"run-huawei": "yarn react-native run-android --variant=huaweiDebug",
"run-google": "yarn react-native run-android --variant=googleDebug",
}
First of all, Huawei does support react-native-maps. Please check here: https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides-V1/integrating-sdk-0000001050034173-V1
When HMS+GMS is the preferred solution, in the code the APP needs to decide whether to use HMS or GMS based on the availability of HMS and GMS services on the device.
. How to decide whether to use HMS or GMS :
Check that GMS is available
There are also some links for your reference:
React Native Application Detect device support GMS or HMS: https://forums.developer.huawei.com/forumPortal/en/topic/0201200045194610058?fid=0101187876626530001
Choice SDK - an open-source GMS-HMS wrapper: https://forums.developer.huawei.com/forumPortal/en/topic/0201555879126330259?fid=0101187876626530001
You may refer to this.
It's configured in here:
And run the following command:

WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'

I just upadated kotlin to 1.3.30 and I now get this error when syncing gradle:
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been
replaced with 'variant.getPackageLibraryProvider()'. It will be
removed at the end of 2019. For more information, see
https://d.android.com/r/tools/task-configuration-avoidance. To
determine what is calling variant.getPackageLibrary(), use
-Pandroid.debug.obsoleteApi=true on the command line to display a stack trace. Affected Modules: hydatabase
Here is my build.gradle:
apply plugin: 'com.squareup.sqldelight'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 19
}
lintOptions {
abortOnError false
}
}
sqldelight {
Database {
packageName = "com.company.hydatabase"
}
}
kotlin {
targets {
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.android, 'android')
}
sourceSets {
commonMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
jvmMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
// ICU4J: Use DecimalFormat
// Get rid of this when minSDKLevel = API 24 - Nougat (7.0)
// https://developer.android.com/guide/topics/resources/internationalization.html
api 'com.ibm.icu:icu4j:60.2'
}
androidMain.dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
api "com.squareup.sqldelight:android-driver:1.1.1"
}
androidMain.dependsOn jvmMain
}
}
task copyDatabase(type: Copy) {
from "${rootProject.file('hyappcommon/Databases/').path}"
into "${rootProject.file('hydatabase/src/main/assets/databases/').path}"
include '**/*.sqlite'
}
preBuild.dependsOn(copyDatabase)
// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
compileClasspath
}
If you debug, it shows
REASON: The Kotlin plugin is currently calling this API. We are working to solve this.
To see this error please run
./gradlew -Pandroid.debug.obsoleteApi=true --stacktrace
As tommyboy said, the Kotlin plugin is calling this deprecated API.
If you don't want to get this warning while Kotlin is working on this, you can just use the previous version of Kotlin plugin like:
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
}
It's probably a bug and fixed soon
You can revert back to the previous version or add this line to gradle.properties
android.debug.obsoleteApi=true
In my project gradle file I had
buildscript {
ext.kotlin_version = '1.3.31'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Simple changing
ext.kotlin_version = '1.3.31' to ext.kotlin_version = '1.3.41' solved the problem
when using version 1.3.31 i had tried gradlew -Pandroid.debug.obsoleteApi=true
It mentioned
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been
replaced with 'variant.getPackageLibraryProvider()'. It will be
removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
REASON: The Kotlin plugin is currently calling this API. We are working to solve this.
WARNING: Debugging obsolete API calls can take time during
configuration. It's recommended to not keep it on at all times.
Looks like it is solved in 1.3.41
It's an issue with the Kotlin plugin, as mentioned here. It'll get fixed in a later version.
After I updated Kotlin to 1.3.30, the following dependencies cause the error:
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// ... other dependencies
}
I have reported the issue here:
https://github.com/bintray/gradle-bintray-plugin/issues/284
https://github.com/dcendents/android-maven-gradle-plugin/issues/81
By the way, you can ignore that error message.
The issue is tracked here and it is fixed.
Just use the Kotlin Gradle plugin v 1.3.40 or higher.
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'.
It will be removed at the end of 2019.
Just updated to "v1.3.40-release-Studio3.4-1" plugin.
as you can see in https://youtrack.jetbrains.com/issue/KT-30784
I met this problem when I use kotlin plugin with library plugin. I found that if you use kotlin plugin with application plugin, it works well. But if you use kotlin plugin with library plugin, it will cause this problem. So that means:
// work well:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
// error:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
As the error diplayed that, you can use ./gradlew -Pandroid.debug.obsoleteApi=true --stacktrace to find out with module caused this problem.
Then I found that one of my module used the wrong plugin combination above. And that seems to be a bug of kotlin plugin. So finally, I upgraded the kotlin plugin, and then it worked well. Below is the kotlin plugin that I finally used:
buildscript {
ext.kotlin_version = '1.3.40'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
You can run this command in the root project
gradlew -Pandroid.debug.obsoleteApi=true
and the warning will be disappeared.

Fabric.io upload not working with crashlytics and kotlin

I want upload my app with Fabric.io but she didn't show in interface.
I follow this tutorial for first time upload.
I add this in my Build.gradle
apply plugin: 'io.fabric'
implementation('com.crashlytics.sdk.android:crashlytics:2.9.7#aar') {
transitive = true;
}
I add my apiKey in my AndroidManifest
<meta-data
android:name="io.fabric.ApiKey"
android:value="*"
/>
And I add this in my app main
import com.crashlytics.android.Crashlytics
import io.fabric.sdk.android.Fabric
override fun onCreate() {
super.onCreate()
Fabric.with(this, Crashlytics())
}
I haven't error when I build with Android Studio but my app isn't upload
Which step did I miss ?
Looks like there is a problem with Crashlytics 2.9.7 because the same problem just happened to me. You can solve it by downgrading the dependency to version 2.9.6
Version 2.9.7 have a bug with "firebase_crashlytics_collection_enabled" flag. New library release (2.9.8) fixes this issue.

librashlitics does not exists in build

I'm trying to integrate Fabric Crashlitics with native code. I had crashes in native code but there are no any records about them in fabric. probably I have not integrated fabric correctly. I do not see libcrashlitics.so in apk.
in my gradle there are
apply plugin: 'io.fabric'
crashlytics {
enableNdk true
manifestPath 'src/main/AndroidManifest.xml'
}
I found good article but version is 2.0.0
https://paramsen.github.io/crashlytics-ndk-and-cmake/?readNext=true
Mike from Fabric here. Here's how to setup Crashlytics NDK in your app.
Add these lines to your build.gradle:
buildscript {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// The Fabric Gradle plugin uses an open ended version to react
// quickly to Android tooling updates
classpath 'io.fabric.tools:gradle:1.+'
}
}
2) Also in your app's build.gradle apply the Fabric plugin:
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
3) Add the Fabric repo:
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
4) Enable NDK as you mentioned:
crashlytics {
enableNdk true
}
5) Add Crashlytics and Crashlytics NDK AARs:
dependencies {
// Crashlytics Kit
compile 'com.crashlytics.sdk.android:crashlytics:2.9.3'
// NDK Kit
compile 'com.crashlytics.sdk.android:crashlytics-ndk:2.0.4'
}
6) In your Java code, add these imports:
import io.fabric.sdk.android.Fabric;
import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.ndk.CrashlyticsNdk;
7) Init Fabric as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics(), new CrashlyticsNdk());
setContentView(R.layout.activity_sample);
}
and you're done. Don't forget to upload symbols as well:
~ username$ ./gradlew crashlyticsUploadSymbolsRelease