Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use gradle module as a dependency with specific flavor

I have a library module called library that has two flavors flavor1 and flavor2. Then I have two application modules, lets call them app1 and app2 that will use this library module and each one will use it with specific flavor .. eg. app1 will always use library with flavor1 flavor. How should I configure gradle dependencies to make it do what I want to? If I try just implementation project(':library') in app1 build.gradle it obviously does not work because it doesn't know which flavor to choose .. so I’ve tried something like implementation project(path: ':library', configuration: 'flavor1Release') but that does not work too, it prints a lot of errors like

ERROR: Unable to resolve dependency for ':app1@debug/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@debugAndroidTest/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@debugUnitTest/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@release/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

ERROR: Unable to resolve dependency for ':app1@releaseUnitTest/compileClasspath': Could not resolve project :library.
Show Details
Affected Modules: app1

I've seen some questions here on SO like here that says to copy flavors from library module to main app module, but it does not make sense to me, app1 doesn't know anything about flavor2 and I don't want to introduce it there.

like image 624
Billda Avatar asked May 27 '19 10:05

Billda


People also ask

How do I force Gradle to use specific dependency?

If the project requires a specific version of a dependency on a configuration-level then it can be achieved by calling the method ResolutionStrategy. force(java. lang. Object[]).

What is Gradle product flavors?

This means you can generate different versions or variants of your app using a single codebase. Product flavors are a powerful feature of the Gradle plugin from Android Studio to create customised versions of products. They form part of what we call Build Variants.

How to avoid transitive dependencies in Gradle?

Excluding a dependency from a configuration completely Similar to excluding a dependency in a dependency declaration, you can exclude a transitive dependency for a particular configuration completely by using Configuration. exclude(java. util.

How do you change transitive dependency on Gradle?

To override the version of a transitive dependency in Gradle, exclude it from the declared dependency that pulls it in, and then explicitly declare the version that you prefer to use in your build.


1 Answers

You should define missingDimensionStrategy for both app1 and app2.

For example:

// app1/build.gradle

 defaultConfig {
    missingDimensionStrategy 'library', 'flavor1'
}

// app2/build.gradle

 defaultConfig {
    missingDimensionStrategy 'library', 'flavor2'
}
like image 196
Saeed Masoumi Avatar answered Sep 18 '22 15:09

Saeed Masoumi