Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the code version for android #113

Merged
merged 1 commit into from
May 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions android/src/main/java/com/rollbar/RollbarReactNative.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rollbar;

import java.util.Map;
import java.util.MissingResourceException;

import com.google.gson.JsonObject;

Expand All @@ -12,7 +13,12 @@
import com.facebook.react.bridge.ReactMethod;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageInfo;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Bundle;

import com.rollbar.android.Rollbar;
import com.rollbar.android.provider.NotifierProvider;
Expand All @@ -25,6 +31,8 @@ public class RollbarReactNative extends ReactContextBaseJavaModule {
private static final String REACT_NATIVE = "react-native";
private static final String NOTIFIER_NAME = "rollbar-react-native";
private static final String NOTIFIER_VERSION = "0.7.2";
private static final String MANIFEST_CODE_VERSION = "com.rollbar.android.CODE_VERSION";

private ReactContext reactContext;

public static ReactPackage getPackage() {
Expand All @@ -37,18 +45,44 @@ public RollbarReactNative(ReactApplicationContext reactContext) {
}

public static void init(Context context, String accessToken, String environment) {
final String codeVersion = loadCodeVersionFromManifest(context);

Rollbar.init(context, accessToken, environment, true, false, new ConfigProvider() {
@Override
public Config provide(ConfigBuilder builder) {
return builder
.framework(REACT_NATIVE)
.platform("android")
.codeVersion(codeVersion) // OK to send null here if there's no code version.
.notifier(new NotifierProvider(NOTIFIER_VERSION, NOTIFIER_NAME))
.build();
}
});
}

private static String loadCodeVersionFromManifest(Context context) {
ApplicationInfo ai;

try {
ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
return null;
}

Bundle data = ai.metaData;

// The bundle will be null if there are no metadata values.
if (data == null) {
return null;
}

try {
return data.getString(MANIFEST_CODE_VERSION);
} catch (MissingResourceException e) {
return null;
}
}

/**
* Record a critical error.
*
Expand Down