Skip to content

Commit 12eacfc

Browse files
authored
[Fix][SDK-436] Expo crash on init (#209)
* fix: add missing init call to Rollbar Android * fix: prevent future NPEs, fail silently * refactor: remove initialization of Native notifier when RollbarReactNative is created * ci: add workflow to run Android tests * build: set explicit dependencies versions and add test libraries * test: add tests * ci: modify target branch * ci: remove install npm dependencies step * ci: remove android working directory --------- Co-authored-by: chris <christian.buongarzoni@gmail.com>
1 parent 1fb7659 commit 12eacfc

11 files changed

Lines changed: 464 additions & 3 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Android Unit Tests
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
name: Run Java Unit Tests
12+
runs-on: ubuntu-latest
13+
14+
env:
15+
CI: true
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v3
20+
21+
- name: Set up JDK 11
22+
uses: actions/setup-java@v3
23+
with:
24+
distribution: 'temurin'
25+
java-version: '11'
26+
27+
- name: Set up Node.js
28+
uses: actions/setup-node@v3
29+
with:
30+
node-version: '18'
31+
32+
- name: Make Gradle executable
33+
run: chmod +x ./gradlew
34+
35+
- name: Run unit tests
36+
run: ./gradlew test

android/build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ android {
1414

1515
dependencies {
1616
implementation('com.rollbar:rollbar-android:1.7.1')
17-
implementation 'com.facebook.react:react-native:+'
18-
implementation 'com.google.code.gson:gson:+'
17+
implementation 'com.facebook.react:react-android:0.71.0'
18+
implementation 'com.google.code.gson:gson:2.9.0'
19+
20+
testImplementation 'junit:junit:4.13.2'
21+
testImplementation 'org.mockito:mockito-core:3.8.0'
22+
testImplementation 'org.mockito:mockito-inline:3.8.0'
1923
}

android/src/main/java/com/rollbar/RollbarReactNative.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,16 @@ public String getName() {
525525

526526
@ReactMethod
527527
public void init(ReadableMap options) {
528+
Rollbar rollbar = Rollbar.instance();
529+
if (rollbar == null) {
530+
return;
531+
}
528532
final String environment = options.hasKey("environment") ? options.getString("environment") : "production";
529533
final String platform = options.hasKey("platform") ? options.getString("platform") : "android";
530534
final String notifier_version = options.hasKey("notifier") ? options.getMap("notifier").getString("version") : NOTIFIER_VERSION;
531535
final String notifier_name = options.hasKey("notifier") ? options.getMap("notifier").getString("name") : NOTIFIER_NAME;
532536
final boolean enabled = options.hasKey("enabled") ? options.getBoolean("enabled") : true;
533-
Rollbar.instance().configure(new ConfigProvider() {
537+
rollbar.configure(new ConfigProvider() {
534538
@Override
535539
public Config provide(ConfigBuilder builder) {
536540
return builder
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.rollbar;
2+
3+
import android.content.pm.ApplicationInfo;
4+
import android.content.pm.PackageManager;
5+
import android.content.pm.PackageManager.NameNotFoundException;
6+
import android.util.Log;
7+
import com.facebook.react.bridge.ReactApplicationContext;
8+
import com.facebook.react.bridge.ReadableMap;
9+
10+
import java.util.HashMap;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.mockito.MockedStatic;
14+
15+
import static org.mockito.Mockito.*;
16+
import static org.junit.Assert.*;
17+
18+
public class RollbarReactNativeTest {
19+
20+
private ApplicationInfo mockedApplicationInfo;
21+
private PackageManager mockedPackageManager;
22+
private RollbarReactNative rollbarModule;
23+
private ReactApplicationContext mockedContext;
24+
private ReadableMap mockedReadableMap;
25+
26+
@Before
27+
public void setUp() {
28+
mockedContext = mock(ReactApplicationContext.class);
29+
mockedReadableMap = mock(ReadableMap.class);
30+
mockedPackageManager = mock(PackageManager.class);
31+
mockedApplicationInfo = mock(ApplicationInfo.class);
32+
rollbarModule = new RollbarReactNative(mockedContext);
33+
}
34+
35+
@Test
36+
public void configUpdateDoNothingWhenNativeNotifierIsNotInitialized() {
37+
try (MockedStatic<Log> mockedLog = mockStatic(Log.class)) {
38+
39+
rollbarModule.init(mockedReadableMap);
40+
41+
mockedLog.verify(() -> Log.w("Rollbar", "Attempt to access Rollbar.instance() before initialization."));
42+
}
43+
}
44+
45+
@Test
46+
public void configUpdateSuccessfulWhenNativeNotifierIsInitialized() throws NameNotFoundException {
47+
try (MockedStatic<Log> mockedLog = mockStatic(Log.class)) {
48+
initNativeNotifier();
49+
50+
rollbarModule.init(mockedReadableMap);
51+
52+
mockedLog.verify(never(), () -> Log.w("Rollbar", "Attempt to access Rollbar.instance() before initialization."));
53+
}
54+
}
55+
56+
private void initNativeNotifier() throws NameNotFoundException {
57+
when(mockedContext.getPackageManager())
58+
.thenReturn(mockedPackageManager);
59+
60+
when(mockedContext.getPackageName())
61+
.thenReturn("any");
62+
63+
when(mockedPackageManager.getApplicationInfo("any", PackageManager.GET_META_DATA))
64+
.thenReturn(mockedApplicationInfo);
65+
66+
RollbarReactNative.init(mockedContext, "Any token", "Any environment");
67+
}
68+
69+
}

build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
buildscript {
2+
repositories {
3+
google()
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath 'com.android.tools.build:gradle:4.2.2'
8+
}
9+
}
10+
11+
allprojects {
12+
repositories {
13+
google()
14+
mavenCentral()
15+
}
16+
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
android.useAndroidX=true

gradle/wrapper/gradle-wrapper.jar

57.8 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.4-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

gradlew

Lines changed: 234 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)