Multi-flavor single-flavor library module in Gradle

I am working on a multi-flavor app . (gradle files below)

It uses a library called tracker that matches the same tastes of internal and external

Now for the complex part, a new module called feature , it has no taste, but it needs a tracker as a dependency

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') ) } 

Here are the errors when I try to execute 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. [...] 
+5
source share
3 answers

From your question, I understand that you are trying to add the tracker library to your feature module as a dependency. In feature.gradle try the following:

 dependencies { implementation project(':tracker') } 

With Gradle 3.0, there are two new keywords implementation and api . compile keyword is deprecated. You can use the default implementation . Use api , especially when you have transitive dependencies in your project (Module → Lib1 → Lib2), and you need to tell Gradle that the module wants to transit this dependency in transit to other modules so that it is available to them both at runtime, so and at compile time.

Good illustrated explanation:

Here is a good article talking about the difference between the implementation and api keywords: Vs Api implementation in Android Gradle plugin 3.0

The official explanation of the document:

Here is a brief explanation from the official documentation to use the new dependency configurations :

  • Implementation:

    When your module configures an implementation dependency, it lets Gradle know that the module does not want dependency leakage to other modules at compile time. That is, the dependency is available for other modules only at run time. Using this dependency configuration instead of api or compilation can lead to significant improvements in build time, as this reduces the number of projects that the build system must recompile. For example, if an implementation dependency changes its API, Gradle will recompile only that dependency and the modules that directly depend on it. Most applications and test modules should use this configuration.

  • api:

    When a module contains an api dependency, it lets Gradle know that the module wants to transit this dependency to other modules so that it is available to them both at run time and at compile time. This configuration behaves just like compilation (which is now deprecated), and you should usually use it only in library modules. This is because if the api dependency changes the external API, Gradle recompiles all the modules that have access to this dependency at compile time. Thus, the presence of a large number of api dependencies can significantly increase build time. If you do not want to expose the dependency APIs to a separate test module, application modules should use implementation dependencies instead.

Hope this helps.


Update

For completeness, there seems to be a known issue with Gradle 4.1. Using version 4.3 helps. Thanks to Damien.

+1
source

Ok thanks @ ramin-eftekhari I managed to get it working

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

0
source

Try adding this to the build.gradle defaultConfig application block:

 missingDimensionStrategy "internal" 
0
source

Source: https://habr.com/ru/post/1274344/


All Articles