基于Gradle的多风味库的单一风味模块

11

我正在开发一个多品味的 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')
    )
}

这是我尝试进行Gradle同步时出现的错误

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.
[...]

1
尝试这个:implementation project(path: ':tracker', configuration: 'externalRelease') - ramin eftekhari
项目:feature 声明了从配置 'implementation' 到配置 'externalRelease' 的依赖项,但在项目 :tracker 的描述符中未声明。 - Damien Locque
请阅读此博客:https://medium.com/mindorks/implementation-vs-api-in-gradle-3-0-494c817a6fa - Mitesh Vanaliya
4个回答

6

这不像是一个答案,对吧? - Dieter Meemken
文档显示missingDimensionStrategy就是答案。也许它可以帮助那些使用不同gradle版本的人。 - 許雅婷

2

从你的问题中,我了解到你尝试将库tracker作为依赖添加到你的feature模块中。在你的feature.gradle文件中,请尝试以下操作:

dependencies {
    implementation project(':tracker')
}

使用Gradle 3.0,新增了两个关键字implementationapicompile关键字已被弃用。默认情况下可以使用implementation。特别是当项目中存在传递依赖关系(模块-> Lib1-> Lib2),需要告诉Gradle该模块希望在运行时和编译时将依赖项传递导出到其他模块,以便它们都可以使用它。
这里有一篇很好的文章,介绍了implementationapi关键字之间的区别:Implementation Vs Api in Android Gradle plugin 3.0 官方文档Use the new dependency configurations也有简要的解释:
- implementation: 当模块配置实现依赖项时,它会让Gradle知道该模块不想在编译时向其他模块泄露依赖项。也就是说,只有在运行时才能向其他模块提供该依赖项。使用此依赖项配置而不是api或compile可以显著提高构建时间,因为它减少了构建系统需要重新编译的项目数量。例如,如果实现依赖项更改其API,则Gradle仅重新编译该依赖项和直接依赖于它的模块。大多数应用程序和测试模块应使用此配置。 - api: 当模块包含api依赖关系时,它会让Gradle知道该模块希望将该依赖项传递导出到其他模块,以便它们在运行时和编译时都可以使用它。这个配置与compile(现已弃用)完全相同,通常只应在库模块中使用。这是因为,如果api依赖项更改其外部API,则Gradle会重新编译所有具有对该依赖项的访问权限的模块。因此,拥有大量api依赖项可能会显著增加构建时间。除非您想将依赖项的API公开给单独的测试模块,否则应用程序模块应改用implementation依赖项。
希望这能帮助你。

更改为API,目前正在发生以下情况:无法解析依赖项“:feature@debug/compileClasspath”的项目:tracker。无法解析项目:tracker。 所需的是: 项目:feature
无法在以下项目配置之间进行选择:project:tracker: - externalDebugApiElements - internalDebugApiElements 所有这些都与使用者属性匹配。
- Damien Locque
Gradle 4.1存在已知问题,建议使用版本4.3以确保正确性。 - Damien Locque
啊!我明白了。很高兴你能找出问题所在。 - Shobhit Puri
1
@DamienLocque 有没有任何链接可以让我阅读更多关于gradle 4.1的问题? - Shobhit Puri

0

你的示例实际上不起作用。如果你将一些类放入 :tracker 和 :feature 模块中,那么 :feature 模块将无法看到来自 :tracker 模块的类。 - Grekov Serg

-1
尝试将以下内容添加到应用程序的build.gradle文件中的defaultConfig块中:
missingDimensionStrategy "internal"

3
我遇到了一个错误:请求的值列表不能为空 - IgorGanapolsky
请在您的应用程序的 build.gradle 文件的 debug 和 release 配置中使用以下代码片段:debug{ matchingFallbacks = ['release', 'debug'] } release{ matchingFallbacks = ['release', 'debug'] } - Varsha Ravikumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接