Gradle - DependencySubstitution for Android application with library

I have an Android application project with a separate Android library module that is published as binary. I would like to add the ability to switch gradle between creating a library from sources or using a published artifact. Android app depends on binary artifact by default:

compile "com.example.konstantin.mylibrary:mylibrary:${mylibraryVersion}"

Now I want my binary artifact to be replaced with the source code, so I am adding the following code to the root build.gradle file:

 configurations.all { resolutionStrategy { dependencySubstitution { substitute module("com.example.konstantin.mylibrary:mylibrary:${mylibraryVersion}") with project(':mylibrary') } } 

However, when I try to build a gradle, the binary artifact is still taken. What is wrong here?

here is the complete source code

It is also interesting that if I move the dependency substitution code to the allprojects section or to the build.gradle file for the application module, than gradle will not be created with the following message:

 Error:Module version MyApplication:app:unspecified, configuration '_debugCompile' declares a dependency on configuration 'default' which is not declared in the module descriptor for MyApplication:mylibrary:unspecified 
+5
source share
1 answer

Finally, I found a working solution. Somehow it works if I do it the other way around. Instead of replacing binary with a project module

 substitute module("com.example.konstantin.mylibrary:mylibrary:${mylibraryVersion}") with project(':mylibrary') 

I can replace the project module with binary:

 substitute project(':mylibrary') with module("com.example.konstantin.mylibrary:mylibrary:${mylibraryVersion}") 

And how magic works. Full working code is available on a separate branch of the repo example.

However, this is not an ideal solution, because I must always bind the project module in settings.gradle and cannot build it without checking it.

+2
source

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


All Articles