文档章节

Gradle Implementation vs API configuration

SuShine
 SuShine
发布于 01/24 17:36
字数 703
阅读 19
收藏 0

注:

可以把 Implementation理解为java类的private, 使用Implementation则外部依赖本库的代码引用不到本库Implementation的代码, 也就是降低依赖树的层级, 这样就可以避免外部代码编译时候, 导致本库依赖的库的代码发生编译. 从而降低编译时间

 

I think this topic needs a bit more coverage because maybe is not so immediate for every developer.

Gradle compile keyword has been deprecated in favor of the new api and implementationkeywords.

I will not explain api, because it's the same thing as using the old compile, so if you replace all your compile with api everything will works as always.

To understand the implementation keyword we need an example.

EXAMPLE

We have this library called MyLibrary where internally we are using another library called InternalLibrary. Something like this:

//internal library module
public class InternalLibrary {
    public static String giveMeAString(){
        return "hello";
    }
}

//my library module
public class MyLibrary {
    public String myString(){
        return InternalLibrary.giveMeAString();
    }
}

The build.gradle dependencies of MyLibrary its like this:

dependencies {
    api project(':InternalLibrary')
}

Now in your code you want to use MyLibrary so you should have a build.gradle with this dependency

dependencies {
    api project(':MyLibrary')
}

In your application code, with the api keyword (or using the old compile) you can access both MyLibrary and InternalLibrary.

//so you can access the library (as it should)
MyLibrary myLib = new MyLibrary();
System.out.println(myLib.myString());

//but you can access the internal library too (and you shouldn't)
System.out.println(InternalLibrary.giveMeAString());

In this way you are potentially "leaking" the internal implementation of something that you shouldn't use because it's not directly imported by you.

To prevent this, Gradle has created the new implementation keyword, so now if you switch api to implementation in your MyLibrary

dependencies {
    implementation project(':InternalLibrary')
}

And in your app build.gradle

dependencies {
    implementation project(':MyLibrary')
}

you won't be able to call InternalLibrary.giveMeAString() in your app code anymore. While if MyLibrary uses the api keyword to import InternalLibrary, in your app you will be able to call InternalLibrary.giveMeAString() without problems, independently if you use api or implementation to add MyLibrary to your app.

Using this sort of boxing strategy the Android Gradle plugin knows that if you edit something in InternalLibrary it will trigger the recompilation of MyLibrary only. It will not trigger the recompilation of your entire app because you don't have access to InternalLibrary. This mechanism when you have a lot of nested dependencies can speed-up the build a lot.(Watch the video linked at the end for a full understanding of this)

CONCLUSIONS

  • When you switch to the new Android Gradle plugin 3.X.X, you should replace all your compilewith the implementation keyword (1*). Then try to compile and test your app. If everything it's ok leave the code as is, if you have problems you probably have something wrong with your dependencies or you used something that now is private and not more accessible. Suggestion by Android Gradle plugin engineer Jerome Dochez (1)*)

  • If you are a library mantainer you should use api for every dependency which is needed for the public API of your library, while use implementation for test dependencies or dependencies which must not be used by the final users.

Useful article Showcasing the difference between implementation and api

REFERENCES (This is the same video splitted up for time saving)

Google I/O 2017 - How speed up Gradle builds (FULL VIDEO)

Google I/O 2017 - How speed up Gradle builds (NEW GRADLE PLUGIN 3.0.0 PART ONLY)

Google I/O 2017 - How speed up Gradle builds (reference to 1*)

Android documentation

本文转载自:https://stackoverflow.com/questions/44413952/gradle-implementation-vs-api-configuration

共有 人打赏支持
SuShine
粉丝 126
博文 568
码字总数 154706
作品 0
朝阳
后端工程师
私信 提问
android gradle 3.0 学习笔记

Android gradle 3.0带来了编译速度的大幅优化,api也做了大幅调整 image.png android gradle 3.0 需要Gradle版本 >= 4.1 变化 引用library时会自动引用对应的flavor,比如app的 debug varian...

yangweigbh
2017/10/27
0
0
了解一下:一款好用的 Maven Library 发布插件

GitHub: gradle-publish-plugin Gradle Publish Plugin A flex gradle plugin for publish your library to maven repository become easy. Feature support publish multi-library, such as......

yonglan.whl
前天
0
0
Android Gradle 插件 3.0 挖坑日记

为了提升编译速度,这几天用上了 AS 3.0 和 Gradle 3.0 插件,不得不说不论是 AS 3.0,还是 Gradle 3.0 都变化非常大,具体的更新日志参考下官网,其中 Gradle 3.0的配置和可能遇到的问题可以...

WeaponZhi
2018/10/29
0
0
android studio 3.1 升级gradle4.4时所踩到的坑

转载自:https://blog.csdn.net/Publicx/article/details/80224838 公司原项目是使用Android studio 2.3编译的,最近将Android studio升级到3.1后,build apk 时提示: The project is using......

犀牛有脾气
2018/11/04
0
0
如何在Android studio上运行从github上下载的RN项目

想要编译别人的RN项目,还是要踩踩坑才能走上正轨啊,分享下我试过多种方法后最喜欢的方法(其实是因为我多次用VS Code编译都是以失败而告终,所以才选择的studio) 注意:这一步是你的开发环...

思考的种子
01/28
0
0

没有更多内容

加载失败,请刷新页面

加载更多

RabbitMQ入门

RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue)协议的开源实现。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面都非常的优秀。是当前最主流的消息中间...

watermelon11
今天
14
0
今天的学习

自动加载:方法一 function __autoload( $className ){在这里,完成加载B这个类文件的工作。}class A{} //这是一个类$a1 = new A(); //这里没有自动加载的发生,因为A这个类...

墨冥
今天
2
0
印刷工艺步骤

印刷厂从收到订单到交付整个流程,一般涉及到以下步骤 1.设计(经过软件如cdr,psd,ai等等设计需要印刷的名片,宣传单,画册等物料); 2.排版拼版(在电脑软件这区域完成); 3.出版、出硫...

focusone
昨天
2
0
virtualbox中安装ubuntu

virtualbox+ubuntu 安装virtualbox,当前版本是6.0.4 下载ubuntu安装盘,建议lubuntu,链接是http://mirrors.ustc.edu.cn/ubuntu-cdimage/lubuntu/releases/18.04.2/release/lubuntu-18.04.......

chuqq
昨天
5
0
exists 谓词的子查询

https://blog.csdn.net/qq_19782019/article/details/78730882

仟昭
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部