Skip to content

Commit

Permalink
Merge pull request #40 from bugsnag/apikey-in-manifest
Browse files Browse the repository at this point in the history
Configure Bugsnag API key in AndroidManifest.xml
  • Loading branch information
loopj committed Dec 31, 2014
2 parents 5b4372c + ec0fd22 commit 31d788c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<application>` 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
<application ...>
<meta-data android:name="com.bugsnag.android.API_KEY" android:value="your-api-key-here"/>
</application>
```

- Enable the `INTERNET`, `ACCESS_NETWORK_STATE` and `GET_TASKS` permissions:

```xml
<!-- Required: Used to deliver Bugsnag crash reports -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- Optional: To provide network connectivity information to Bugsnag -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- Optional: To see which Activity was active at the time of a crash -->
<uses-permission android:name="android.permission.GET_TASKS"/>
```

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
Expand All @@ -69,7 +78,7 @@ Initializing Bugsnag
capturing exceptions:

```java
Bugsnag.init(this, "your-api-key-goes-here");
Bugsnag.init(this);
```


Expand Down
2 changes: 2 additions & 0 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<meta-data android:name="com.bugsnag.android.API_KEY" android:value="066f5ad3590596f9aa8d601ea89af845"/>
</application>

<uses-permission android:name="android.permission.INTERNET" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/bugsnag/android/Bugsnag.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@ public final class Bugsnag {
private static Client client;
private Bugsnag() {}

/**
* Initialize the static Bugsnag client
*
* @param androidContext an Android context, usually <code>this</code>
*/
public static Client init(Context androidContext) {
client = new Client(androidContext);
return client;
}

/**
* Initialize the static Bugsnag client
*
* @param androidContext an Android context, usually <code>this</code>
* @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;
}

/**
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 <code>this</code>
*/
public Client(Context androidContext) {
this(androidContext, null);
}

/**
* Initialize a Bugsnag client
*
Expand All @@ -44,16 +55,22 @@ 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");
}

// 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);
Expand Down

0 comments on commit 31d788c

Please sign in to comment.