Apply NewRelic for specific build type [Android]

Hello,

I am trying to use NewRelic for just one build type in my app. I have several build types and I don’t want to integrate NewRelic with all of them so some APK’s would be less in size.

According to gradle: apply plugin: 'newrelic'
This applies the plugin to the whole project not to some parts of it. And it crashes if I switch from the build type which Compiles NewRelic to other build type doesn’t compile NewRelic

Process: com.example.foo, PID: 3342
                                                                           java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.foo/com.example.foo.activities.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.foo.activities.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.foo-1/base.apk", zip file "/data/app/com.example.foo-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.foo-1/lib/x86, /system/lib, /vendor/lib]]
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                            Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.foo.activities.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.foo-1/base.apk", zip file "/data/app/com.example.foo-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.foo-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.foo-1/lib/x86, /system/lib, /vendor/lib]]
                                                                               at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                               at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                               at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

and build.gradle

apply plugin: 'com.android.application'

android {
    signingConfigs {
        config {
            keyAlias '###'
            keyPassword '###'
            storeFile file('###')
            storePassword '####'
        }
        configStage {
            keyAlias '###'
            keyPassword '###'
            storeFile file('###')
            storePassword '###'
        }
    }
    compileSdkVersion 25
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.foo"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexKeepProguard file("multidex-config.pro")
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
        }
        stage {
            debuggable true
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled false
            zipAlignEnabled false
            signingConfig signingConfigs.configStage
            apply plugin: 'newrelic'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    stageCompile 'com.newrelic.agent.android:android-agent:5.14.0'
}

As you can see, I wanna apply NewRelic in just Stage build type, but with apply plugin: 'newrelic' makes the app crash as above, and removing it and restarting the daemon makes app works fine but without NewRelic integrated in Stage build type.

Also, ‘Invalidate Caches / Restart’ in Android Studio and ./gradlew --stop only works if I remove that apply plugin: 'newrelic'.

Any help or suggestions would be greatly appreciated.

Hi @ahmed.gamal,

There’s a workaround to disable NR for certain build type. Please check it out. Hope this is helpful to you. :slight_smile:

1 Like

Thank you so much, it works fine now. I really appreciate your reply.

You are welcome :slight_smile: Glad it helped.