【问题标题】:Add dependency to specific productFlavor and buildType in gradle在 gradle 中添加对特定 productFlavor 和 buildType 的依赖
【发布时间】:2013-11-04 13:00:52
【问题描述】:

我想知道如何在 gradle 中添加对特定 productFlavor 和 buildType 的依赖。 例如我有productFlavor free 和构建类型release,如何添加对assembleFreeRelease 任务的依赖?

我尝试了许多变体,但都不起作用。

例如我试过:

task('release', dependsOn: assembleProductionRelease) {
} 
// error: Could not find property 'assembleProductionRelease' on root project 'app'.

或者:

task('release', dependsOn: 'assembleProductionRelease') {
}

这里没有错误,但是任务是针对每种风格和构建类型执行的,非常混乱。

【问题讨论】:

    标签: android build gradle


    【解决方案1】:

    有对风味和 buildType 依赖项的内置支持。

    dependencies {
       flavor1Compile "..."
       freeReleaseCompile "..."
    }
    

    http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies

    【讨论】:

    • 如果您的依赖项块位于 android 块之后,则此方法有效
    • 但是如何将风味与 buildTypes 结合起来,例如flavor1DebugCompile 或 flavor1ReleaseCompile。这对我不起作用
    • 要支持“flavorBuildTypeCompile”,您需要使用适当的配置名称“flavorBuildTypeCompile”声明“配置”块,请参阅此答案stackoverflow.com/a/28993581/3256989
    • developer.android.com/studio/build/dependencies: "但是,如果您想为结合了产品风格和构建类型的变体添加依赖项,则必须在配置块中初始化配置名称。以下示例为您的“freeDebug”构建变体添加一个 runtimeOnly 依赖项(使用本地二进制依赖项):“
    【解决方案2】:

    这些任务是根据您的 Android 插件配置动态生成的。在配置时,您还不能使用它们。您可以通过两种方式推迟创建任务:

    等到project is evaluated

    afterEvaluate {
        task yourTask(dependsOn: assembleFreeRelease) {
            println "Your task"
        }
    }
    

    Lazily declaring the task dependency 作为字符串。

    task yourTask(dependsOn: 'assembleFreeRelease') {
        println "Your task"
    }
    

    【讨论】:

      【解决方案3】:

      看起来现在在 android gradle 插件的版本 1 和 gradle 2.2+ 中,您可以通过配置来做到这一点:

      configurations {
        freeReleaseCompile
      }
      
      android {
         ...
        productFlavors {
          free {
            ...
          }
        }
      }
      
      dependencies {
        freeReleaseCompile('myLib@aar')
      }
      

      参考:https://groups.google.com/forum/#!msg/adt-dev/E2H_rNyO6-o/h-zFJso-ncoJ

      【讨论】:

      • 这仍然是正确的。如果有人想使用实现而不是编译,编译可以替换为Implementation
      【解决方案4】:

      Muschko 的回答对我不起作用,所以这是我的解决方案,由我编写并发布 here

      定义只应在特定 buildType/variant/flavor 上执行的任务

      task doSomethingOnWhenBuildProductionRelease << {
          //code
      }
      

      使用“

      gradle添加任务时动态设置依赖

      tasks.whenTaskAdded { task ->
          if (task.name == 'assembleProductionRelease') {
              task.dependsOn doSomethingOnWhenBuildProductionRelease 
          }
      }
      

      【讨论】:

        【解决方案5】:
        android {
            ext.addDependency = {
                task, flavor, dependency ->
                    def taskName = task.name.toLowerCase(Locale.US)
                    if (taskName.indexOf(flavor.toLowerCase(Locale.US)) >= 0) {
                        task.dependsOn dependency
                    }
            }
        
            productFlavors {
                production {
                }
                other
            }
        
            task theProductionTask << {
                println('only in production')
            }
        
            tasks.withType(JavaCompile) {
                compileTask -> addDependency compileTask, "production", theProductionTask
            }
        }
        

        坦率地说,我不知道使用哪个语言环境来生成编译任务的名称,所以toLowerCase(Locale.US) 可能会适得其反。

        【讨论】:

          【解决方案6】:

          这是我使用的另一种方式:

          tasks.withType(JavaCompile) {  
              compileTask ->  
              def dependedTaskName = "dependedTask_";  
              if(compileTask.name.contains('Release') {  
                dependedTaskName += "Release";  
              }  
              createTask(dependedTaskName, Exec) {  
               ........  
              }  
          
              compileTask.dependsOn ndkBuildTaskName  
          }  
          

          另一种方式:

          tasks.whenTaskAdded { task ->  
              if (task.name == 'generateReleaseBuildTypeBuildConfig') {  
                  task.dependsOn doSomethingForReleaseBuild   
              }  
          }   
          

          第一种方法是动态的,而第二种方法更简单。

          【讨论】:

            【解决方案7】:

            以上所有答案都对我有所帮助,但我最终需要在编译每种风味的调试 + 发布版本之前运行一个脚本。脚本的第一个参数采用风味名称。

            为了尽早触发此触发器,但在创建风味特定任务之后,关键是将dependsOn绑定到generateMyflavorDebugBuildConfig。

            这是我最终得到的脚本,可以放在 app/build.gradle 的底部

            // When a task is active, loop through all app flavors, and see if the 
            // task.name matches the earliest task we can set up the dependsOn
            
            // loop through all flavors, and create a task that does the work we want to do 
            // before anything else
            android.productFlavors.all { flavor ->
                task("${flavor.name}RunCommandBefore", type: Exec) {
                    workingDir "$projectDir/../.."
                    commandLine 'sh', 'scripts/run_thing.sh', "${flavor.name}"
                }
            }
            
            // when tasks created, loop through and as early as we can, bind the 
            // RunCommandBefore task we created above
            tasks.whenTaskAdded { task ->
                def taskName = task.name
                // loop through all flavors
                android.productFlavors.all { flavor ->
                    def flavorName = flavor.name 
                    // loop through release types so that this happens for debug and release
                    ['debug', 'release'].each { releaseType -> 
                        // generateMyflavorDebugBuildConfig is the earliest task we're able  
                        // to set up the dependsOn to make sure that our script runs 
                        if (taskName.toLowerCase() == "generate${flavorName.toLowerCase()}${releaseType}buildconfig") {
                            // now myflavorRunCommandBefore will run before 
                            // generateMyflavorDebugBuildConfig
                            tasks."$taskName".dependsOn "${flavorName}RunCommandBefore"
                        }
                    }
                }
            }
            

            希望这对某人有所帮助!令人惊讶的是,Android 首先运行一个简单的脚本是多么困难..

            【讨论】:

              猜你喜欢
              • 2017-05-26
              • 2015-07-25
              • 2014-05-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-06-02
              相关资源
              最近更新 更多