Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - DependencySubstitution for Android App with a Library

I have an Android app project with separate android library module inside, that is published as a binary. I'd like to add an ability to switch the gradle between building a library from sources or using the 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 by the source code, so I add the following code in root build.gradle file :

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

However when I'm trying to build gradle is still taking the binary artifact. What is wrong here?

here is the full source code

Also interesting is that if I move dependency substitution code to allprojects section or to application module build.gradle file, than gradle fails to build 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
like image 387
akd005 Avatar asked Apr 22 '16 12:04

akd005


People also ask

How do you resolve transitive dependencies in Gradle?

A variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.

Where Gradle dependencies are stored?

Gradle caches artifacts in USER_HOME/. gradle folder. The compiled scripts are usually in the . gradle folder in your project folder.


1 Answers

Finally I've found a working solution. Somehow that works if I do it the other way round. Instead of substitute binary with project module

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

I can substitute project module with binary :

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

And than the magic works. The full working code is available on separate branch of an example repo

However that's not an ideal solution, because I have to always link the project module in settings.gradle and can not build without checking it out.

like image 191
akd005 Avatar answered Oct 30 '22 17:10

akd005