Using Android dependencies locally

Sometimes during development when your App depends on open source projects you might find a bug in them, want to use unreleased fixes available in the repository, or want to contribute a new feature. A good way to do this for more substantial changes is to clone the project locally, make the changes, and then you are immediately able to use the code in your App.

However, we needed to do this recently and couldn’t find any good examples of how to do it. It turns out to be quite easy. For this example we will use the RxAndroidBle project.

In your app's build.gradle you would already have something like this:

dependencies {
    ...
    api 'com.polidea.rxandroidble2:rxandroidble:1.11.1'
    api 'com.polidea.rxandroidble2:mockclient:1.11.1'
}

To switch out this remote dependency to a local dependency, first add the project as a submodule to your git repo by running:

git submodule add git@github.com:Polidea/RxAndroidBle.git

Then, in your settings.gradle at the root of the project, add:

// tag::composite_substitution[]
includeBuild('RxAndroidBle') {
    dependencySubstitution {
        substitute module('com.polidea.rxandroidble2:rxandroidble') with project(':rxandroidble')
        substitute module('com.polidea.rxandroidble2:mockclient') with project(':mockrxandroidble')
    }
}
// end::composite_substitution[]

This will replace any dependencies on com.polidea.rxandroidble2:rxandroidble and com.polidea.rxandroidble2:mockclient on their local directories inside the RxAndroidBle directory. Re-sync gradle and you're done!

Note: at the time of writing, there are issues using RxAndroidBle like this in Android Studio 4. I've submitted a pull request to fix this.


Nick