Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e1c1063

Browse files
feat: report startup crashes on android (#1012)
Jira ID: MOB-12628, MOB-12689, MOB-12690
1 parent 15e442b commit e1c1063

File tree

8 files changed

+381
-62
lines changed

8 files changed

+381
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add new strings (`StringKey.discardAlertStay` and `StringKey.discardAlertDiscard`) for overriding the discard alert buttons for consistency between iOS and Android ([#1001](https://github.com/Instabug/Instabug-React-Native/pull/1001)).
88
- Add a new string (`StringKey.reproStepsListItemNumberingTitle`) for overriding the repro steps list item (screen) title for consistency between iOS and Android ([#1002](https://github.com/Instabug/Instabug-React-Native/pull/1002)).
99
- Add support for RN version 0.73 by updating the `build.gradle` file with the `namespace` ([#1004])(https://github.com/Instabug/Instabug-React-Native/pull/1004)
10+
- Add native-side init API which can be used to catch and report startup crashes on android. ([#1012](https://github.com/Instabug/Instabug-React-Native/pull/1012))
1011

1112
### Deprecated
1213

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ dependencies {
6868
implementation "androidx.multidex:multidex:2.0.1"
6969
implementation 'com.facebook.react:react-native:+'
7070

71-
testImplementation "org.mockito:mockito-inline:3.4.0"
71+
testImplementation "org.mockito:mockito-inline:3.12.1"
7272
testImplementation "org.mockito:mockito-android:3.4.0"
7373
testImplementation 'junit:junit:4.13.2'
7474
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.instabug.reactlibrary;
2+
3+
import android.app.Application;
4+
import android.util.Log;
5+
6+
import androidx.annotation.NonNull;
7+
import androidx.annotation.VisibleForTesting;
8+
9+
import com.instabug.apm.APM;
10+
import com.instabug.library.Instabug;
11+
import com.instabug.library.LogLevel;
12+
import com.instabug.library.Platform;
13+
import com.instabug.library.invocation.InstabugInvocationEvent;
14+
import com.instabug.reactlibrary.utils.InstabugUtil;
15+
16+
import java.lang.reflect.InvocationTargetException;
17+
import java.lang.reflect.Method;
18+
19+
public class RNInstabug {
20+
21+
private static RNInstabug instance;
22+
23+
private RNInstabug() {}
24+
25+
26+
public static RNInstabug getInstance() {
27+
if (instance == null) {
28+
synchronized (RNInstabug.class) {
29+
if (instance == null) {
30+
instance = new RNInstabug();
31+
}
32+
}
33+
}
34+
return instance;
35+
}
36+
37+
/**
38+
* Initializes the SDK on the native side, which is useful for capturing startup issues specific to the native part of the app.
39+
*
40+
* @param application The application context.
41+
* @param applicationToken The app's identifying token, available on your dashboard.
42+
* @param logLevel The level of detail in logs that you want to print.
43+
* <p>Pick one of the log levels described in {@link LogLevel}.
44+
* default logLevel is {@link LogLevel#ERROR}</p>
45+
* @param InvocationEvent The events that trigger the SDK's user interface.
46+
* Choose from the available events listed in {@link InstabugInvocationEvent}.
47+
*
48+
* @example <p>Here's an example usage: </p>
49+
* <blockquote><pre>
50+
* RNInstabug.getInstance().init(
51+
* this,
52+
* "your_token_here",
53+
* LogLevel.VERBOSE,
54+
* InstabugInvocationEvent.SHAKE,
55+
* ... // Other invocation events
56+
* );
57+
* </pre></blockquote>
58+
*/
59+
public void init(
60+
@NonNull Application application,
61+
@NonNull String applicationToken,
62+
int logLevel,
63+
@NonNull InstabugInvocationEvent... InvocationEvent
64+
) {
65+
try {
66+
67+
setBaseUrlForDeprecationLogs();
68+
setCurrentPlatform();
69+
70+
new Instabug.Builder(application, applicationToken)
71+
.setInvocationEvents(InvocationEvent)
72+
.setSdkDebugLogsLevel(logLevel)
73+
.build();
74+
75+
// Temporarily disabling APM hot launches
76+
APM.setHotAppLaunchEnabled(false);
77+
} catch (Exception e) {
78+
e.printStackTrace();
79+
}
80+
81+
}
82+
83+
84+
85+
/**
86+
* Initializes the SDK on the native side, which is useful for capturing startup issues specific to the native part of the app.
87+
*
88+
* @param application The application context.
89+
* @param applicationToken The app's identifying token, available on your dashboard.
90+
* @param invocationEvent The events that trigger the SDK's user interface.
91+
* Choose from the available events listed in {@link InstabugInvocationEvent}.
92+
*
93+
* @example <p>Here's an example usage: </p>
94+
* <blockquote><pre>
95+
* RNInstabug.getInstance().init(
96+
* this,
97+
* "your_token_here",
98+
* InstabugInvocationEvent.SHAKE,
99+
* ... // Other invocation events
100+
* );
101+
* </pre></blockquote>
102+
*/
103+
public void init(
104+
@NonNull Application application,
105+
@NonNull String applicationToken,
106+
@NonNull InstabugInvocationEvent... invocationEvent
107+
) {
108+
init(application, applicationToken, LogLevel.ERROR, invocationEvent);
109+
}
110+
111+
@VisibleForTesting
112+
public void setCurrentPlatform() {
113+
try {
114+
Method method = InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "setCurrentPlatform", int.class);
115+
if (method != null) {
116+
Log.i("IBG-CP-Bridge", "invoking setCurrentPlatform with platform: " + Platform.RN);
117+
method.invoke(null, Platform.RN);
118+
} else {
119+
Log.e("IBG-CP-Bridge", "setCurrentPlatform was not found by reflection");
120+
}
121+
} catch (Exception e) {
122+
e.printStackTrace();
123+
}
124+
}
125+
126+
@VisibleForTesting
127+
public void setBaseUrlForDeprecationLogs() {
128+
try {
129+
Method method = InstabugUtil.getMethod(Class.forName("com.instabug.library.util.InstabugDeprecationLogger"), "setBaseUrl", String.class);
130+
if (method != null) {
131+
method.invoke(null, "https://docs.instabug.com/docs/react-native-sdk-migration-guide");
132+
}
133+
} catch (Exception e) {
134+
e.printStackTrace();
135+
}
136+
}
137+
}

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import android.graphics.Bitmap;
66
import android.net.Uri;
7-
import android.util.Log;
87
import android.view.View;
98

109
import com.facebook.react.bridge.Arguments;
@@ -20,14 +19,12 @@
2019
import com.facebook.react.uimanager.NativeViewHierarchyManager;
2120
import com.facebook.react.uimanager.UIBlock;
2221
import com.facebook.react.uimanager.UIManagerModule;
23-
import com.instabug.apm.APM;
2422
import com.instabug.bug.instabugdisclaimer.Internal;
2523
import com.instabug.library.Feature;
2624
import com.instabug.library.Instabug;
2725
import com.instabug.library.InstabugColorTheme;
2826
import com.instabug.library.InstabugCustomTextPlaceHolder;
2927
import com.instabug.library.LogLevel;
30-
import com.instabug.library.Platform;
3128
import com.instabug.library.internal.module.InstabugLocale;
3229
import com.instabug.library.invocation.InstabugInvocationEvent;
3330
import com.instabug.library.logging.InstabugLog;
@@ -37,15 +34,13 @@
3734
import com.instabug.library.visualusersteps.State;
3835
import com.instabug.reactlibrary.utils.ArrayUtil;
3936
import com.instabug.reactlibrary.utils.EventEmitterModule;
40-
import com.instabug.reactlibrary.utils.InstabugUtil;
4137
import com.instabug.reactlibrary.utils.MainThreadHandler;
4238

4339
import org.json.JSONException;
4440
import org.json.JSONObject;
4541
import org.json.JSONTokener;
4642

4743
import java.io.File;
48-
import java.lang.reflect.InvocationTargetException;
4944
import java.lang.reflect.Method;
5045
import java.util.ArrayList;
5146
import java.util.Arrays;
@@ -92,7 +87,7 @@ public void removeListeners(Integer count) {
9287
super.removeListeners(count);
9388
}
9489

95-
/**
90+
/**
9691
* Enables or disables Instabug functionality.
9792
* @param isEnabled A boolean to enable/disable Instabug.
9893
*/
@@ -111,7 +106,7 @@ public void run() {
111106
}
112107
}
113108
});
114-
}
109+
}
115110

116111
/**
117112
* Initializes the SDK.
@@ -120,59 +115,15 @@ public void run() {
120115
*/
121116
@ReactMethod
122117
public void init(final String token, final ReadableArray invocationEventValues, final String logLevel) {
123-
MainThreadHandler.runOnMainThread(new Runnable() {
124-
@Override
125-
public void run() {
126-
try {
127-
final ArrayList<String> keys = ArrayUtil.parseReadableArrayOfStrings(invocationEventValues);
128-
final ArrayList<InstabugInvocationEvent> parsedInvocationEvents = ArgsRegistry.invocationEvents.getAll(keys);
129-
final int parsedLogLevel = ArgsRegistry.sdkLogLevels.getOrDefault(logLevel, LogLevel.ERROR);
130-
131-
setCurrentPlatform();
132-
setBaseUrlForDeprecationLogs();
133-
134-
new Instabug.Builder(getCurrentActivity().getApplication(), token)
135-
.setInvocationEvents(parsedInvocationEvents.toArray(new InstabugInvocationEvent[0]))
136-
.setSdkDebugLogsLevel(parsedLogLevel)
137-
.build();
138-
139-
// Temporarily disabling APM hot launches
140-
APM.setHotAppLaunchEnabled(false);
141-
} catch (Exception e) {
142-
e.printStackTrace();
143-
}
144-
}
118+
MainThreadHandler.runOnMainThread(() -> {
119+
final ArrayList<String> keys = ArrayUtil.parseReadableArrayOfStrings(invocationEventValues);
120+
final ArrayList<InstabugInvocationEvent> parsedInvocationEvents = ArgsRegistry.invocationEvents.getAll(keys);
121+
final InstabugInvocationEvent[] invocationEvents = parsedInvocationEvents.toArray(new InstabugInvocationEvent[0]);
122+
final int parsedLogLevel = ArgsRegistry.sdkLogLevels.getOrDefault(logLevel, LogLevel.ERROR);
123+
RNInstabug.getInstance().init(getCurrentActivity().getApplication(), token, parsedLogLevel, invocationEvents);
145124
});
146125
}
147126

148-
private void setCurrentPlatform() {
149-
try {
150-
Method method = InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "setCurrentPlatform", int.class);
151-
if (method != null) {
152-
Log.i("IB-CP-Bridge", "invoking setCurrentPlatform with platform: " + Platform.RN);
153-
method.invoke(null, Platform.RN);
154-
} else {
155-
Log.e("IB-CP-Bridge", "setCurrentPlatform was not found by reflection");
156-
}
157-
} catch (Exception e) {
158-
e.printStackTrace();
159-
}
160-
}
161-
162-
private void setBaseUrlForDeprecationLogs() {
163-
try {
164-
Method method = InstabugUtil.getMethod(Class.forName("com.instabug.library.util.InstabugDeprecationLogger"), "setBaseUrl", String.class);
165-
if (method != null) {
166-
method.invoke(null, "https://docs.instabug.com/docs/react-native-sdk-migration-guide");
167-
}
168-
} catch (ClassNotFoundException e) {
169-
e.printStackTrace();
170-
} catch (IllegalAccessException e) {
171-
e.printStackTrace();
172-
} catch (InvocationTargetException e) {
173-
e.printStackTrace();
174-
}
175-
}
176127

177128
/**
178129
* Adds tag(s) to issues before sending them
@@ -282,7 +233,7 @@ public void run() {
282233
}
283234
});
284235
}
285-
236+
286237
/**
287238
* Gets tags.
288239
*
@@ -576,7 +527,7 @@ public void run() {
576527
}
577528
});
578529
}
579-
530+
580531
/**
581532
* Overrides any of the strings shown in the SDK with custom ones.
582533
* Allows you to customize any of the strings shown to users in the SDK.
@@ -1076,4 +1027,4 @@ public Map<String, Object> getConstants() {
10761027

10771028
return constants;
10781029
}
1079-
}
1030+
}

android/src/test/java/com/instabug/reactlibrary/RNInstabugReactnativeModuleTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ public void tearDown() {
452452
@Test
453453
public void givenString$setString_whenQuery_thenShouldCallNativeApiWithEnum() {
454454
// given
455-
mockStatic(Log.class);
456455
Map<String, InstabugCustomTextPlaceHolder.Key> args = ArgsRegistry.placeholders;
457456
Set<String> keys = args.keySet();
458457

0 commit comments

Comments
 (0)