How to apply plugin to only one flavor in gradle?
Solution 1
Use this code:
if (!getGradle().getStartParameter().getTaskRequests()
.toString().contains("Develop")){
apply plugin: 'com.google.gms.google-services'
}
getGradle().getStartParameter().getTaskRequests().toString()
returns something like [DefaultTaskExecutionRequest{args=[:app:generateDevelopDebugSources],projectPath='null'}]
so as stated in the comments Develop
must start with an uppercase.
Solution 2
Tried different solutions, but none of them worked for me. This is what I came up with and seems to work as far as I tested:
build.gradle
productFlavors {
someFlavorWithGoogleGcm {
dimension "type"
applicationId "com.example.withGcm"
ext.useGoogleGcm = true
}
someFlavorWithoutGoogleGcm {
dimension "type"
applicationId "com.example.withoutGcm"
}
}
And outside the configuration, inside the build.gradle
file:
android.productFlavors.each { flavor ->
if (getGradle().getStartParameter().getTaskRequests().toString().toLowerCase().contains(flavor.name) && flavor.ext.useGoogleGcm) {
println("Building flavor with Google GCM [${flavor.name}] - applying plugin")
apply plugin: 'com.google.gms.google-services'
}
}
Solution 3
- Define variable -
def fl
Initialize variable in you Flavours (and/or builds)
productFlavors { freeFlavour { (...) fl = "free" } paidFlavour { (...) fl = "paid" } }
Use if statement -
if (fl == "free") { apply plugin: something }
Solution 4
I simply used apply plugin: 'com.google.gms.google-services'
inside the flavor in app level build.gradle and it works just fine.
productFlavors {
..... your other flavors ....
yourFlv {
....
....
apply plugin: 'com.google.gms.google-services'
}
}
No extra step needed.
Solution 5
I found a solution, but it is not the best so far. So I'm not sure anymore, that what I wanted to do initially is possible. The gradle file evaluation and the choosing of the right flavor and build type is in different phases of the gradle build, so what I've done is:
I use a build parameter from the command line. When that paramerer is true, I apply the plugin, when it is not even there, I also apply it (for IDE build). I use Jenkins, so I could write that parameter in the build job.
build.gradle file:
// First we have to attach a task to the project, which will be running first
gradle.projectsEvaluated {
preBuild.dependsOn(applyNewRelicByProperty)
}
// Then check on the parameter, which comes from the command line
task applyNewRelicByProperty {
if(!project.hasProperty('compileNewRelic')) {
// NewRelic on: 'compileNewRelic' property not set, so running from IDE.
apply plugin: 'newrelic'
} else if(project.hasProperty('compileNewRelic') && project.getProperties().get('compileNewRelic').equals('true')) {
// NewRelic on: 'compileNewRelic' property is set and it is 'true'.
apply plugin: 'newrelic'
} else {
// NewRelic off
println("No NewRelic")
}
}
And you have to run the gradle build by this:
assembleYourApp -PcompileNewRelic=true
![szidijani](https://i.stack.imgur.com/0Rf0R.jpg?s=256&g=1)
szidijani
I'm a true mobile enthusiast, who always searches for smart solutions :)
Updated on June 16, 2022Comments
-
szidijani about 2 years
I have a multi-flavored, multi-build-typed android project and I want to integrate the NewRelic plugin. But I have to apply it only for one of the customers, thus only for one product flavor.
NewRelic uses instrumentation and the plugin would generate code in other flavors if I applied the plugin there, and that is not permitted for us.So my question is: How can I use the
apply plugin: something
command in the gradle file to be applied to only one of my flavors? -
Sebastiano about 8 yearsThis does not work for me as of today. Apparently, the
build.gradle
parser looks forapply
statements before even evaluating project, hence nullifying theif
checks. -
daemontus over 7 yearsThis doesn't seem to work. In my case (let's say I'm calling assembleFree), both blocks are executed so the value of fl is the name of the last declared flavour. Any other ideas?
-
Phoenix Wang about 7 yearsThis code work. But you need to pay attention that first letter of flavor name should be in Uppercase
-
user3193413 about 6 yearsDidn't worked when clicked assemble from the right side gradle menu... When installed the app manually..
-
l0v3 over 5 yearsThis will be resolved in configuration time, and since this needs to go all over it, the fl will contain the last value.
-
Fabio over 5 yearsthis almost works for me, but I noticed when type
./gradlew assembleDe
that the value ingetTaskRequests
is alsoassembleDe
- when I expected it to be already expanded toassembleDebug
-
Pavel over 4 yearsThe code doesn't work. This prints an empty array "[]" println("${getGradle().getStartParameter().getTaskRequests().toString()}")
-
AlanKley almost 4 yearsI confirmed @Pavel comment. Seems to work from within Android Studio but not when run from command line. This is all I get: [DefaultTaskExecutionRequest{args=[build],projectPath='null'}]
-
Gabriel Gava over 3 yearsThis was the only solution that worked for me. However, I changed your if a little bit to
if (getGradle().getStartParameter().getTaskNames().stream().filter { it.toLowerCase().contains(flavor.name) }.count() > 0 && flavor.ext.useGoogleGcm)
-
Ljdawson over 3 yearsI would suggest others use this
-
Nuwisam over 3 yearsWow, it really is that simple!
-
lilienberg over 3 yearsI used it for exact this use case. It worked for me.
-
mayank1513 over 3 yearsThanks for the complements. If it really helped you, you may go ahead and hit vote up button. That will help others find it easily.
-
anro over 3 yearsThis works for builds through Android Studio, but didn't work for builds with Jenkins.
-
AndroidDev over 3 yearsNot working for me, when I do clean, rebuild, the value that I get is [defaulttaskexecutionrequest{args=[],projectpath='null'}]
-
Slion about 3 yearsIt does not seem to be executing for me. I don't get the logs somehow and the plugin is just not applied. Added extra logs before the if statement, still no joy.
-
Slion about 3 yearsSeems that using .all instead of .each fixes it.
-
Erik about 3 years@Fabio did you find a solution to the problem? E.g. running
./gradlew asF
instead of./gradlew assembleFlavor
fails the solution proposed by this answer: the task request string contains"asF"
but notassembleFlavor
. That is a problem when trying to match e.g."Flavor"
! -
Fabio about 3 yearsNope, I got tired of dealing with firebase and now distribute Apks with slack
-
yorkw almost 3 yearsThis is a syntax cheat, the plugin is still got involved when build other product flavor, tested with android gradle plugin version 4.2.0
-
Kreiri almost 3 yearsUnfortunately, this doesn't seem to work when running tests.
-
Sasha Davydenko about 2 yearsFailed for me with Gradle 7.0.2, the error was
Cannot get property 'shouldMinify' on extra properties extension as it does not exist
. So I needed to add theext.shouldMinify
property to all flavors (and set to according values)