Disable New Relic for Android debug builds


New Relic gradle plugin for Android drastically slows down app build time. For debug builds it’s not even necessary. Is there any way, how to enable it only for release builds?


Hi @ondrej. While it may be possible to configure the agent to not perform instrumentation on debug builds, we hesitate to provide this information because in general, testing in your debug environment should be done alongside New Relic. But if you really want to do that, you can tell Gradle to only include a compile time dependency by target using the following:

dependencies {
    debugCompile "mydebugdependency"
    releaseCompile "myreleasedependency"

Please note the debug and release monikers. This is not just for the New Relic agent. Any compile time dependency can be made debug or release dependent as called above.

If you do this, you must create logic around the starting call for the agent, otherwise the application will crash. The start call being NewRelic.withApplicationToken("ThisToken").start(this.getApplication()); within the MainActivity.

For more information about compile configuration, you can check this documentation out: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Dependencies-on-binary-packages

Thanks. I think this approach won’t remove instrumentation hidden in newrelic plugin coming from buildscript dependency classpath "com.newrelic.agent.android:agent-gradle-plugin:5.4.1", which is slowing down build. I’d like to do not apply newrelic plugin in debug build at all.


After some scouring Gradle docs, I was able to find a way to disable the New Relic instrumentation task when compiling the app for debug.

The below script can go into your application module’s build.gradle file directly without modification. If you have flavors the script is smart enough to know if those are debug build types still - or you can further hone in on the builds you don’t want to run with NewRelic by changing/appending to the tasks listed in the task.name check.

task disableNR << {
        logger.info("[newrelic] newRelicInstrumentTask will be disabled")
    try {
        newRelicInstrumentTask.enabled = false
    } catch (Exception e) {
        logger.log(LogLevel.ERROR, "[newrelic] an error occured attempting to disable New Relic instrumentation." + "\n" + e.printStackTrace())
    if (newRelicInstrumentTask.enabled == true) {
        logger.log(LogLevel.ERROR, "[newrelic] an error occured attempting to disable New Relic instrumentation.")
    } else {
        logger.info("[newrelic] newrelicInstrumentTask has been disabled")

tasks.whenTaskAdded { task ->
    if (task.name.startsWith("transformClassesWithDexFor")) {
        task.dependsOn disableNR

**Edit: ** I found through some rigorous testing that the task compileDebugJava isn’t as consistently ran for debug builds as transformClassesWithDexFor. Depending on how you build your app, that may be a better task to attach to. Keep in mind that this task includes flavor name before the build type so you’ll need to be more specific about the task.


@pknudsen Works fine. Thank you!

1 Like

YAY! :tada:

Thanks @pknudsen and @ondrej!

how about “transformClassesWithDexForDebug” how can I exclude newRelic build plugin
do something like tracing and rewrite some class which cost so many buiding times ,from this task because I can’t skip the hole task

I’m not sure I understand your question, but I’ll do my best to answer. It sounds like you’d still like to keep some of the instrumentation present in the agent, but ignore some others?

Automatic instrumentation is one of the agent’s more convenient features. However, if it gets in the way of the method being traced, add @SkipTrace to the method, and the agent will skip it during compile time instrumentation. To do this you will need to have access to the class and methods directly.

1 Like

@pknudsen the script runs for both debug and release builds and only disables the newRelicInstrumentTask. I’m still seeing newRelicDeinstrumentTask running and other instrumentation running as part of compilation.

Any updates to this script? Evaluating NR for our android app and the performance at build time is a real big downside.

This particular bit of groovy was only to prevent the instrumentation task from running for some build types or variants - it can certainly be expanded or limited if that’s what needed. If the newRelicInstrumentTask doesn’t run, build time should be reduced as the agent won’t be part of class transformation process.

The deinstrument task shouldn’t do anything to substantially increase build time - it’s specifically to remove the agent from the dex process once instrumentation has completed. If no instrumentation occurs, there’s no cleanup to be done.

You will still see New Relic in the build log as 'com.newrelic.agent.android:android-agent:5.+' is also included as a compiled dependency. The android-agent is how New Relic harvests metric data, events and crash reports, and is separate from the instrumentation module included via classpath "com.newrelic.agent.android:agent-gradle-plugin:5.6.+".

Hi NewRelic-- as far as I can tell, this solution no longer works. What’s the eta on providing official support for disabling instrumenting at a build configuration level? It really is an unacceptable compilation hit.

My current solution is to comment out NR from build.gradle entirely, and only re-enable it when it’s time to do a production build. We’ll see if I remember.

@Aaron.Sarazan I’ve still got this running and it’s working on my test app. It’s possible that changes to Gradle or the Android Gradle plugin are in play in your app.

Do you use the Gradle Daemon for all your builds?
If so, does the behavior change if the daemon is disabled?
What version of the Android Gradle plugin are you using?

What seems to work for us is to move the line

apply plugin: 'newrelic'

from the base of our gradle file inside one of the flavour blocks:

productFlavors {

    dev {

    production {
        apply plugin: 'newrelic'

Thanks so much for sharing your workaround @savvas.dalkitsis - this will give others something to try for themselves. Go Community!

There does seem to be a downside though. It seems that newrelic remains enabled if run once even on subsequent builds that do not have it on.

So, for example, on the above sample, running the following:


Will result in the following. The first time, newrelic won’t be invoked. The second time it will (as requested). The problem happens on the third time. New relic is not specified in the dev flavour but it will still run. We think it has something to do with the gradle daemon. Maybe killing it before the last job would not invoke newrelic. Haven’t had time to test this yet


Ooh I see. @savvas.dalkitsis Can’t wait to hear back after you have tested this! :blush:

Keep us posted!

1 Like

A post was split to a new topic: Remove Custom Metrics From Debug Build

This does not work with new relic plugin 5.15.1 and android gradle plugin 3.0.0.

Thanks for letting us know that Paul’s workaround doesn’t work for you, @vinay.nagaraj. Our engineering team is currently working on sorting this. Hang tight and we will update this thread as we learn more on the progress!

The problem is that with

it no longer filters it’s effect only to debug builds