具有维度的特定风味的链接依赖项

M. *_*evi 3 android gradle android-gradle-plugin

我正在开发一个 android 应用程序,它有两种类型:免费和高级。每层有 2 个版本:轻量级和重型。这是 Gradle 的实现:

flavorDimensions "tier", "distro"
productFlavors {
    free {
        dimension "tier"
    }
    premium {
        dimension "tier"
    }
    lightweight {
        dimension "distro"
    }
    heavy {
        dimension "distro"
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试仅为.jar高级层添加依赖项。添加它的方法很简单:

premiumImplementation fileTree(dir: 'libs', include: ['*.jar']) // Works
Run Code Online (Sandbox Code Playgroud)

问题是我有两个版本的 this .jar。上面提到的每个颠覆一个:lightweightheavy

我试图将简单的解决方案应用于这种类型的依赖:

premiumLightweightImplementation fileTree(dir: 'libs', include: ['*lightweight.jar'])
premiumHeavyImplementation fileTree(dir: 'libs', include: ['*heavy.jar'])
Run Code Online (Sandbox Code Playgroud)

但我收到一个gradle错误:

找不到参数的方法 premiumLightweightImplementation() [目录“libs”]

当然,这也适用于 premiumHeavyImplementation。

我确定有一种 gr​​adle-way 可以做我要问的事情。我找到了两个选项来处理这个问题:

  1. 完全停止使用维度(糟糕的解决方案,我必须自己编写所有风味组合)。
  2. 实现一个函数来检索我正在尝试组装的当前风格,并在目录路径中使用其返回值。例如:

    def组合 = getCurrentFlavorCombination()
    premiumImplementation fileTree(dir: "libs/$combination", include: ['*.jar'])

这些是实现这个简单要求的丑陋和不可靠的方法:高级层应该有一个 .jar 链接到它 - 该 .jar 的版本取决于另一个维度 - 发行版。

我并没有要求太多,但在 Gradle 文档中找不到一个很好的方法来做到这一点。我只找到了将库编译成 jars 的方法,但是我的已经在 jars 中了,我只想适当地链接它。也许你们可以在这里帮助我。

非常感谢!

Pet*_*ook 9

I was going by this section of the Android Studio manual which states that you need to declare explicitly any configurations that for flavor/build type combinations. In this case, that means you need to add the following to your build script before the dependencies {} block:

configurations {
    freeLightweightImplementation
    freeHeavyImplementation
    premiumLightweightImplementation
    premiumHeavyImplementation
}
Run Code Online (Sandbox Code Playgroud)