gradle 3.1.2依赖于图书馆的产品风味

时间:2018-05-02 18:32:02

标签: android gradle

Gradle版本3.1.2

我有一个应用程序和一个模块。该模块具有产品口味。当我使应用程序依赖于模块时,gradle会失败。这是gradle文件,

app gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.myapplication"
        ...
    }
    buildTypes {
         release {
             ...
         }
    }
}

dependencies {
     implementation fileTree(include: ['*.jar'], dir: 'libs')
     ...
     implementation project(':mylibrary') <- this is the dependency added
}

模块gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 19
        ...
    }

    buildTypes {
        release {
            ...
        }
    }

    flavorDimensions 'api'
    productFlavors {
        v1_0 {
            dimension 'api'
        }

        v2_0 {
            dimension 'api'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ...
}

以下是gradle生成的错误,

Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :mylibrary.
Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve project :mylibrary.
Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve project :mylibrary.
Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve project :mylibrary.
Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve project :mylibrary.

如何使应用程序依赖于使用产品风味的模块?

gradle 3 +

实施项目(路径:':mylibrary',配置:'v1_0')不再有效

1 个答案:

答案 0 :(得分:0)

从Gradle 3.0开始,您必须使用missingDimensionStrategy

这意味着您需要告诉您的app模块将哪些样式用于库中具有的维度,而您的app模块则没有。

defaultConfig {
    applicationId "com.example.myapplication"
    missingDimensionStrategy 'api', 'v1_0'
    ...
}

当然,您不仅可以在missingDimensionStrategy中使用defaultConfig,而且还可以将debug{}release{}的buildTypes用于productFlavors,或者稍后再添加。 >