创建gradle依赖项 - 删除对其自身依赖项的访问权限

时间:2016-03-16 00:30:03

标签: android maven gradle dependencies okhttp3

我正在处理的未发布的Android库中有第三方网络库 - 在这种情况下是OkHttp。

使用此库作为依赖项的项目现在也可以使用该网络库创建对象。

我可以限制或禁止访问我的库中包含的网络库吗?

3 个答案:

答案 0 :(得分:1)

您可以创建依赖项transitive但是如果您的代码遇到其应用程序中丢失的代码,则ClassNotFound或MethodNotFound将失败

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

如果代码与您的lib打包在一起,那么任何想要从您的lib中使用它的人都可以使用它。

这仍然无法解决您想要的问题,但您可以使用proguard重命名okhttp类。您的lib的用户仍然可以调用okhttp类,但是他们会被proguard重命名为a,b,c,......

答案 1 :(得分:1)

你想要做的是阴影依赖。以下是关于shading dependencies in Gradle的博文中的说明:

  

Shade一个库就是把所有库的内容文件放进去   他们在你自己的罐子里,并改变他们的包装。这是不同的   打包只是将库文件放在你的旁边   自己的jar而不将它们重新定位到不同的包。术语   fatjar通常用于指代具有应用程序的罐子   以及它的依赖关系打包或阴影。

对于较小的库,这很容易。我可以想象一下像OkHttp这样的大型图书馆可能很难。您只需将文件复制到项目中并更改包即可手动完成。还有some scripts会自动执行此操作。他们通常使用Jar Jar Links

答案 2 :(得分:1)

Normally be default you don't have the dependencies like that:

compile rootProject.ext.okhttp

compiled in your jar only your sources are. So OkHttp classes will not be in your lib.

I have exactly the same case. I use gradle to build and upload to maven. You can check here

So if your intention is to have the exact dept version in the package and to be hidden you just need to include it in you project as a module and to change some things like the package of OkHttp to avoid conflicts and also the access to currenly public okhttp members. OkHttp is using Okio so you may want to privatize it too.

Note that this kind of shadowing + hiding functionality of the shadowed class can be useful for framework dependencies(ensuring all depts in runtime available) but it is increasing the size of your libs and will not be the best option for apps using your lib as they anyway ensure packaging required depts in the apk.