Skip to content

Commit fbb75ee

Browse files
TheBuggedYRNAli Abdelfattah
andauthored
[MOB-8866] Start SDK from JS only (#704)
* Start SDK from JS only * Update CHANGELOG * Update README * Update CHANGELOG.md Co-authored-by: Ali Abdelfattah <[email protected]>
1 parent 0f6e548 commit fbb75ee

File tree

10 files changed

+74
-268
lines changed

10 files changed

+74
-268
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
## master
22

3+
* Breaking: Adds the ability to initialize the Android SDK from JavaScript. Check the migration guide referenced in our README
4+
* Breaking: Removes the deprecated APIs. Check the migration guide referenced in our README
35
* Adds the ability to opt out of iOS source maps auto upload through the INSTABUG_SOURCEMAPS_UPLOAD_DISABLE env variable
46
* Adds dynamic entry file support through the INSTABUG_ENTRY_FILE env variable
57
* Fixes an issue with setRequestFilterExpression API not working with Hermes
68
* Fixes an issue with swipe invocation event not working on Android
7-
* Breaking: Removes the deprecated APIs. For the full list, check PR #703
89

910
## 10.13.0 (2022-03-17)
1011

InstabugSample/android/app/src/main/java/com/instabugsample/MainApplication.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.facebook.react.ReactPackage;
1010
import com.facebook.soloader.SoLoader;
1111
import java.lang.reflect.InvocationTargetException;
12-
import com.instabug.reactlibrary.RNInstabugReactnativePackage;
1312

1413
import java.util.List;
1514

@@ -45,12 +44,6 @@ public ReactNativeHost getReactNativeHost() {
4544
@Override
4645
public void onCreate() {
4746
super.onCreate();
48-
new RNInstabugReactnativePackage.Builder("2c63627b9923e10eee2c8abf92e6925f", MainApplication.this)
49-
.setInvocationEvent("button")
50-
.setPrimaryColor("#1D82DC")
51-
.setFloatingEdge("left")
52-
.setFloatingButtonOffsetFromTop(250)
53-
.build();
5447
SoLoader.init(this, /* native exopackage */ false);
5548
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
5649
}

README.md

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,56 +44,26 @@ Updating to a new version? Check the [Update Guide](#update-guide) before bumpin
4444

4545
## Initializing Instabug
4646

47-
To start using Instabug, import it as follows.
47+
To start using Instabug, import it as follows, then initialize it in the `constructor` or `componentWillMount`. This line will let the SDK work with the default behavior. The SDK will be invoked when the device is shaken. You can customize this behavior through the APIs.
4848

4949
```javascript
5050
import Instabug from 'instabug-reactnative';
51+
52+
Instabug.start('APP_TOKEN', [Instabug.invocationEvent.shake]);
5153
```
52-
### iOS
53-
- Initialize it in the `constructor` or `componentWillMount`. This line will let the Instabug SDK work with the default behavior. The SDK will be invoked when the device is shaken. You can customize this behavior through the APIs.
5454

55-
```javascript
56-
Instabug.start('IOS_APP_TOKEN', [Instabug.invocationEvent.shake]);
57-
```
58-
### Android
59-
- Open `android/app/src/main/java/[...]/MainApplication.java`
60-
* Make sure to import the package class:
61-
`import com.instabug.reactlibrary.RNInstabugReactnativePackage;`
62-
* **For React Native >= 0.60**
63-
Add the integration code to the `onCreate()` method like the below snippet.
64-
65-
```java
66-
@Override
67-
public void onCreate() {
68-
super.onCreate();
69-
new RNInstabugReactnativePackage
70-
.Builder("APP_TOKEN", MainApplication.this)
71-
.setInvocationEvent("shake")
72-
.setPrimaryColor("#1D82DC")
73-
.setFloatingEdge("left")
74-
.setFloatingButtonOffsetFromTop(250)
75-
.build();
76-
}
77-
```
78-
* **For React Native < 0.60**
79-
You should find the `getPackages()` method looks like the below snippet. You just need to add your Android app token.
80-
81-
```javascript
82-
@Override
83-
protected List<ReactPackage> getPackages() {
84-
return Arrays.<ReactPackage>asList(
85-
new MainReactPackage(),
86-
new RNInstabugReactnativePackage.Builder("YOUR_APP_TOKEN", MainApplication.this)
87-
.setInvocationEvent("shake")
88-
.setPrimaryColor("#1D82DC")
89-
.setFloatingEdge("left")
90-
.setFloatingButtonOffsetFromTop(250)
91-
.build()
92-
);
93-
}
94-
```
95-
* You can change the invocation event from here, simply by replacing the `"shake"` with any of the following `"button"`, `"none"`, `"screenshot"`, or `"swipe"`. You can change the primary color by replacing the `"#1D82DC"` with any color of your choice.
96-
In the case that you are using the floating button as an invocation event, you can change the floating button edge and the floating button offset using the last two methods, by replacing `"left"` to `"right"`, and by changing the offset number.
55+
**For React Native < 0.60 on Android**
56+
You should find the `getPackages()` method looks like the below snippet. You just need to add your Android app token.
57+
58+
```java
59+
@Override
60+
protected List<ReactPackage> getPackages() {
61+
return Arrays.<ReactPackage>asList(
62+
new MainReactPackage(),
63+
new RNInstabugReactnativePackage()
64+
);
65+
}
66+
```
9767
9868
_You can find your app token by selecting the SDK tab from your [**Instabug dashboard**](https://dashboard.instabug.com)._
9969

__tests__/index.spec.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ describe('Instabug Module', () => {
101101
});
102102

103103
it('should call the native method start', () => {
104-
105-
Platform.OS = 'ios';
106104
const token = 'some-token';
107105
const invocationEvents = [Instabug.invocationEvent.floatingButton, Instabug.invocationEvent.shake];
108106
Instabug.start(token, invocationEvents);
@@ -111,17 +109,6 @@ describe('Instabug Module', () => {
111109

112110
});
113111

114-
// it('should not call the native method start when platform is android', () => {
115-
116-
// Platform.OS = 'android';
117-
// const token = 'some-token';
118-
// const invocationEvents = [Instabug.invocationEvent.floatingButton, Instabug.invocationEvent.shake];
119-
// Instabug.start(token, invocationEvents);
120-
121-
// expect(start.calledOnceWithExactly(token, invocationEvents)).toBe(true);
122-
123-
// });
124-
125112
it('should call the native method setUserData', () => {
126113

127114
const userData = "userData";

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.net.Uri;
77
import android.os.Handler;
88
import android.os.Looper;
9+
import android.util.Log;
910
import android.view.View;
1011

1112
import com.facebook.react.bridge.Arguments;
@@ -25,6 +26,7 @@
2526
import com.facebook.react.uimanager.NativeViewHierarchyManager;
2627
import com.facebook.react.uimanager.UIBlock;
2728
import com.facebook.react.uimanager.UIManagerModule;
29+
import com.instabug.apm.APM;
2830
import com.instabug.bug.BugReporting;
2931
import com.instabug.bug.instabugdisclaimer.Internal;
3032
import com.instabug.bug.invocation.InvocationMode;
@@ -38,6 +40,7 @@
3840
import com.instabug.library.Instabug;
3941
import com.instabug.library.InstabugState;
4042
import com.instabug.library.OnSdkDismissCallback;
43+
import com.instabug.library.Platform;
4144
import com.instabug.library.extendedbugreport.ExtendedBugReport;
4245
import com.instabug.library.invocation.InstabugInvocationEvent;
4346
import com.instabug.library.InstabugColorTheme;
@@ -236,23 +239,16 @@ public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
236239
private final String REPORT_DISCARD_DIALOG_POSITIVE_ACTION = "discardAlertAction";
237240
private final String REPORT_ADD_ATTACHMENT_HEADER = "addAttachmentButtonTitleStringName";
238241

239-
private Application androidApplication;
240-
private Instabug mInstabug;
241-
private InstabugInvocationEvent invocationEvent;
242242
private InstabugCustomTextPlaceHolder placeHolders;
243243
private Report currentReport;
244244

245245
/**
246-
* Instantiates a new Rn instabug reactnative module.
246+
* Instantiates a new Rn Instabug ReactNative module.
247247
*
248248
* @param reactContext the react context
249-
* @param mInstabug the m instabug
250249
*/
251-
public RNInstabugReactnativeModule(ReactApplicationContext reactContext, Application
252-
androidApplication, Instabug mInstabug) {
250+
public RNInstabugReactnativeModule(ReactApplicationContext reactContext) {
253251
super(reactContext);
254-
this.androidApplication = androidApplication;
255-
this.mInstabug = mInstabug;
256252
//init placHolders
257253
placeHolders = new InstabugCustomTextPlaceHolder();
258254
}
@@ -283,13 +279,52 @@ public void run() {
283279
String key = stringArray[i];
284280
invocationEventsArray[i] = ArgsRegistry.getDeserializedValue(key, InstabugInvocationEvent.class);
285281
}
286-
new Instabug.Builder(getCurrentActivity().getApplication(), token).setInvocationEvents(invocationEventsArray).build();
282+
283+
final Application application = (Application) getReactApplicationContext().getApplicationContext();
284+
285+
setCurrentPlatform();
286+
setBaseUrlForDeprecationLogs();
287+
288+
new Instabug.Builder(application, token)
289+
.setInvocationEvents(invocationEventsArray)
290+
.build();
291+
292+
// Temporarily disabling APM hot launches
293+
APM.setHotAppLaunchEnabled(false);
287294
} catch (Exception e) {
288295
e.printStackTrace();
289296
}
290297
}
291298
});
299+
}
300+
301+
private void setCurrentPlatform() {
302+
try {
303+
Method method = InstabugUtil.getMethod(Class.forName("com.instabug.library.Instabug"), "setCurrentPlatform", int.class);
304+
if (method != null) {
305+
Log.i("IB-CP-Bridge", "invoking setCurrentPlatform with platform: " + Platform.RN);
306+
method.invoke(null, Platform.RN);
307+
} else {
308+
Log.e("IB-CP-Bridge", "setCurrentPlatform was not found by reflection");
309+
}
310+
} catch (Exception e) {
311+
e.printStackTrace();
312+
}
313+
}
292314

315+
private void setBaseUrlForDeprecationLogs() {
316+
try {
317+
Method method = InstabugUtil.getMethod(Class.forName("com.instabug.library.util.InstabugDeprecationLogger"), "setBaseUrl", String.class);
318+
if (method != null) {
319+
method.invoke(null, "https://docs.instabug.com/docs/react-native-sdk-migration-guide");
320+
}
321+
} catch (ClassNotFoundException e) {
322+
e.printStackTrace();
323+
} catch (IllegalAccessException e) {
324+
e.printStackTrace();
325+
} catch (InvocationTargetException e) {
326+
e.printStackTrace();
327+
}
293328
}
294329

295330
/**
@@ -1070,7 +1105,7 @@ public void getAllUserAttributes(final Callback userAttributesCallback) {
10701105
public void run() {
10711106
WritableMap writableMap = Arguments.createMap();
10721107
try {
1073-
HashMap<String, String> map = mInstabug.getAllUserAttributes();
1108+
HashMap<String, String> map = Instabug.getAllUserAttributes();
10741109
for (HashMap.Entry<String, String> entry : map.entrySet()) {
10751110
writableMap.putString(entry.getKey(), entry.getValue());
10761111
}

0 commit comments

Comments
 (0)