How do i prevent gradle from exporting a dependency?

I have two Android apps that share a subproject. The build.gradle in the subproject contains a dependency like this:

dependencies {

compile “my.library.dependency:${version}” }

What I’d like is to use this dependency at compile time, but not runtime. This is because the two apps need to link against slightly different versions of the dependency (same API).

The Gradle docs describe dependency configurations like this:

compile The dependencies required to compile the production source of the project.

runtime The dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.

If runtime also includes compile dependencies, does this mean the library is exported from the subproject to the parent projects (and included in my apk)? If so, how do I prevent this? I assume it’s possible because it says “by default”.

Thanks in advance…

You need to create another configuration which is setup to be included only for compilation. Then add your dependencies to this instead of the compile configuration, like this: http://stackoverflow.com/a/17696142/260280

Thanks Stig. I couldn’t get that approach to work with my build.gradle for Android. However I’m upgraded AGP and now I’ve got the “provided” configuration and everything works!