• English日本語한국어
  • Log inStart now

Configure Android apps with Gradle and Android Studio

Once you've installed our Android agent, you can configure your mobile app to improve its performance, stability, and user engagement. Follow the steps below if you're using Gradle and Android Studio.

Configure with Gradle and Android Studio

These procedures to configure your Android app with Gradle and Android Studio also appear in the "Add your DSL code" step of the Android guided UI installation.

  1. In your project's top-level build.gradle file, merge or append the following code before the plugins are applied.

  2. (Optional) For ProGuard or DexGuard:

    a. In your project’s root directory (/projectname/app), add a newrelic.properties file with the following line:


    b. Complete the configuration steps on Configure ProGuard or DexGuard for Android apps. Then come back to this page and move on to the next step.

  1. Set app permissions. Ensure that your Android app requests INTERNET and ACCESS_NETWORK_STATE permissions by adding these lines to your AndroidManifest.xml file:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  2. Start our Android agent. In your Default Activity (as defined in your Manifest), import the NewRelic class:

    import com.newrelic.agent.android.NewRelic;


    We do not support starting the mobile monitoring agent in other classes, as that can cause unexpected or unstable behavior.

  3. Initialize mobile monitoring. In the onCreate() method, add this call:

  4. (Optional) Change the logging level.

  5. Build and run your application. Clean your project, then run your app in an emulator or device to generate traffic.

  6. Wait a few minutes, then view data for your Android app from the Summary page. Go to one.newrelic.com > Mobile > (select an app).

If you have problems with your Android installation, or if you do not see data in the mobile monitoring UI, follow the troubleshooting procedures.

Did this doc help with your installation?

Read on to learn how to avoid the 64K reference limit by enabling multidex support for your Android app.

Enable Multidex support

New Relic's mobile monitoring for Android versions prior to 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. In order 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 and then manages access to the additional DEX files and the code they contain.

When building each DEX file for a multidex app, the build tools perform complex decision making to determine which classes are needed in the primary DEX file so that your app can start successfully. If any class required during startup is not provided in the primary DEX file, then your app crashes with the error java.lang.NoClassDefFoundError.

If you see the java.lang.NoClassDefFoundError error, then you must manually specify these additional classes as required in the primary DEX file:

  1. Create a proguard.multidex.config file within the /app folder of your project. Update mypackage to reflect your package name.

    # keep class names #
    #Keep New Relic in the main dex
    -keep class com.newrelic.** { *; }
    -keep class com.mypackage.activities.** { *; }
  2. Merge the following code into your app-level build.gradle file:

    android {
    multiDexKeepProguard file("proguard.multidex.config")

For more information, see the Android Developers documentation on declaring classes required in the primary DEX file.

New Relic Gradle Plugin configuration

For details on how to configure the behavior of the Android agent plugin during Gradle builds, see the New Relic Gradle plugin extension guide.

Copyright © 2023 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.