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 overriding the versionCode via Configuration #610

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## TBD

* Allow overriding the versionCode via Configuration
[#610](https://github.com/bugsnag/bugsnag-android/pull/610)

## 4.20.0 (2019-09-25)

* Record StorageManager cache behaviour in internal error reports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void setUp() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
PackageManager packageManager = context.getPackageManager();
Configuration config = new Configuration("api-key");
config.setVersionCode(1);
AppData obj = new AppData(context, packageManager, config, sessionTracker);
this.appData = obj.getAppDataSummary();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void setUp() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
PackageManager packageManager = context.getPackageManager();
Configuration config = new Configuration("api-key");
config.setVersionCode(1);
AppData obj = new AppData(context, packageManager, config, sessionTracker);
this.appData = obj.getAppData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public void testEmptyManifestConfig() {
ConfigFactory.populateConfigFromManifest(protoConfig, data);

assertEquals(config.getApiKey(), protoConfig.getApiKey());
assertEquals(config.getVersionCode(), protoConfig.getVersionCode());
assertEquals(config.getBuildUUID(), protoConfig.getBuildUUID());
assertEquals(config.getAppVersion(), protoConfig.getAppVersion());
assertEquals(config.getReleaseStage(), protoConfig.getReleaseStage());
Expand All @@ -198,13 +199,15 @@ public void testEmptyManifestConfig() {
public void testFullManifestConfig() {
String buildUuid = "123";
String appVersion = "v1.0";
Integer versionCode = 27;
String releaseStage = "debug";
String endpoint = "http://example.com";
String sessionEndpoint = "http://session-example.com";

Bundle data = new Bundle();
data.putString("com.bugsnag.android.BUILD_UUID", buildUuid);
data.putString("com.bugsnag.android.APP_VERSION", appVersion);
data.putInt("com.bugsnag.android.VERSION_CODE", versionCode);
data.putString("com.bugsnag.android.RELEASE_STAGE", releaseStage);
data.putString("com.bugsnag.android.SESSIONS_ENDPOINT", sessionEndpoint);
data.putString("com.bugsnag.android.ENDPOINT", endpoint);
Expand All @@ -219,6 +222,7 @@ public void testFullManifestConfig() {
ConfigFactory.populateConfigFromManifest(protoConfig, data);
assertEquals(buildUuid, protoConfig.getBuildUUID());
assertEquals(appVersion, protoConfig.getAppVersion());
assertEquals(versionCode, protoConfig.getVersionCode());
assertEquals(releaseStage, protoConfig.getReleaseStage());
assertEquals(endpoint, protoConfig.getEndpoint());
assertEquals(sessionEndpoint, protoConfig.getSessionEndpoint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ private String calculateNotifierType() {
@Nullable
@SuppressWarnings("deprecation")
private Integer calculateVersionCode() {
if (packageInfo != null) {
return packageInfo.versionCode;
Integer versionCode = config.getVersionCode();

if (versionCode != null) {
return versionCode;
} else {
return null;
if (packageInfo != null) {
return packageInfo.versionCode;
} else {
return null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ConfigFactory {

private static final String BUGSNAG_NAMESPACE = "com.bugsnag.android";
private static final String MF_APP_VERSION = BUGSNAG_NAMESPACE + ".APP_VERSION";
private static final String MF_VERSION_CODE = BUGSNAG_NAMESPACE + ".VERSION_CODE";
private static final String MF_ENDPOINT = BUGSNAG_NAMESPACE + ".ENDPOINT";
private static final String MF_SESSIONS_ENDPOINT = BUGSNAG_NAMESPACE + ".SESSIONS_ENDPOINT";
private static final String MF_RELEASE_STAGE = BUGSNAG_NAMESPACE + ".RELEASE_STAGE";
Expand Down Expand Up @@ -92,6 +93,9 @@ static void populateConfigFromManifest(@NonNull Configuration config,
config.setAppVersion(data.getString(MF_APP_VERSION));
config.setReleaseStage(data.getString(MF_RELEASE_STAGE));

if (data.containsKey(MF_VERSION_CODE)) {
config.setVersionCode(data.getInt(MF_VERSION_CODE));
}
if (data.containsKey(MF_ENDPOINT)) {
String endpoint = data.getString(MF_ENDPOINT);
String sessionEndpoint = data.getString(MF_SESSIONS_ENDPOINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Configuration extends Observable implements Observer {
private final String apiKey;
private String buildUuid;
private String appVersion;
private Integer versionCode;
private String context;
private volatile String endpoint = "https://notify.bugsnag.com";
private volatile String sessionEndpoint = "https://sessions.bugsnag.com";
Expand Down Expand Up @@ -129,6 +130,26 @@ public void setAppVersion(@NonNull String appVersion) {
NativeInterface.MessageType.UPDATE_APP_VERSION, appVersion));
}

/**
* Gets the version code sent to Bugsnag.
*
* @return Version Code
*/
@Nullable
public Integer getVersionCode() {
return versionCode;
}

/**
* Set the version code sent to Bugsnag. By default we'll pull this
* from your AndroidManifest.xml
*
* @param versionCode the version code to send
*/
public void setVersionCode(@Nullable Integer versionCode) {
this.versionCode = versionCode;
}

/**
* Gets the context to be sent to Bugsnag.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,12 @@ public void deliver(@NonNull Report report,
public void testSetNullDelivery() {
config.setDelivery(null);
}

@Test
public void testVersionCode() {
Configuration configuration = new Configuration("api-key");
assertNull(configuration.getVersionCode()); // populated in client ctor if null
configuration.setVersionCode(577);
assertEquals(577, (int) configuration.getVersionCode());
}
}