To install the Android agent, we recommend you follow our guided install:
- Go to one.newrelic.com > Integrations & Agents > Mobile > Android.
- Follow the guided install steps to set up your Android agent.
- Wait a few minutes, then view your data by going to one.newrelic.com > All capabilities > Mobile > (select your app).
Manually install the Android agent
The steps below are outlined in our guided install. Note that even if you manually install the agent, you'll still need to create a New Relic app and obtain the generated application token. This token is essential for the agent to send data to New Relic.
Add the Android agent plugin to your project.
Regarding the location of the plugin, you have two options:
Recommended: Using the Gradle plugin DSL
- Starting with agent version 7.6.0, the Android agent plugin is available on the Gradle Plugin Portal as a community plugin. The Gradle plugin DSL simplifies adding plugin dependencies to apps.
- This method involves adding the agent plugin dependency to your project's plugins DSL block.
Legacy Plugin ID (
newrelic)- For older projects or specific use cases, you can use the legacy plugin ID.
- This method involves adding the agent as a dependency to your project's build script, specifying the MavenCentral repository.
Follow the detailed steps below for your chosen installation method.
Important
New Relic automatically adds the following permissions to allow the agent to make HTTP requests to deliver captured data for agent versions 7.6.8 and higher.
In your AndroidManifest.xml file, add the following permissions:
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />Start the agent in the onCreate() method of your Application class, so the agent is running before Android creates any Activity, no matter which Activity your app opens on.
If your app already has an Application subclass, add the import statement and the start() call below to it. If your app doesn't have one, create it:
import android.app.Application;import com.newrelic.agent.android.NewRelic;
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); NewRelic.withApplicationToken("GENERATED_TOKEN").start(this); }}Make sure to replace GENERATED_TOKEN with your app token (you generate it when you create your app in New Relic).
If you created a new Application class, register it in your AndroidManifest.xml by adding the android:name attribute to the <application> element:
<application android:name=".MyApplication" ... >Without this step, Android doesn't create your class, and the agent doesn't start.
Use either the fully qualified class name or a name that starts with a dot (for example, .MyApplication), which Android resolves relative to your app's package.
(Optional) Additional configurations for Proguard/Dexguard
If you use ProGuard or Dexguard for code shrinking, follow these steps:
Create a
newrelic.propertiesfile in your app-level directory (ex:/projectname/app/newrelic.properties).In this new file, add the following line, replacing
GENERATED_TOKENwith your actual token:com.newrelic.application_token=GENERATED_TOKENImportant
New Relic automatically adds the proguard rules for agent version 7.6.8 and later.
Follow the steps described on Configure ProGuard or DexGuard for Android apps.
Clean your project, then run your app in an emulator or device to generate traffic. Wait a few minutes as your agent captures that data.
View your app's data on New Relic by going to one.newrelic.com > All capabilities > Mobile > (select an app).
Choose where to start the agent
We recommend starting the agent from Application.onCreate(). Android creates your Application class before any Activity, so the agent is running whether your app opens on its usual launch Activity or on a different one, for example from a push notification, a deep link, an app shortcut, or a widget.
You can still start the agent in the onCreate() method of an Activity. If you do, the agent runs only for launches that reach that Activity. If you move the start() call to your Application class, remove it from your Activity.
Keep the following in mind:
- Start the agent once. The agent ignores any call to
start()while it's already running, so leaving the call in both places doesn't start the agent twice. - Android runs
Application.onCreate()in every process your app declares. If any of your components setandroid:process, the agent starts in each of those processes too, and each one reports its own session. Start the agent only when your code is running in your app's main process. start()reads and writes small files on the calling thread, so StrictMode can report disk access on the main thread wherever you call it.
What's next?
Congratulations! You've successfully installed the Android agent. You have a few options for next steps:
- Configure agent behavior during Gradle builds with the New Relic Gradle plugin.
- Upgrade the Android agent SDK to keep your Android agent up to date.
- Having problems with your Android installation? Follow the troubleshooting procedures.
Android 4.x: Multidex support
Mobile monitoring for Android versions older than Android 5.0 (API level 21) use the Dalvik runtime to execute app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. To get around this limitation, you must enable multidex support. Then you can use the multidex support library, which becomes part of the primary DEX file of your app. The library manages access to the additional DEX files and their containing code.