If you have ever worked on an Android library, you might have faced the problem where you have some other private JAR dependencies which are hosted on a private Maven repository.

For your library to work, you now need to make these dependencies public or copy the JARs into you project.

Copying the JARs manually is not an ideal solution, this creates unwanted overhead whenever there are new versions available, it is easy to make mistakes and easy to let the dependencies get outdated.

After trying to solve this problem for a while, I finally found A solution. You can tell gradle to use a remote dependency as if it is a local JAR and it will copy and package the files for you.

First, create a new configuration, I called mine privateLibs:

configurations { privateLibs }

Next, add the dependencies you want to that configuration. I am using OkHttp for this example so others can reproduce these steps, but it is a very bad idea to actually do this with publicly available libraries:

dependencies {

privateLibs 'com.squareup.okhttp3:okhttp:3.3.1'

}

Lastly, add the privateLibs configuration as a FileTree to the compile configuration:

dependencies {

compile configurations.privateLibs.asFileTree

}

If you extract the AAR you will see your dependencies in the libs folder, and if you turn on obfuscation you will see that they are actually removed from the libs folder and merged into your classes.jar.

An example library can be found on github