How to apply plugin to only one flavor in gradle?

前端 未结 5 764
我寻月下人不归
我寻月下人不归 2020-12-01 07:27

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 o

相关标签:
5条回答
  • 2020-12-01 07:43

    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.

    0 讨论(0)
  • 2020-12-01 07:54

    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'
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:00

    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.

    0 讨论(0)
  • 2020-12-01 08:00
    1. Define variable - def fl
    2. Initialize variable in you Flavours (and/or builds)

          productFlavors {
      
                  freeFlavour {
                      (...)
                      fl = "free"
                  }
      
                  paidFlavour {
                      (...)
                      fl = "paid"
                  }
              }
      
    3. Use if statement -

      if (fl == "free") { apply plugin: something }

    0 讨论(0)
  • 2020-12-01 08:02

    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
    
    0 讨论(0)
提交回复
热议问题