Dependency configurations

2019-07-14 05:02发布

问题:

I know that if I make a library that uses product flavors, then when I use that library in an application, I can do this in a gradle:

dependencies {
    flavor1Compile(path: '{path}', configuration: 'flavor1Config')
    flavor2Compile(path: '{path}', configuration: 'flavor2Config')
}

I also know that I can do this:

dependencies {
    debugCompile(path: '{path}', configuration: 'debugConfig')
    releaseCompile(path: '{path}', configuration: 'releaseConfig')
}

What I want to do is essentially this:

dependencies {
    flavor1DebugCompile(path: '{path}', configuration: 'flavor1DebugConfig')
    flavor1ReleaseCompile(path: '{path}', configuration: 'flavor1ReleaseConfig')
    flavor2DebugCompile(path: '{path}', configuration: 'flavor2DebugConfig')
    flavor2ReleaseCompile(path: '{path}', configuration: 'flavor2ReleaseConfig')
}

But that code produces this:

Error:(30, 0) Gradle DSL method not found: 'flavor1DebugCompile()' Possible causes:

  • The project 'android' may be using a version of Gradle that does not contain the method. Open Gradle wrapper file
  • The build file may be missing a Gradle plugin. Apply Gradle plugin
  • Is there a way to do this?

    回答1:

    There is an open bug on the Android issue tracker to support this.

    As of right now, you can accomplish this by declaring a configuration like so for each combination you want to use:

    configurations {
      flavor1DebugCompile
    } 
    
    dependencies {
      flavor1DebugCompile(path: '{path}', configuration: 'flavor1DebugConfig')
    }