Configure ProGuard or DexGuard for Android Apps

Admins or Owner

Follow these instructions to support ProGuard or DexGuard with New Relic Mobile for Android. If you have previously installed the Android agent SDK for New Relic Mobile, follow the steps before upgrading to the latest version with ProGuard or DexGuard.

Configure ProGuard

To add support for ProGuard:

  1. Add a New Relic Mobile exclusion to your app; for example:

    • To support New Relic instrumentation, add keep class.
    • To disable warnings related to unresolved problems, add dont warn.
    • To preserve line numbers for crash reporting, add LineNumberTable.
    • To preserve stack information for handled exceptions, add SourceFile and EnclosingMethod.
  2. Add the following to your ProGuard config file (proguard.cfg, proguard-android.txt, proguard-rules.pro, etc.):

    -keep class com.newrelic.** { *; }
    -dontwarn com.newrelic.**
    -keepattributes Exceptions, Signature, InnerClasses, LineNumberTable, SourceFile, EnclosingMethod
    
  3. Clean and rebuild your project.
  4. Run your app in an emulator or device to start seeing data on your mobile app's Overview page.
  5. Optional: Use the New Relic Mobile Settings page to customize your mobile app.

If no data appears, review your logcat output for errors.

Network library support

New Relic instrumentation happens before ProGuard obfuscates your code. In order for these libraries to be properly instrumented, their classes must not be obfuscated by ProGuard.

Here are examples of rules to be applied to preserve these classes. This is not a complete list. Different rules will be needed depending on your library or version.

Library Rules
Apache Http
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**
OkHttp 2
-keep class com.squareup.okhttp.* { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
OkHttp 3
-keep class okhttp3.** { *; }
-dontwarn okhttp3.**
-dontwarn okio.**
Retrofit 2
-keep class retrofit2.** { *; }
-dontwarn retrofit2.**

Configure DexGuard

DexGuard uses the same configuration options as ProGuard, and it can read any pre-existing ProGuard configuration files. DexGuard also has its own set of additional configuration options and provides sample configurations for many common app frameworks.

To add support for DexGuard:

  1. Add DexGuard to your app and set up the Gradle plugin according to the directions from DexGuard.
  2. Create a dexguard-project.txt file to hold specific DexGuard configuration options, and add:

    -dontnote com.newrelic.agent.android.NewRelic
    -dontnote com.newrelic.agent.android.harvest.crash.Crash
  3. After the DexGuard plugin has been configured, verify that your app's buildTypes configuration is similar to this:

    buildTypes {
       release {
          minifyEnabled true
          proguardFile getDefaultDexGuardFile('dexguard-release.pro')
          proguardFile 'proguard-rules.pro'
          proguardFile 'dexguard-project.txt'
       } ...
    
  4. Clean and rebuild your project.
  5. Run your app in an emulator or device to start seeing data on your mobile app's Overview page.
  6. Optional: Use the New Relic Mobile Settings page to customize your mobile app.

If no data appears, review your logcat output for errors.

For more help

Recommendations for learning more: