frogermcs / AndroidDevMetrics

Performance metrics library for Android development (includes dagger2metrics)
1.54k stars 118 forks source link

Add option to disable plugin for certain flavors #27

Open maciekjanusz opened 8 years ago

maciekjanusz commented 8 years ago

There should be an option to disable plugin for certain product flavors, for example I use a specialized flavor for instrumented testing, where I need all debug tools to be off, since their notifications and actions might interfere with Espresso tests. Right now, if the plugin is not initialized in Application for every debug product flavor, the app will throw a runtime exception:

java.lang.RuntimeException: MethodsTracingManager must be initialized by init(..)

It means that something like this:

if("debug".equals(BuildConfig.BUILD_TYPE) && !"mock".equals(BuildConfig.FLAVOR)) {
    AndroidDevMetrics.initWith(this);
}

is not possible, as the mockDebug build variant will throw the aforementioned exception.

CaptnBlubber commented 7 years ago

I've used a workaround for this issue thats not perfect but works. My app has a a internal and a production flavor. All Debug tools are only included in the Internal variant. The internal flavor has its own Application that extends from my production flavor Application and adds additional devtools that require application initialization in its onCreate() method. Gradle however does not support flavor specific plugin applies, this is why we have to use a "hack" by defining a variable, that gets overridden in the flavor configuration. To actually make this work the plugin has to be applied last.

Here is a stripped sample from my build.gradle:

apply plugin: 'com.android.application'
... other plugins ...

// indicates weather devmetrics should be activated or not.
// Override in desired flavor/buildconfig if you want it
def useDevMetrics = false

android {

... general configuration stuff ... 

    productFlavors {
        internal {
            ... other stuff ...
            useDevMetrics = true
        }
        prod {
            ... other stuff ...
        }
    }
}

// This has to be applied here and not on top with the other plugins
// otherwise the flavor configuration was not done and it would never be true resulting in failing builds
if (useDevMetrics) {
    apply plugin: 'com.frogermcs.androiddevmetrics'
}
rraallvv commented 5 years ago

@CaptnBlubber could this be used in buildTypes to disable certain plugins for the debug variant?

audiserg commented 3 years ago

This is not work. On each build, useDevMetrics will be set to true