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 3 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 @@ -66,7 +66,10 @@ Map<String, Object> getAppDataSummary() {
map.put("type", calculateNotifierType());
map.put("releaseStage", guessReleaseStage());
map.put("version", calculateVersionName());
map.put("versionCode", calculateVersionCode());
Integer versionCode = config.getVersionCode();
if (versionCode != null) {
map.put("versionCode", versionCode.intValue());
}
map.put("codeBundleId", config.getCodeBundleId());
return map;
}
Expand Down Expand Up @@ -131,20 +134,6 @@ private String calculateNotifierType() {
}
}

/**
* The version code of the running Android app, from android:versionCode
* in AndroidManifest.xml
*/
@Nullable
@SuppressWarnings("deprecation")
private Integer calculateVersionCode() {
if (packageInfo != null) {
return packageInfo.versionCode;
} else {
return null;
}
}

/**
* The version code of the running Android app, from android:versionName
* in AndroidManifest.xml
Expand Down
14 changes: 14 additions & 0 deletions bugsnag-android-core/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
Expand Down Expand Up @@ -123,6 +124,7 @@ public Client(@NonNull Context androidContext,
* @param androidContext an Android context, usually <code>this</code>
* @param configuration a configuration for the Client
*/
@SuppressWarnings("deprecation") // ignore packageInfo.versionCode deprecation on API 28
public Client(@NonNull Context androidContext, @NonNull Configuration configuration) {
warnIfNotAppContext(androidContext);
appContext = androidContext.getApplicationContext();
Expand Down Expand Up @@ -201,6 +203,18 @@ public Unit invoke(Boolean connected) {
}
}

//noinspection ConstantConditions
if (config.getVersionCode() == null) {
try {
PackageManager packageManager = appContext.getPackageManager();
String packageName = appContext.getPackageName();
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
config.setVersionCode(packageInfo.versionCode);
} catch (PackageManager.NameNotFoundException exception) {
Logger.warn("Could not retrieve package/application information");
}
}

// Create the error store that is used in the exception handler
errorStore = new ErrorStore(config, appContext, new ErrorStore.Delegate() {
@Override
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
*/
@NonNull
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(@NonNull 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());
}
}