diff --git a/README.md b/README.md index 3b0b9d6617..2393a54641 100644 --- a/README.md +++ b/README.md @@ -38,21 +38,30 @@ Add `bugsnag-android` as a dependency in your `pom.xml`: - Place it in your Android app's `libs/` folder -Configuring Your Manifest -------------------------- +Configuring Your `AndroidManifest.xml` +-------------------------------------- -- Ensure you have the `android.permission.INTERNET` permission listed in - your `AndroidManifest.xml`. This is required to send crash reports to - Bugsnag +- Configure your Bugsnag API key as `meta-data` in your manifest's `` tag: -- To enable network diagnostics for each device (internet connectivity, etc) - you should also add the `android.permission.ACCESS_NETWORK_STATE` - permission to your `AndroidManifest.xml`. + ```xml + + + + ``` + +- Enable the `INTERNET`, `ACCESS_NETWORK_STATE` and `GET_TASKS` permissions: + + ```xml + + + + + + + ``` + +For a full example, see our [example AndroidManifest.xml](https://raw.githubusercontent.com/bugsnag/bugsnag-android/master/example/src/main/AndroidManifest.xml). -- To see which activity was active at the time of a crash, you should also - add the `android.permission.GET_TASKS` permission to your - `AndroidManifest.xml`. If you are targeting API level 21+ (Android 5.0+) - this is not required. Initializing Bugsnag @@ -69,7 +78,7 @@ Initializing Bugsnag capturing exceptions: ```java - Bugsnag.init(this, "your-api-key-goes-here"); + Bugsnag.init(this); ``` diff --git a/example/src/main/AndroidManifest.xml b/example/src/main/AndroidManifest.xml index e6739fec8a..5dd1cc96ee 100644 --- a/example/src/main/AndroidManifest.xml +++ b/example/src/main/AndroidManifest.xml @@ -12,6 +12,8 @@ + + diff --git a/example/src/main/java/com/bugsnag/android/example/ExampleActivity.java b/example/src/main/java/com/bugsnag/android/example/ExampleActivity.java index f2a6d50c1d..81f9eb58e0 100644 --- a/example/src/main/java/com/bugsnag/android/example/ExampleActivity.java +++ b/example/src/main/java/com/bugsnag/android/example/ExampleActivity.java @@ -19,8 +19,6 @@ public class ExampleActivity extends Activity { - private static String BUGSNAG_API_KEY = "066f5ad3590596f9aa8d601ea89af845"; - /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) @@ -29,7 +27,7 @@ public void onCreate(Bundle savedInstanceState) setContentView(R.layout.main); // Initialize the Bugsnag client - Bugsnag.init(this, BUGSNAG_API_KEY); + Bugsnag.init(this); // Execute some code before every bugsnag notification Bugsnag.beforeNotify(new BeforeNotify() { diff --git a/src/main/java/com/bugsnag/android/Bugsnag.java b/src/main/java/com/bugsnag/android/Bugsnag.java index 638d493e8e..303fd983c4 100644 --- a/src/main/java/com/bugsnag/android/Bugsnag.java +++ b/src/main/java/com/bugsnag/android/Bugsnag.java @@ -15,6 +15,16 @@ public final class Bugsnag { private static Client client; private Bugsnag() {} + /** + * Initialize the static Bugsnag client + * + * @param androidContext an Android context, usually this + */ + public static Client init(Context androidContext) { + client = new Client(androidContext); + return client; + } + /** * Initialize the static Bugsnag client * @@ -22,7 +32,8 @@ private Bugsnag() {} * @param apiKey your Bugsnag API key from your Bugsnag dashboard */ public static Client init(Context androidContext, String apiKey) { - return init(androidContext, apiKey, true); + client = new Client(androidContext, apiKey); + return client; } /** diff --git a/src/main/java/com/bugsnag/android/Client.java b/src/main/java/com/bugsnag/android/Client.java index 9015bffcba..c2937daec4 100644 --- a/src/main/java/com/bugsnag/android/Client.java +++ b/src/main/java/com/bugsnag/android/Client.java @@ -1,6 +1,8 @@ package com.bugsnag.android; import android.content.Context; +import android.content.pm.ApplicationInfo;; +import android.content.pm.PackageManager; /** * A Bugsnag Client instance allows you to use Bugsnag in your Android app. @@ -22,6 +24,15 @@ public class Client { private User user = new User(); private ErrorStore errorStore; + /** + * Initialize a Bugsnag client + * + * @param androidContext an Android context, usually this + */ + public Client(Context androidContext) { + this(androidContext, null); + } + /** * Initialize a Bugsnag client * @@ -44,6 +55,15 @@ public Client(Context androidContext, String apiKey, boolean enableExceptionHand throw new NullPointerException("You must provide a non-null android Context"); } + // Get the application context, many things need this + appContext = androidContext.getApplicationContext(); + + // Attempt to load API key from AndroidManifest.xml + try { + ApplicationInfo ai = appContext.getPackageManager().getApplicationInfo(appContext.getPackageName(), PackageManager.GET_META_DATA); + apiKey = ai.metaData.getString("com.bugsnag.android.API_KEY"); + } catch (Exception e) { } + if(apiKey == null) { throw new NullPointerException("You must provide a Bugsnag API key"); } @@ -51,9 +71,6 @@ public Client(Context androidContext, String apiKey, boolean enableExceptionHand // Build a configuration object config = new Configuration(apiKey); - // Get the application context, many things need this - appContext = androidContext.getApplicationContext(); - // Set up and collect constant app and device diagnostics appData = new AppData(appContext, config); deviceData = new DeviceData(appContext);