简体   繁体   中英

gradle 3.1.2 dependency on library's product flavor

Gradle version 3.1.2

I have a app and a module. The module has product flavors. When I make the app to depend on the module, gradle fails. Here is the gradle files,

The 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
}

The module 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'])
    ...
}

The followings are the error generated by 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.

How do I make the app depend on the module using a product flavor?

implementation project(path: ':mylibrary', configuration: 'v1_0') is no longer work after gradle 3+

Since Gradle 3.0 you have to use missingDimensionStrategy

That means you need to tell your app module which flavors to use for dimensions that your library has but your app module does not have.

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

You can of course use the missingDimensionStrategy not only in your defaultConfig but also for debug{} and release{} buildTypes or to productFlavors, if you add some later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM