Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a gradle dependency - remove access to its own dependencies

An unreleased android library I am working on has a third party networking library in it - OkHttp in this case.

Projects that use this library as a dependency also are now able to create objects with that networking library.

Can I limit or disable access to the networking library contained within my library?

like image 401
CQM Avatar asked Mar 16 '16 00:03

CQM


2 Answers

You could make the dependency transitive however if your code hits the missing code inside their app it will fail ClassNotFound or MethodNotFound

dependencies {
    compile('com.squareup.okhttp3:okhttp:3.2.0') {
        transitive = false
    }
}

Short of that once the code is packaged with your lib it's available to anyone who wants to use it from your lib.

This still won't solve the problem as you would like but you could use proguard to rename the okhttp classes. Users of your lib could still call the okhttp classes but they would be renamed by proguard to something like a,b,c,...

like image 184
JBirdVegas Avatar answered Oct 31 '22 09:10

JBirdVegas


What you want to do is shade the dependency. Here's a description from a blog post about shading dependencies in Gradle:

To Shade a library is to take the contents files of said library, put them in your own jar, and change their package.This is different from packaging which is simply shipping the libraries files in side your own jar without relocating them to a different package. The term fatjar is commonly used to refer to jars that have the application as well as its dependencies packaged or shaded into them.

It's easy to do for smaller libraries. I could image it might be difficult for a large library like OkHttp. You can do it manually by simply copying the files into your project and changing the package. There are also some scripts that will do it automatically. They usually use Jar Jar Links.

like image 28
Edward Dale Avatar answered Oct 31 '22 08:10

Edward Dale