Gradle 中基于多口味库的单口味模块

Single flavor module based on multi flavor library in Gradle

我正在研究多口味 app。 (下面 gradle 个文件)

它使用一个名为 tracker 的库,它遵循相同的风格 internalexternal

现在是棘手的部分,来一个名为 feature 的新模块,这个模块没有任何特色,但它需要 tracker 作为依赖项

app.gradle:

android {

    buildTypes {
        debug {
        }
        release {
        }
    }

    flavorDimensions "target"

    productFlavors {
        internal {
            dimension "target"
        }

        external {
            dimension "target"
        }
    }
}

tracker.gradle:

android {

    publishNonDefault true

    buildTypes {
        release {
        }
        debug {
        }
    }

    flavorDimensions 'target'

     productFlavors {
        internal {
            dimension "target"
        }

        external {
            dimension "target"
        }
    }
}

feature.gradle:

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        compileSdkVersion rootProject.ext.compileSdkVersion
        buildToolsVersion rootProject.ext.buildToolsVersion

        defaultConfig {
            minSdkVersion rootProject.ext.minSdkVersion
            targetSdkVersion rootProject.ext.targetSdkVersion
            versionCode 1
            versionName "1.0"
            javaCompileOptions {
                annotationProcessorOptions {
                    includeCompileClasspath false
                }
            }
        }
    }

}

dependencies {
    implementation(
            [...]
            project(':tracker')
    )
}

这是 errors 当我尝试 gradle sync:

Unable to resolve dependency for ':feature@debug/compileClasspath': Could not resolve project :tracker.

Could not resolve project :tracker.
Required by:
    project :feature
 > Project :feature declares a dependency from configuration 'implementation' to configuration 'externalRelease' which is not declared in the descriptor for project :tracker.

Unable to resolve dependency for ':feature@debugAndroidTest/compileClasspath': Could not resolve project :tracker.

Could not resolve project :tracker.
[...]

根据你的问题,我得到的是你试图将库 tracker 添加到你的 feature 模块作为依赖项。在您的 feature.gradle 中尝试以下操作:

dependencies {
    implementation project(':tracker')
}

在 Gradle 3.0 中,有两个新关键字 implementationapicompile 关键字已弃用。您可以使用 implementation 作为默认值。使用 api 特别是当你在你的项目中有一个传递依赖时(模块 -> Lib1 -> Lib2),你需要告诉 Gradle 该模块想要将该依赖传递到其他模块,所以它在运行时和编译时都可供他们使用。

很好的图解说明:

这是一篇很好的文章,讲述了 implementationapi 关键字之间的区别:Implementation Vs Api in Android Gradle plugin 3.0

官方文档解释:

这里是官方文档的简单解释Use the new dependency configurations:

  • 执行:

    When your module configures an implementation dependency, it's letting Gradle know that the module does not want to leak the dependency to other modules at compile time. That is, the dependency is available to other modules only at runtime. Using this dependency configuration instead of api or compile can result in significant build time improvements because it reduces the amount of projects that the build system needs to recompile. For example, if an implementation dependency changes its API, Gradle recompiles only that dependency and the modules that directly depend on it. Most app and test modules should use this configuration.

  • api:

    When a module includes an api dependency, it's letting Gradle know that the module wants to transitively export that dependency to other modules, so that it's available to them at both runtime and compile time. This configuration behaves just like compile (which is now deprecated), and you should typically use this only in library modules. That's because, if an api dependency changes its external API, Gradle recompiles all modules that have access to that dependency at compile time. So, having a large number of api dependencies can significantly increase build times. Unless you want to expose a dependency's API to a separate test module, app modules should instead use implementation dependencies.

希望这对您有所帮助。


更新

为了完整起见,gradle 4.1 似乎存在一个已知问题。使用 4.3 版会有所帮助。感谢达米恩。

好的,感谢@ramin-eftekhari 我设法让它工作

https://github.com/luxsypher/gradle-flavor-dependency

尝试在应用 build.gradle defaultConfig 块中添加此内容:

missingDimensionStrategy "internal"

我的 gradle 版本是 4.4。

在文档中, Android developer and Android Plugin DSL Reference 表明,应该添加以下代码。

missingDimensionStrategy 'external'
missingDimensionStrategy 'target'

Android developer link image

Android Plugin DSL Reference image

但这对我不起作用。最后,我在 feature.gradle.

中添加了以下代码
flavorDimensions 'target'

 productFlavors {
    internal {
        dimension "target"
    }
}